Architect of the road (6) --- OOD principles of opening and closing

2 the principle of opening and closing (Open-Closed Principle, OCP)


2.1 What is the principle of opening and closing the
    opening and closing principle is the cornerstone of the object-oriented design "reusable design" is one of the most important object-oriented design principles, and many other design principles are a means of achieving the principle of opening and closing .


    In 1988, Bertrand Meyer presented in his book "Object Oriented Software Construction" in the opening and closing principle, it is the original text: "Software entities should be open for extension, but closed for modification". Translation: "Software entities should be open for extension, but closed for modification." This is, indeed a little bit professional, we have put it more plainly, that is: the various components included in the system software, such as modules (Modules), class (Classes) and function (Functions), etc., should not be modify existing code based on the introduction of new features. Closing the principle of "open" refers to the function expansion assembly is open, allowing its function expansion; closing principle of "closed", refers to a closed modifications to existing code, i.e., do not You should modify the existing code.


2.2 How to implement the principle of opening and closing
    to achieve the key principles is that the opening and closing "abstract." All possible behavior of the system as an abstraction of the underlying abstraction, the abstraction of the underlying features of the method for all the predetermined specific implementation must provide. As the system design abstraction layer, to anticipate all possible extension, so that in any case the extended abstract the underlying system without modification; Meanwhile, since it is possible to derive one or more new abstraction from the underlying concrete implementation, the system may be varied behavior, so the design of the system is open to expansion.


    We are in the process of software development, has always advocated the demand-driven. This requires us in the design, to be very clear understanding of user needs, determine possible changes in demand included so explicitly using the principle of opening and closing under what circumstances.


    Section on the system variable, and more particularly to a package variability principle (Principle of Encapsulation of Variation, EVP), which split from the software engineering principles to achieve closed further explanation. EVP required to do when the system design, all parts of the possible changes to evaluate the system and classify each of the variable factors are packaged separately.


    We begin in the design phase of the actual development process, we must set out all the possible behavior of the system, and these acts added to the bottom of the abstract, is simply impossible to do so is not economical, time-consuming. In addition, beginning in the design stage, all the variables were expected and packaging is not very realistic, but also very difficult to do it. Therefore, opening and closing the principle that the vision is only an ideal situation or extreme state, the real world is difficult to be fully realized. We only certain components, in part to meet the requirements of the principle of opening and closing.


    Through the above analysis, for the principle of opening and closing, we can conclude that: Although we can not do one hundred percent closed, but at the time of system design, we still have to try to do this.


    For extensions software system, we can through inheritance, overloading or commissioned by other means to achieve. To the interface as an example, it is modified is closed, while the implementation is open, we can provide different implementations based on actual needs, so the interface is in line with the principle of opening and closing.

 


2.3 principles can open and close any good
    if a software system in line with the principle of opening and closing, then from a software engineering point of view, it has at least this advantage:


  reusability good.

    We may in the future of software is completed, the software can still be extended by adding new features, very flexible. Therefore, this software system can by constantly adding new components to meet changing needs.


  maintainability.

    Due to existing software components of the system, especially its not modify the underlying abstract, so we do not worry about the stability of the original system software components, which makes software system changes have a certain stability and continuity .


2.4 Principles opening and closing relationship with other principles of
    opening and closing principle with a color idealism, which is the ultimate goal of object-oriented design. Therefore, for the implementation of the principle of opening and closing, there has been an object-oriented design guru pains to study the implementation of the principle of opening and closing. Richter substitution principle (LSP) to be mentioned later, Dependency Inversion Principle (the DIP), the interface segregation principle (ISP) as well as abstract classes (Abstract Class), the interface (interace), etc., can be regarded as the principle of opening and closing implementation.

Guess you like

Origin blog.csdn.net/wanghao72214/article/details/3985700