开闭原则
OpenClosedPrinciple
基本介绍
1. 开闭原则OpenClosedPrinciple是编程中最基础、最重要的设计原则 (ocp原则)
2. 一个软件的实体(eg:类),模块和函数应该对扩展开放(对提供方),对修改关闭(对使用方)用抽象构建框架,用实现扩展细节。
3. 当软件需求发生变化的时候,尽量通过扩展软件实体的行为实现变化,而不是通过修改已有的代码来实现变化
4. 编程中遵循其他原则,以及使用设计模式的目的就是遵循开闭原则
案例1
public class OpenClosedPrinciple1{
public static void main(String args[]){
GraphicEditor graphicEditor = new GraphicEditor();
graphicEditor.drawShape(new Rectangle());
graphicEditor.drawShape(new Circle());
graphicEditor.drawShape(new Triangle());
}
}
class GraphicEditor{
public void drawShape(Shape s)