Double Six principles of opening and closing the principles of software design patterns (PHP language)

Reprinted, the original link https://www.cnblogs.com/zhanghengscnc/p/8299459.html

Six principles design pattern (6): closing Principles

Is defined: a software entity, such as classes, modules and functions should be open for extension but closed for modification.

The origin of the problem: in the software life cycle because of changes, upgrades, and maintenance or other reasons need to modify existing software code, the old code could have introduced errors or may cause us to have to reconstruct the whole function, and requires the original code has been re-tested.

Solution: When software needs to change, try to achieve changes in behavior by extending the software entity, rather than to achieve change by modifying existing code.

Opening and closing the principles of object-oriented design is the most basic design principles that guide us in how to build a stable and flexible system. Opening and closing the principle of the six principles of design patterns may be defined in one of the most obscure, it only tells us open for extension, but closed for modification, but in the end how can we open to extension but closed for modification, and did not explicitly tell us . Previously, if someone told me, "When you make design must comply with the principle of opening and closing," I feel he did not say anything, but it looks like and what the say. Since the open principles really Void.

After careful thought and read many articles design patterns, and finally have a little understanding of the principles of opening and closing. In fact, we follow the design patterns in front of the five principles, purpose and use of 23 kinds of design patterns is to follow the principle of opening and closing. In other words, as long as we abide by the principles of good for front five, a software design is naturally in line with the principle of opening and closing, opening and closing of this principle is more like the previous level of compliance with the five principles of "average score", in front of 5 compliance with the principle of good, average naturally high, indicating that the software designed to comply with the principle of opening and closing of the good; if the previous five principles comply with the bad, then good compliance with the principle of opening and closing.

In fact, I believe that the opening and closing principle nothing more than to express this layer of meaning: a framework for building an abstract, with details to achieve expansion. Because the abstract good flexibility and adaptability, as long as the abstract rational, can remain stable software architecture. The details of the software variable, we used to extend from the abstract implementation class students, when software needs to change, we just need to implement a class derived according to the demand to extend it. Of course, only if we abstract should be reasonable, forward-looking and predictability to the job of changing needs.

Here, then recall earlier said five principles, it is precisely tell us build the framework of abstract, with precautions only achieve expansion details: Single Responsibility Principle tells us to implement a single class duties; Richter substitution principle tells us not damage inheritance system; Dependency inversion principle tells us to be oriented programming interface; Interface Segregation principle tells us to streamline the design of the interface when single; Demeter tells us to reduce the coupling. The opening and closing principle is master, he told us to open for extension, but closed for modification.

Finally, explain how to comply with these six principles. Follow these six principles and is not no problem, but the more and less of a problem, that is to say, we generally do not say there is no compliance, but to say how much the degree of compliance. Anything is too far, six design principles of design patterns is the same, the development of the six principles of our purpose is not to be rigid compliance with them, need flexibility in the use of the actual situation. As long as their level of compliance within a reasonable range, even if it is good design. We use a diagram to explain.

 

Each dimension in the figure each represent a principle, we are in accordance with the degree of compliance with this principle in the dimension drawing a point, if you follow this principle of reasonable, then, this point should fall inside the red concentric circles; If compliance is poor, the point will be inside the small round; if excessive compliance, the point will fall outside the great circle. A good design is reflected in the figures, it should be six vertex of the hexagon in concentric circles.

In the figure, 1 design, design 2 belongs to a well-designed, to the extent that they are in compliance with the six principles of a reasonable range; 3 design, design 4 design although some deficiencies, but basically acceptable; design 5 serious lack of principles are not strictly followed; the transition is designed to comply with the six, design 5 and 6 are designed urgent need to reconstruct the design.

Guess you like

Origin www.cnblogs.com/shamohai/p/11100472.html