英文名:Open Closed Principle,OCP
定义:一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。
开闭原则是编程中最基础、最重要的设计原则。一个软件实体如类,模块和函数应该对扩展开放(对提供方),对修改关闭(对使用方)。用抽象构建框架,用实现扩展细节。当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化。编程中遵循其它原则,以及使用设计模式的目的就是遵循开闭原则。为什么使用开闭原则 :在程序的生命周期内,因为变化,升级和维护等原因需要对程序原有的代码进行修改时,可能会给代码引入错误,增加项目开发测试的复杂度,也可能会使我们不得不对整个功能进行重构,而且还要对原有的代码进行测试。
问题由来:在软件的生命周期内,因为变化、升级和维护等原因需要对软件原有代码进行修改时,可能会给旧代码中引入错误,也可能会使我们不得不对整个功能进行重构,并且需要原有代码经过重新测试。
解决方案:当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化。
【例1】绘制图形
package com.lilair.principle.ocp; //Shape类,基类 class Shape {int m_type;} class Rectangle extends Shape { Rectangle() {super.m_type = 1;} } class Circle extends Shape { Circle() {super.m_type = 2;} } //新增画三角形 class Triangle extends Shape { Triangle() {super.m_type = 3} } //这是一个用于绘图的类 [使用方] class GraphicEditor { //接收Shape对象,然后根据type,来绘制不同的图形 public void drawShape(Shape s) { if (s.m_type == 1) drawRectangle(s); else if (s.m_type == 2) drawCircle(s); //需要对GraphicEditor类新增else if 语句,违背开闭原则 else if (s.m_type == 3) drawTriangle(s); } //绘制矩形 public void drawRectangle(Shape r) {System.out.println(" 绘制矩形 ");} //绘制圆形 public void drawCircle(Shape r) {System.out.println(" 绘制圆形 ");} //绘制三角形 public void drawTriangle(Shape r) {System.out.println(" 绘制三角形 ");} } public class Ocp { public static void main(String[] args) { //使用看看存在的问题 GraphicEditor graphicEditor = new GraphicEditor(); graphicEditor.drawShape(new Rectangle()); graphicEditor.drawShape(new Circle()); graphicEditor.drawShape(new Triangle()); } }优点是比较好理解,简单易操作。 缺点是违反了设计模式的ocp原则,即 对扩展开放( 提供方) ,对修改关闭( 使用方)。即当我们给类增加新功能的时候,尽量不修改代码,或者尽可能少修改代码.
使用开闭原则解决问题, 改进的思路分析 思 路 :把创建Shape类做成抽象类,并提供一个抽象的draw方法,让子类去实现即可,这样我们有新的图形种类时,只需要让新的图形类继承Shape,并实现draw方法即可,使用方的代码就不需要修改 -> 满足了开闭原则
package com.atguigu.principle.ocp.improve; //Shape类,基类 abstract class Shape { int m_type; public abstract void draw();//抽象方法 } class Rectangle extends Shape { Rectangle() { super.m_type = 1; } @Override public void draw() { // TODO Auto-generated method stub System.out.println(" 绘制矩形 "); } } class Circle extends Shape { Circle() { super.m_type = 2; } @Override public void draw() { // TODO Auto-generated method stub System.out.println(" 绘制圆形 "); } } //新增画三角形 class Triangle extends Shape { Triangle() { super.m_type = 3; } @Override public void draw() { // TODO Auto-generated method stub System.out.println(" 绘制三角形 "); } } //新增一个图形 class OtherGraphic extends Shape { OtherGraphic() { super.m_type = 4; } @Override public void draw() { // TODO Auto-generated method stub System.out.println(" 绘制其它图形 "); } } public class Ocp { public static void main(String[] args) { //使用看看存在的问题 GraphicEditor graphicEditor = new GraphicEditor(); graphicEditor.drawShape(new Rectangle()); graphicEditor.drawShape(new Circle()); graphicEditor.drawShape(new Triangle()); graphicEditor.drawShape(new OtherGraphic()); } } //这是一个用于绘图的类 [使用方] class GraphicEditor { //接收Shape对象,调用draw方法 public void drawShape(Shape s) { s.draw(); } }