Open Closed Principle is the most basic design principle in the Java world. It guides us how to build a stable and flexible system.
definition:
A software entity such as classes, modules, and functions should be open to extensions and closed to modifications.
Software entities like classes,modules and functions should be open for extension but closed for modifications.
The meaning of the opening and closing principle is that a software entity should achieve changes through extensions, rather than by modifying existing code.
The software entity includes the following parts:
The principle of opening and closing is a principle for the constraints on current development design formulated for future things of the software entity.
Note: The principle of opening and closing is open to extensions and closing modifications does not mean that no modifications are made. Changes to low-level modules must be coupled with high-level modules, otherwise it will be an isolated and meaningless code snippet.
Types of change:
The basic path of a project should be as follows: project development, reconstruction, testing, production, operation and maintenance. The reconstruction can modify the original design and code, and the operation and maintenance can minimize the modification of the original code, maintain the purity of historical code, and improve the stability of the system.
Importance of the principle of opening and closing:
1. The impact of the opening and closing principle on tests. The opening and closing principle is to keep the original test code still running normally. We only need to test the extended code.
2. The principle of opening and closing can improve reusability. In object-oriented design, all logic is composed of atomic logic, rather than implementing a business logic independently in a class. Only in this way can the code be reused. The smaller the granularity, the greater the possibility of being reused.
3. The opening and closing principle can improve the requirements of maintainability object-oriented development.
How to use the opening and closing principle:
1. Abstract constraints first, extend the extension through interface or abstract class constraints, and do not allow public methods that do not exist in interface or abstract class;
Second, try to use interfaces or abstract classes for parameter types and reference objects, rather than implementation classes;
Third, try to remain stable as much as possible, and once determined, modifications are not allowed.
2. Metadata control module behavior metadata is the data used to describe the environment and data. In layman's terms, it is configuration parameters. Parameters can be obtained from files or from databases.
Spring containers are a typical example of the behavior of metadata control modules, and the ultimate is Inversion of Control
3. Formulating a project charter. In a team, it is very important to establish a project charter because the charter specifies agreements that all personnel must abide by. For projects, agreements are better than configuration.
4. The packaging changes have two meanings:
First, encapsulate the same changes into an interface or abstract class;
Second, encapsulate different changes into different interfaces or abstract classes, and there should not be two different changes in the same interface or abstract class.
example
Here is an example, first of all, a bad example:
class GraphicEditor { public void drawShape(Shape s) { if (s.m_type==1) drawRectangle(s); else if (s.m_type==2) drawCircle(s); } public void drawCircle(Circle r) {....} public void drawRectangle(Rectangle r) {....} } class Shape { int m_type; } class Rectangle extends Shape { Rectangle() { super.m_type=1; } } class Circle extends Shape { Circle() { super.m_type=2; } }When we want to expand a shape, we need to understand the GraphicEditor class first, then add a new type in drawShape, and then add a function. Here is the improved code:
class GraphicEditor { public void drawShape(Shape s) { s.draw(); } } class Shape { abstract void draw(); } class Rectangle extends Shape { public void draw() { // draw the rectangle } }
There is no need to understand the drawing logic, put the specific implementation into a subclass.
Summarize:
1. Complying with the opening and closing principles can improve the scalability and maintenance of software.
2. Most design patterns and design principles are about realizing the opening and closing principle.