1. Existing classes can be easily retrofitted to implement a new interface.
2. Interfaces are ideal for defining mixins.
3. A mixin is a type that a class can implement in addition to its “primary type” to declare that it provides some optional behavior. For example, Comparable is a mixin interface that allows a class to declare that its instances are ordered with respect to other mutually comparable objects.
4. Interfaces allow the construction of nonhierarchical type frameworks.
5. Using interfaces to define types does not prevent you from providing implementation assistance to programmers. You can combine the virtues of interfaces and abstract classes by providing an abstract skeletal implementation class to go with each nontrivial interface that you export. (i.e. AbstractCollection, AbstractSet, AbastractMap, AbstractList, etc.)
6. For most implementors of an interface, extending the skeletal implementation is the obvious choice. Furthermore, the skeletal implementation can still aid the implementor’s task if the class implementing the interface is implemented manually. It can forward invocations of interface methods to a contained instance of a private inner class that extends the skeletal implementation. This technique, known as simulated multiple inheritance, is closely related to the wrapper class idiom discussed in Item 16. It provides most of the benefits of multiple inheritance, while avoiding the pitfalls.
7. Writing a skeletal implementation is relatively simple. First you must study the interface and decide which methods are the primitives in terms of which the others can be implemented. These primitives will be the abstract methods in your skeletal implementation. Then you must provide concrete implementations of all the other methods in the interface.
8. A simple implementation is like a skeletal implementation in that it implements an interface and is designed for inheritance, but it differs in that it isn’t abstract: it is the simplest possible working implementation.(i.e. AbstractMap.SimpleEntry)
9. It is far easier to evolve an abstract class than an interface. Once an interface is released and widely implemented, it is almost impossible to change.
10. An interface is generally the best way to define a type that permits multiple implementations. An exception to this rule is the case where ease of evolution is deemed more important than flexibility and power. If you export a nontrivial interface, you should strongly consider providing a skeletal implementation to go with it. Finally, you should design all of your public interfaces with the utmost care and test them thoroughly by writing multiple implementations.
相关推荐
Item 20: Prefer interfaces to abstract classes Item 21: Design interfaces for posterity Item 22: Use interfaces only to define types Item 23: Prefer class hierarchies to tagged classes Item 24: Favor ...
Item 18: Distinguish Between Value Types and Reference Types 104 Item 19: Ensure That 0 Is a Valid State for Value Types 110 Item 20: Prefer Immutable Atomic Value Types 114 Chapter 3 Expressing ...
专享:prefer的用法__(全).pdf
I get asked this question sometimes from seasoned programmers who are new to C++. There are plenty of good books written on the subject, but I found no clear and concise set of rules on the Internet ...
Minikube and Docker for Mac or Windows should undoubtedly be there for those who prefer to "play" with Docker locally. AWS is the biggest hosting provider so Kubernetes Operations (kops) must be ...
ESLint是针对javascript代码的静态检查工具。资源是本人配置的ESLint 规则文件。官方文档:http://eslint.org/docs/rules/。
public.actor.first_name: error prefer-text-to-varchar : Prefer text to varchar types 用法 安装方式: $ npm i -g schemalint 要运行,请确保您位于具有.schemalintrc.js配置文件的文件夹中,然后键入: $ ...
条款18:让接口容易被正确使用,不易被误用 make interfaces easy to use correctly and hard to use incorrectly. 条款19:设计class犹如设计type treat class design as type design. 条款20:宁以pass-by-...
var prefer = req.prefer; if (prefer.handling === 'strict') { // strict handling } else if (prefer.handling === 'lenient') { // lenient handling } if (prefer.respondAsync) { // client prefers ...
Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control Declaration Order Write Short Functions Google-Specific Magic Smart Pointers cpplint Other C++ Features ...
--tee : mirror output to a filename specified after '--tee' -4 : prefer ipv4 -6 : prefer ipv6 -c : only show an output line on changed state --file : treat the "server-address" as a filename ...
Prefer recent versions of the above packages, in order to avoid compatibility issues, notably for Matplotlib. Note that this latter package is not necessary for the program to run, although you might ...
often prefer more to less. As a result, the ones mentioned above have become major parts of the book. There are also several unusual short subjects, such as the Todd- Coxeter algorithm and the ...
SI3US-721119-13700 psh create the following serial number : prefer number 6 SI3US-000006-39407
启用首选:安全的HTTP标头 Chrome扩展程序启用prefer: safe HTTP标头。 更多信息: :
Berger_Goulding_Rice-Do small businesses still prefer community banks-JBF2014
传单首选画布使用meteorhacks:inject-initial软件包注入L_PREFER_CANVAS = true; 选项,然后加载您喜欢的传单包。 这被添加到您的文档头中: <head> [removed]L_PREFER_CANVAS = true;[removed] ... style...
4.1.3. Prefer non-final classes (optional) 4.1.4. Declare accessors and mutators for persistent fields (optional) 4.2. Implementing inheritance 4.3. Implementing equals() and hashCode() 4.4. Dynamic ...
Identifying the Classes to Be Inherited and Their Objects 170 Identify the Methods to Be Overridden 173 Write the Code 182 Execute the Code 189 Summary 190 Chapter 9 Exception Handling 193 ...