4. Opening and closing principles-object-oriented design principles

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 the 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 (Bertrand Meyer). In his 1988 book "Object Oriented Software Construction" (Object Oriented Software Construction), he proposed that software entities should be open for extension , Software entities should be open for extension, but closed for modification, this is the classic definition of the open and close principle.

The software entity here includes the following parts:

The module
class and interface
method divided in the project

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 software entities to have a certain degree of adaptability and flexibility at the same time with stability and continuity. Specifically, its role is as follows.

1. Impact on software testing

If the software complies with the open and closed 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, so it is 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 the Windows desktop theme 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 according to needs, without modifying the original code, so it meets the principle of opening and closing, and its class diagram is shown in the following figure.

Windows desktop theme class diagram
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46864744/article/details/112855579
Recommended