TOP4. The principle of opening and closing-object-oriented design principles

Definition of opening and closing principle

The role of opening and closing principles

1. Impact on software testing

2. Can improve code reusability

3. Can improve the maintainability of the software

How to realize the principle of opening and closing


In software development, in order to improve the maintainability and reusability of the software system, and increase the scalability and flexibility of the software, programmers should try their best to develop programs according to 7 principles, thereby improving software development efficiency and saving software development Cost and maintenance cost. We will introduce these seven principles in sequence in the following sections. This section first introduces the opening and closing principles.

Definition of opening and closing principle


The Open Closed Principle (OCP) was proposed by Bertrand Meyer. In his 1988 book "Object Oriented Software Construction" (Object Oriented Software Construction), he proposed that software entities should be open to extension , Software entities should be open for extension, but closed for modification, this is the classic definition of open and closed principles.

The software entity here includes the following parts:

  1. Modules divided in the project
  2. Class and interface
  3. method

The meaning of the principle of opening and closing is: when the requirements of the application change, the functions of the module can be expanded to meet the new requirements without modifying the source code or binary code of the software entity.

The role of opening and closing principles


The principle of opening and closing is the ultimate goal of object-oriented programming. It enables the software entity to have a certain degree of adaptability and flexibility, as well as stability and continuity. Specifically, its role is as follows.

1. Impact on software testing

If the software complies with the opening and closing principle, only the extended code needs to be tested during software testing, because the original test code can still run normally.

2. Can improve code reusability

The smaller the granularity, the greater the possibility of being reused; in object-oriented programming, the reusability of code can be improved based on atomic and abstract programming.

3. Can improve the maintainability of the software

The software that complies with the opening and closing principle has high stability and continuity, which makes it easy to expand and maintain.

How to realize the principle of opening and closing


The principle of opening and closing can be realized through "abstract constraints, encapsulation changes", that is, a relatively stable abstraction layer is defined for software entities through interfaces or abstract classes, and the same variable factors are encapsulated in the same concrete realization class.

Because abstraction has good flexibility and wide adaptability, as long as the abstraction is reasonable, the stability of the software architecture can be basically maintained. The changeable details in the software can be extended from the implementation class derived from abstraction. When the software needs to change, only need to re-derive an implementation class to extend it according to the requirements.

The following uses Windows desktop themes as an example to introduce the application of the opening and closing principle.

[Example 1] Windows desktop theme design.

Analysis: The theme of Windows is a combination of elements such as desktop background images, window colors, and sounds. Users can change their desktop themes according to their favorites, or download new themes from the Internet. These themes have common characteristics, you can define an abstract class (Abstract Subject) for them, and each specific subject (Specific Subject) is its subclass. The user form can select or add new themes as needed, without modifying the original code, so it satisfies the principle of opening and closing. Its class diagram is shown in Figure 1.
 

Windows desktop theme class diagram

                                                           Figure 1 Windows desktop theme class diagram

<The relationship between class diagrams and class diagrams in UML, the                                                                                                                              principle of Liskov substitution>

Guess you like

Origin blog.csdn.net/m0_38023584/article/details/106343921