The open-closed Principle (OCP) states that a software entity (such as classes, modules, and functions) should be Open for extension and Closed for modification. The so-called open and close, it is the extension and modification of the two behavior of a principle. It emphasizes that building a framework with abstraction and extending details with implementation can improve the reusability and maintainability of software systems. Open and close principle is the most basic design principle in object-oriented design, it guides us how to establish stable and flexible system. For example, for version updates, we do not modify the source code as much as possible, but we can add new features. The open and close principle is also reflected in real life. For example, many Internet companies have flexible schedules that limit work to eight hours a day. That is to say, the eight-hour workday is closed, but when you come and when you leave is open. Early come and early go, late come and late go. The core of the open close principle is abstraction oriented programming, so let’s look at some code. Take golpo College’s course system as an example. First, create a course interface, ICourse:

public interface ICourse {
    Integer getId(a);
    String getName(a);
    Double getPrice(a);
}
Copy the code

This course includes one class that includes Java Architecture, big Data, artificial intelligence, front end, and software testing.

public class JavaCourse implements ICourse{
    private Integer Id;
    private String name;
    private Double price;
    public JavaCourse(Integer id, String name, Double price) {
        this.Id = id;
        this.name = name;
        this.price = price;
    }
    public Integer getId(a) {
        return this.Id;
    }
    public String getName(a) {
        return this.name;
    }
    public Double getPrice(a) {
        return this.price; }}Copy the code

Now we are going to do an activity for the Java Architecture course at a discounted price. If you change the getPrice() method in JavaCourse, you run the risk of affecting the call results elsewhere. How do we implement this feature without modifying the original code? Now, let’s write another JavaDiscountCourse class that handles discount logic (think about why we call it JavaDiscountCourse instead of DiscountCourse) :


public class JavaDiscountCourse extends JavaCourse {
    public JavaDiscountCourse(Integer id, String name, Double price) {
        super(id, name, price);
    }
    public Double getOriginPrice(a){
        return super.getPrice();
    }
    public Double getPrice(a){
        return super.getPrice() * 0.61; }}Copy the code

To recap, take a quick look at the class structure diagram, as shown below.

This article is “Tom play structure” original, reproduced please indicate the source. Technology is to share, I share my happiness! If this article is helpful to you, welcome to follow and like; If you have any suggestions can also leave a comment or private letter, your support is my motivation to adhere to the creation. Pay attention to “Tom bomb architecture” for more technical dry goods!

Other Design Principles

Dependence Inversion Principle (DIP)

Simple Responsibility Pinciple (SRP)

Interface Segregation Principle (ISP)

Law of Demeter LoD

Liskov Substitution Principle (LSP)

Composite/Aggregate Reuse Principle, CARP