OOAD-3 seven object-oriented design principles - the principle of opening and closing

In software development, software system in order to improve the maintainability and reusability, scalability and increase the flexibility of the software, programmers should try to develop a program based on seven principles. Thereby enhancing the efficiency of software development, software development costs and saving maintenance costs. We will in the next few sections in order to introduce these seven principles, this section introduces the principle of opening and closing.

 

Definition of the principle of opening and closing

  Software entities should be open for extension, but closed for modification, this is the classic definition of the principle of opening and closing.

 

  Here software entity includes the following sections

    Carved out of the project module

    Classes and interfaces

    Break the law

 

  Meaning the opening and closing principle is: When the application needs change, without modifying the source code of software entities or binary code, can be a good function expansion modules, to meet the new demand. In other words, adding new demands of the time, you do not need to modify the code before. But the new implementation of the interface, add a new category, or add a configuration to implement new features.

 

The principle of opening and closing action

  Open Closed Principle is the ultimate goal of object-oriented programming, which makes software entities have a certain flexibility and adaptability along with stability and continuity. Specifically, its role is as follows

    Impact of software testing: software to comply with the principle of opening and closing words, software testing only need to test the extension of the code on it. Because the original test code still works correctly.

    Improve the code reusability: The smaller the particle size, the greater the possibility of being multiplexed; in object-oriented programming, and the atomic abstract programming can improve code reusability.

    You can improve the maintainability of software: software to comply with the principle of opening and closing, high stability and strong continuity, making it easy to expand and maintain.

 

Implementation of the principle of opening and closing

  It may be achieved by "abstract constraint, the package changes" on-off principle, i.e. through the software interface or abstract class defines a relatively stable entity abstraction layer, and the same package variables embodied in the same class.

  Because the abstract good flexibility and adaptability, as long as a reasonable abstraction, can remain stable software architecture. The software variable specific implementation details can be extended from a class abstract derived when changes software requirements, only need to implement a class derived according to the demand to extend it.

 

 

  Below is an example to introduce Windows desktop themes application of the principle of opening and closing.


Desktop theme design [Example 1] of Windows.

Analysis: Windows theme is a combination of desktop background pictures, window colors and sounds and other elements. Users can change their desktop themes according to their own favorite, you can also download new themes from the Internet. These themes have common characteristics that can be defined for an abstract class (Abstract Subject), and each specific topic (Specific Subject) is its subclasses. Form user can select or add a new topic, without modifying the original code, so it satisfies the principle of opening and closing, the class shown in Figure 1.

Windows desktop themes class diagram
Figure 1 Windows desktop themes class diagram

Guess you like

Origin www.cnblogs.com/exceptionblog/p/11041044.html