1. Basic introduction
- The Open Closed Principle is the most fundamental and important design Principle in programming
- A software entity such as classes, modules, and functions should be open for extension but closed for modification. Build the framework with abstractions and extend the details with implementations
- When soft needs to change, try to do so by extending the behavior of software entities rather than by modifying existing code
- The purpose of following other principles in programming and using design patterns is to follow the open closed principle
2. Application cases
-
Case description Draw different figures according to the given different types
-
Regular writing
public class OpenClosed { public static void main(String[] args) { // use it to see what the problem is GraphicEditor graphicEditor = new GraphicEditor(); graphicEditor.drawShape(new Rectangle()); graphicEditor.drawShape(new Circle()); graphicEditor.drawShape(newTriangle()); }}// This is a class for drawing [user] class GraphicEditor { // Take Shape and draw different shapes according to type public void drawShape(Shape s) { if (s.m_type == 1) drawRectangle(s); else if (s.m_type == 2) drawCircle(s); else if (s.m_type == 3) drawTriangle(s); } // Draw a rectangle public void drawRectangle(Shape r) { System.out.println("Rectangle"); } // Draw a circle public void drawCircle(Shape r) { System.out.println("Circular"); } // Draw a triangle public void drawTriangle(Shape r) { System.out.println(triangle); }}//Shape, base class class Shape { int m_type; } class Rectangle extends Shape { Rectangle() { super.m_type = 1; }}class Circle extends Shape { Circle() { super.m_type = 2; }}// Add a triangle class Triangle extends Shape { Triangle() { super.m_type = 3; }}Copy the code
The above method is easy to understand, simple to operate, but violates the open closed principle, that is, open for extension, closed for modification. That is, when we need to add new functionality to a class, we should change the code as little or as little as possible. In the example above, when we need to add a new type of graph, there are many changes, and we will also draw the GraphicEditor class to add other types of methods
Improve the idea: create Shape class to make abstract class, and provide an abstract draw method, let subclasses to achieve, so that when the need for new graphics, just let the new graphics inherit Shape, and draw method can be achieved. In this way, the user’s code does not need to be modified to meet the open and close principle
-
Improved code
public class OpenClosed { public static void main(String[] args) { // use it to see what the problem is GraphicEditor graphicEditor = new GraphicEditor(); graphicEditor.drawShape(new Rectangle()); graphicEditor.drawShape(new Circle()); graphicEditor.drawShape(new Triangle()); graphicEditor.drawShape(newOtherGraphic()); }}// This is a class for drawing [user] class GraphicEditor { // Receive Shape and call draw public void drawShape(Shape s) { s.draw(); }}//Shape, base class abstract class Shape { int m_type; public abstract void draw(a);// Abstract methods } class Rectangle extends Shape { Rectangle() { super.m_type = 1; } @Override public void draw(a) { System.out.println("Rectangle"); }}class Circle extends Shape { Circle() { super.m_type = 2; } @Override public void draw(a) { System.out.println("Circular"); }}// Add a triangle class Triangle extends Shape { Triangle() { super.m_type = 3; } @Override public void draw(a) { System.out.println(triangle); }}// Add a graphic class OtherGraphic extends Shape { OtherGraphic() { super.m_type = 4; } @Override public void draw(a) { System.out.println("Other shapes"); }}Copy the code
3. Notes and details
- Open-closed principle (OCP) is the cornerstone of reusable design in object-oriented design. It is one of the most important principles in object-oriented design. Many other design principles are a means to implement open-closed principle
- In the open and closed principle, “open” refers to the function extension of a component is open and allowed to be extended. “Closed” in the open and closed principle means that the modification of the original code is closed, that is, the original code should not be modified