2.0 - Design Patterns seven principles of software architecture design - the principle of opening and closing

Closing principle (Open-Closed Principle, OCP) refers to a software entity, such as classes, modules and functions should be open for extension but closed for modification. The so-called open and close, it is also a principle to extend and modify the behavior of the two. Emphasis is to build abstract frame, with details to achieve expansion. The system can improve software reusability and maintainability. Opening and closing the principle of object-oriented design is the most basic design principles. It guides how we build a stable and flexible system, for example: We updated version, I try not to modify the source code, but you can add new features.

 

For example: In real life, for the principle of opening and closing is also reflected. For example, many Internet companies are the implementation of flexible production time information, the provisions of 8 hours a day. His point is that this provision for working eight hours a day is close, but when you come, when to go is open. Early to leave early, late and leaving late.

 

Implement the principle of opening and closing the core idea is to abstract-oriented programming, then we look at a piece of code:

/**
 * @author madongyu
 * @projectName design-pattern-ma
 * @description: 课程
 * @date 2019/6/1717:57
 */
public interface Curriculum {
    Integer getId();
    String getName();
    Double getPrice();

}
/**
 * @author madongyu
 * @projectName design-pattern-ma
 * @description: TODO
 * @date 2019/6/1717:59
 */
public class JavaCurriculum implements Curriculum {

    private Integer Id;
    private String name;
    private Double price;


    public JavaCurriculum(Integer id, String name, Double price) {
        Id = id;
        this.name = name;
        this.price = price;
    }

    @Override
    public Integer getId() {
        return this.Id;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public Double getPrice() {
        return this.price;
    }
}

Now we give Java programs do activities, price concessions. If you modify getPrice JavaCurriculum in () method, certain risks would exist that may affect the result of the call elsewhere. How do we advance before without modifying the original code to achieve price this function? Now, we write a preferential logic class treatment, JavaCurriculumCourse categories:

/**
 * @author madongyu
 * @projectName design-pattern-ma
 * @description: TODO
 * @date 2019/6/1718:03
 */
public class JavaCurriculumCourse extends JavaCurriculum  {

    public JavaCurriculumCourse(Integer id, String name, Double price) {
        super(id, name, price);
    }
    /**
     * 新加的方法
     * @return
     */
    public Double getOriginPrice(){
        return super.getPrice();
    }
    @Override
    public Double getPrice(){
        return super.getPrice() * 0.5;
    }
}

FIG class configuration as follows:

Code Address: https://github.com/madongyu555666/design-pattern-ma/tree/master/seven-principles

Guess you like

Origin blog.csdn.net/madongyu1259892936/article/details/92798042