ξ4.1 what is the opening and shutting principle
☆ Open and close principle refers to a software entity that should be developed for expansion, to modify the shutdown (software entities should be opened for extension, but closed for modification). The principle is that when designing a module, the module can be extended without modification, in other words, it should change the behavior of the module without having to modify the source code.
☆ The advantages of the software system to meet the opening and closing principle:
By extending the existing software system, ① can provide new behaviors to meet the new requirements of software, so as to make the software system of change have some adaptability and flexibility.
② existing software modules, especially the most important abstraction layer module can not be modified, which makes the software system changes in a certain degree of stability and continuity.
ξ4.2 the key of realizing opening and closing principle
Abstract is the key to solve the problem, in the object-oriented programming language, the system can be defined a relatively fixed set of abstract design, this design allows endless behavior in the implementation layer is implemented. In language, one or more abstract classes or interfaces can be given, specifying the characteristics of the methods that all concrete classes must provide as an abstraction layer of system design. This abstraction layer foresees all extensibility, so that it does not change in any of the extensions. This makes the system abstraction does not need to modify, so as to meet the opening and closing principle of the second, the modification is closed.
At the same time, because exporting one or more new concrete classes from the abstraction layer can change the behavior of the system, the design of the system is open to the extension, which satisfies the first of the opening and closing principle.
☆ Packaging Principles for variability
This is another description of the principle of closure, which is about finding a variable element of a system that encapsulates it. This principle means two points:
① a variability should not be scattered in many corners of the code, but should be encapsulated inside an object. Inheritance should be seen as a way to encapsulate changes, rather than as a way to generate special objects from generic objects.
② one type of variability should not be mixed with another variability. This means that the general level of inheritance does not exceed two levels.
Key points of knowledge:
☆ The concept of open and close principle, software entities to expand development, to modify the closure;
☆ to realize the key of opening and closing principle, using interface or abstract class to abstract the abstraction layer of the system, the abstraction layer is unchanged, and the implementation layer is used to expand;
☆ Packaging for variability, encapsulating variable elements to prevent changes from spreading to the entire application;
☆ attention to control the granularity of the package, do not combine the two types of variability together;
☆ Inheritance is used to encapsulate the variability, the general level of inheritance should not exceed two layers;
☆ Strategy mode is a good interpretation of the principle of closure, and other Factory mode, construction mode, bridge mode, façade mode, mediator mode, visitor mode and iterative sub-model, etc.
☆ The refactoring behavior of "rewriting conditional transfer statement to polymorphism" should follow the principle of opening and closing, and prevent the pollution of polymorphism.
The single-method interface under ☆java is usually used to implement function pointers or delegate functions;
☆ Any tree inherits the abstract class as the root, the concrete class is not to inherit, not to inherit from the tool class;
☆ Abstract classes have as much common code as possible and have as little data as possible.
☆ When the Coad condition is fully met, you should consider using inheritance: A derived class is a special kind of base class, not one of its roles, that is, to differentiate between "has-a" and "is-a", and never occur when you need to replace a derived class with a derived class of another class , derived classes have the responsibility of extending the base class rather than having the responsibility of displacing or unregistering the base class, and inheritance can only be used if it makes sense in a taxonomic perspective.
Java Design principles