枚举命名规范_Reading 12: Interfaces & Enumerations 接口&枚举

053e62459f9b53e0dce50576893096ec.png

接口

使用接口的好处

  • One advantage of this approach is that the interface specifies the contract for the client and nothing more.
  • 真正的将规格和实现分离。因为在接口中我们不能填充任何实现。
  • Another advantage is that multiple different representations of the abstract data type can co-exist in the same program, as different classes implementing the interface.
  • 同一接口的多个实现可以在一个程序中共存。

tips

  • 在接口中可以定义静态方法,类似构造方法,返回一个具体的实现的类。

Why Interface 使用接口的理由

  • Documentation for both the compiler and for humans
  • 为编译器和人类写文档
  • Allowing performance trade-offs
  • 允许性能权衡
  • Methods with intentionally underdetermined specifications
  • 可以故意放弃统一制定某些方法的规格,而在具体实现中规定
  • Multiple views of one class
  • 可以在一个类中实现多个接口,这也是我们进行多继承的一种方式
  • More and less trustworthy implementations
  • 根据应用程序选择更值得信赖的实现,或者为了某些原因接受有可能有bug的复杂实现

enumeration 枚举

  • 可以用在switch结构中
  • 枚举会被静态检查

All enum types also have some automatically-provided operations, defined by Enum : ordinal() is the index of the value in the enumeration, so JANUARY.ordinal() returns 0. compareTo() compares two values based on their ordinal numbers. name() returns the name of the value’s constant as a string, e.g. JANUARY.name() returns “JANUARY”. toString() has the same behavior as name().

fd12079e117a755b05a8195c40a69d57.png

Summary

Java interfaces help us formalize the idea of an abstract data type as a set of operations that must be supported by a type.

This helps make our code…

  • Safe from bugs. An ADT is defined by its operations, and interfaces do just that. When clients use an interface type, static checking ensures that they only use methods defined by the interface. If the implementation class exposes other methods — or worse, has visible representation — the client can’t accidentally see or depend on them. When we have multiple implementations of a data type, interfaces provide static checking of the method signatures.
  • Easy to understand. Clients and maintainers know exactly where to look for the specification of the ADT. Since the interface doesn’t contain instance fields or implementations of instance methods, it’s easier to keep details of the implementation out of the specifications.
  • Ready for change. We can easily add new implementations of a type by adding classes that implement an interface. If we avoid constructors in favor of static factory methods, clients will only see the interface. That means we can switch which implementation class clients are using without changing their code at all.

Java enumerations allow defining an ADT with a small finite set of immutable values. Compared to the old-fashioned alternatives of special integer values or special strings, enumerations help make our code:

  • Safe from bugs. Static checking ensures that clients can’t use values outside the finite set, or confuse two different enumerated types.
  • Easy to understand. Named constants are not as magical as integer literals, and named types explain themselves better than int or String.
  • Ready for change. Enumerations have no particular advantage here.

Java接口帮助我们将抽象数据类型的概念形式化为必须由类型支持的一组操作。

这有助于我们的代码……

  • 从bug中安全。ADT由其操作定义,接口就是这样。当客户端使用接口类型时,静态检查可确保它们仅使用接口定义的方法。如果实现类暴露了其他方法 - 或者更糟糕的是,具有可见的表示 - 客户端不会意外地看到或依赖它们。当我们有多个数据类型的实现时,接口提供方法签名的静态检查。
  • 容易明白。客户和维护人员确切地知道在哪里寻找ADT的规范。由于接口不包含实例字段或实例方法的实现,因此更容易将实现的详细信息保留在规范之外。
  • 准备好改变。我们可以通过添加实现接口的类来轻松添加类型的新实现。如果我们避免构造函数支持静态工厂方法,客户端将只看到接口。这意味着我们可以切换哪些实现类客户端而不改变它们的代码。

Java枚举允许使用一小组有限的不可变值来定义ADT。与特殊整数值或特殊字符串的旧式替代方法相比,枚举有助于我们的代码:

  • 从bug中安全。静态检查确保客户端不能使用有限集之外的值,或者混淆两种不同的枚举类型。
  • 容易明白。命名常量不像整数文字那样神奇,命名类型比intor更好地解释自己String。
  • 准备好改变。枚举在这里没有特别的优势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值