Design Mode - six principles of the opening and closing principle (a)

Definition of the principle of opening and closing

  Open Closed Principle (Open Closed Principle, OCP) proposed by Brandt Meyer (Bertrand Meyer), in his 1988 book "Object-Oriented Software Construction" (Object Oriented Software Construction) proposed: Software entities should be open to extension but closed for modification (Software entities should be open for extension , but closed for modification), which is the classic definition of the principle of opening and closing.

  Here software entity includes the following sections:

    1.   Project divided into modules
    2.   Classes and interfaces
    3.   method


  Meaning the opening and closing principle is that: when changing needs of the application, without physical modification of the source code or software binary code can be extended function module, to meet the new demand.

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.

  1. The impact of software testing

    Software to comply with the principle of opening and closing, then, requires only a software test code to test for extensions on it, because the original test code still works correctly.

  2. 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.

  3. You can improve software maintainability

    Comply with the principle of opening and closing software, high stability and strong continuity, making it easy to extend and maintain.

Implementation of the principle of opening and closing

  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 the abstract rational, can remain stable software architecture. The software variable details can be extended abstract is born from the realization of class, when software needs change, you only need to re-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/yuexiaoyun/p/11824500.html