1.1 基本介绍


1) 开闭原则(Open Closed Principle)是编程中最基础、最重要的设计原则

2) 一个软件实体如类,模块和函数应该对扩展开放(对提供方),对修改关闭(对使用 方)。用抽象构建框架,用实现扩展细节。

3) 当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已 有的代码来实现变化。

4) 编程中遵循其它原则,以及使用设计模式的目的就是遵循开闭原则。

1.2 看下面一段代码


看一个画图形的功能 。

类图设计,如下:
image.png

代码演示:

  1. package com.atguigu.principle.ocp;
  2. public class Ocp {
  3. public static void main(String[] args) {
  4. //使用看看存在的问题
  5. GraphicEditor graphicEditor = new GraphicEditor();
  6. graphicEditor.drawShape(new Rectangle());
  7. graphicEditor.drawShape(new Circle());
  8. graphicEditor.drawShape(new Triangle());
  9. }
  10. }
  11. //这是一个用于绘图的类 [使用方]
  12. class GraphicEditor {
  13. //接收Shape对象,然后根据type,来绘制不同的图形
  14. public void drawShape(Shape s) {
  15. if (s.m_type == 1)
  16. drawRectangle(s);
  17. else if (s.m_type == 2)
  18. drawCircle(s);
  19. else if (s.m_type == 3)
  20. drawTriangle(s);
  21. }
  22. //绘制矩形
  23. public void drawRectangle(Shape r) {
  24. System.out.println(" 绘制矩形 ");
  25. }
  26. //绘制圆形
  27. public void drawCircle(Shape r) {
  28. System.out.println(" 绘制圆形 ");
  29. }
  30. //绘制三角形
  31. public void drawTriangle(Shape r) {
  32. System.out.println(" 绘制三角形 ");
  33. }
  34. }
  35. //Shape类,基类
  36. class Shape {
  37. int m_type;
  38. }
  39. class Rectangle extends Shape {
  40. Rectangle() {
  41. super.m_type = 1;
  42. }
  43. }
  44. class Circle extends Shape {
  45. Circle() {
  46. super.m_type = 2;
  47. }
  48. }
  49. //新增画三角形
  50. class Triangle extends Shape {
  51. Triangle() {
  52. super.m_type = 3;
  53. }
  54. }

1.3 方式1的优缺点


1) 优点是比较好理解,简单易操作。

2) 缺点是违反了设计模式的ocp原则,即对扩展开放(提供方),对修改关闭(使用方)。 即当我们给类增加新功能的时候,尽量不修改代码,或者尽可能少修改代码.

3) 比如我们这时要新增加一个图形种类 三角形,我们需要做如下修改,修改的地方 较多


1.4 改进的思路分析


思路:把创建Shape类做成抽象类,并提供一个抽象的draw方法,让子类去实现即可, 这样我们有新的图形种类时,只需要让新的图形类继承Shape,并实现draw方法即可, 使用方的代码就不需要修 -> 满足了开闭原则

改进后的代码:

package com.atguigu.principle.ocp.improve;

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();
    }


}

//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(" 绘制其它图形 ");
    }
}