Design Principle - Opening and Closing Principle

Open-close principle: expand new categories rather than modify old ones

meaning

The open-closed principle ( Open-Closed Principle, OCP) means that a software entity such as a class, module, and function should be open for extension and closed for modification.

The so-called opening and closing is also a principle for the two behaviors of expansion and modification. The emphasis is on using abstraction to build the framework and using implementation to extend the details . The opening and closing principle is the most basic design principle in object-oriented design. It guides us on how to build a stable and flexible system. For example, when the program needs to be expanded, the original code cannot be modified to achieve a hot-swappable effect. In short, it is to make the program scalable and easy to maintain and upgrade.

main idea

  • Programming towards abstraction

advantage

  • Improve code reusability : After the software is completed, we can still expand the software and add new functions, which is very flexible. Therefore, this software system can continuously add new components to meet changing needs.
  • Improve the maintainability of software : Since the components of the existing software system, especially its abstract bottom layer, will not be modified, we do not need to worry about the stability of the original components in the software system, which makes the changing software system more stable. Certain stability and continuity.
    For example: a change in one module will have an impact on other modules. In particular, a change in a low-level module will inevitably cause changes in high-level modules. Therefore, changes are completed through expansion.

Method to realize

The key to realizing the Open-Closed Principle lies in abstraction . Abstract all possible behaviors of the system/software into an abstract bottom layer. This abstract bottom layer specifies the characteristics of all methods that must be provided by the specific implementation.
As the abstraction layer of system design, it is necessary to foresee all possible extensions, so that in any expansion case, the abstract bottom layer of the system does not need to be modified; at the same time, because one or more new concrete implementations can be derived from the abstract bottom layer, the system can be changed behavior, so the system design is open to extension.
Abstraction is a general description of a set of things without specific implementation, which means that it can have many possibilities and can change as needs change. Therefore, through interfaces or abstract classes, a set of potentially changing behaviors can be constrained and open to extensions, which contains six levels of meaning:

  1. The opening and closing principle is the most basic and important design principle in programming
  2. Use abstraction to build the framework, and use implementation to extend details . A software entity class, module, or function should be open for extension (for the provider) and closed for modification (for the user).
  3. Try to keep the abstraction layer stable and do not modify it once it is determined. When the software needs to change, try to achieve the change by extending the behavior of the software entity rather than modifying the existing code.
  4. Constraint diffusion through interfaces or abstract classes limits the boundaries of extensions and does not allow public methods that do not exist in interfaces or abstract classes.
  5. Following other principles in programming, and the purpose of using design patterns is to follow the opening and closing principle
  6. Try to use interfaces or abstract classes for parameter types and reference objects instead of implementation classes. This is mainly a requirement for realizing the Liskov substitution principle.

question

During the life cycle of software, when the original code of the software needs to be modified due to changes, upgrades, maintenance, etc., errors may be introduced into the old code, or we may have to reconstruct the entire function and require the original code. There is code that has been retested.

solution

When the software needs to change, try to achieve the change by extending the behavior of the software entity rather than by modifying the existing code.

Case

Taking drawing graphics as an example, the opening and closing principle is not used: different methods will be added to the Sharp class to implement different drawing methods.

Do not use the open-close principle

Do not use the open-close principle

Use the open-closed principle

Use the open-closed principle

Abstract a graphics class

public abstract class Sharp {
    
    
  public abstract void draw();
}

Let rectangles and circles implement graphics classes

public class Rectangle extends Sharp {
    
    
  @Override
  public void draw() {
    
    
    System.out.println("绘制矩形");
  }
}
public class Rotundity extends Sharp {
    
    
  @Override
  public void draw() {
    
    
    System.out.println("绘制圆形");
  }
}

Main

public class Drawing {
    
    
  // 使用基类去调用(多态)
  public void drawSharp(Sharp sharp) {
    
    
    sharp.draw();
  }

  public static void main(String[] args) {
    
    
    Drawing drawing = new Drawing();
    drawing.drawSharp(new Rectangle());
    drawing.drawSharp(new Rotundity());
  }
}

If you want to draw a triangle, just continue to extend the new class without modifying the original class, that is, comply with the opening and closing principle.

Guess you like

Origin blog.csdn.net/qq_42700109/article/details/132861097
Recommended