Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted to contain implementations. N Y The implemented class must reside the class hierarchy. N Y Single inheritance N Y Easy to evolve N Y Advantages of In…
Reason The constant interface pattern is a poor use of interfaces. That a class uses some constants internally is an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class's exported API. It…
Advantage Disadvantage Enum types Clarity Safety Ease of maintenance. None extensibility Typesafe enum pattern(Interfaces to emulate extensible enums) Extensibility No good way to enumerate all of the elements of a base type and its extension. Extens…
Marker interface is an interface that contains no method declarations, but merely designates (or "marks") a class that implements the interface as having some property. Such as Serializable interface which indicates that the instance implements…
条款17 在operator=中检查给自己赋值的情况 1 2 3 class X { ... }; X a; a = a; // a 赋值给自己 >赋值给自己make no sense, 但却是合法的; 重要的是, 赋值给自己的情况可以以隐蔽的形式出现: a = b; 如果b是a的另一个名字(初始化为a的引用), 那也是对自己赋值; 这是一个别名的例子: 同一个对象有两个以上的名字; 别名可以以任意形式的伪装出现, 在写函数时一定要考虑到; Note 赋值运算符中要特别注意可能出现别名的情况…