1、基础介绍

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

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

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

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

2、应用实例

下面是一个画图形的功能。

类图设计,如下:
设计模式-七大原则-开闭原则

代码演示:

package com.wenze.principle.ocp;

/**
 * 开闭原则演示
 *
 * @author wenze
 * @version 1.0
 * @Date 2023-10-16 15:00:05
 * @since 1.0
 */
public class Ocp {
    public static void main(String[] args) {
        // 使用看看存在的问题
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
    }
}

/**
 * 这是一个用于绘图的类
 */
class GraphicEditor {
    /**
     * 接收 Shape 对象,然后根据 type,来绘制不同的图形
     * @param s Shape 对象
     */
    public void drawShape(Shape s) {
        if (s.m_type == 1) {
            drawRectangle(s);
        } else if (s.m_type == 2) {
            drawCircle(s);
        }
    }

    public void drawRectangle(Shape r) {
        System.out.println("绘制矩形");
    }

    public void drawCircle(Shape r) {
        System.out.println("绘制圆形");
    }
}

/**
 * Shape 基类
 */
class Shape {
    int m_type;
}

class Rectangle extends Shape {
    Rectangle() {
        super.m_type = 1;
    }
}

class Circle extends Shape {
    Circle() {
        super.m_type = 2;
    }
}

3、方式 1 的优缺点

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

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

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

4)代码演示

package com.wenze.principle.ocp;

/**
 * 开闭原则演示
 *
 * @author wenze
 * @version 1.0
 * @Date 2023-10-16 15:00:05
 * @since 1.0
 */
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());
    }
}

/**
 * 这是一个用于绘图的类
 */
class GraphicEditor {
    /**
     * 接收 Shape 对象,然后根据 type,来绘制不同的图形
     * @param s Shape 对象
     */
    public void drawShape(Shape s) {
        if (s.m_type == 1) {
            drawRectangle(s);
        } else if (s.m_type == 2) {
            drawCircle(s);
        } 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("绘制三角形");
    }
}

/**
 * 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;
    }
}

4、方式 1 的改进的思路分析

改进的思路分析

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

5、方式 2 来解决

1)方式 2 的设计方案:定义一个 Shape 抽象类。

2)代码示例:

package com.wenze.principle.ocp.improve;

/**
 * 开闭原则-改进-演示
 *
 * @author wenze
 * @version 1.0
 * @Date 2023-10-16 16:05:54
 * @since 1.0
 */
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 对象,然后根据 type,来绘制不同的图形
     * @param s Shape 对象
     */
    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() {
        System.out.println("绘制矩形");
    }
}

class Circle extends Shape {
    Circle() {
        super.m_type = 2;
    }

    @Override
    public void draw() {
        System.out.println("绘制圆形");
    }
}

// 新增画三角形
class Triangle extends Shape {
    Triangle() {
        super.m_type = 3;
    }

    @Override
    public void draw() {
        System.out.println("绘制三角形");
    }
}

// 新增其他的图形
class OtherGraphic extends Shape {
    OtherGraphic() {
        super.m_type = 4;
    }

    @Override
    public void draw() {
        System.out.println("绘制其他图形");
    }
}

3)从方式 2 看,代码满足了 OCP 原则