23种设计模式之开闭原则

开闭原则(Open Closed Principle)

在这里插入图片描述

基本介绍:

开闭原则是编程中最基础、最重要的设计原则

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

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

编程中遵循其他原则,以及使用设计模式的目的就是遵循开闭原则
代码示例
有这样一个绘图的类

/**
 * @create: 2021/9/26
 * @author: Tony Stark
 */
public class Ocp {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawCircle(new Circle());
        graphicEditor.drawRectangle(new Rectangle());
    }
}

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

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

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

/**
 * 基类
 */
class Shape {
    int m_type;
}
class Rectangle extends Shape {
    Rectangle() {
        super.m_type = 1;
    }
}
class Circle extends Shape {
    Circle() {
        super.m_type = 2;
    }
}

方式一的优缺点:

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

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

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

比如我们新增一个正方形的类

class Square extends Shape{
    Square(){
        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) {
            drawShape(s);
        }else if (s.m_type==3){
            drawShape(s);
        }
    }

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

    public void drawCircle(Shape r) {
        System.out.println("圆形");
    }
    public void drawSquare(Shape r) {
        System.out.println("正方形");
    }
}

调用

public class Ocp {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawCircle(new Circle());
        graphicEditor.drawRectangle(new Rectangle());
        graphicEditor.drawSquare(new Square());
    }
}

可以看出我们要改动非常繁琐 且需要修改已有的代码 违反了开闭原则

根据开闭原则进行改进

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

/**
 * @create: 2021/9/26
 * @author: Tony Stark
 */
public class Ocp {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Square());
    }
}

class GraphicEditor{
    public void drawShape(Shape s){
        s.draw(s);
    }
}

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

    /**
     * 绘制图形的方法
     * @param s
     */
    public abstract void draw(Shape s);
}

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

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

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

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

class Square extends Shape {
    Square(){
        super.m_type=3;
    }

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

此时 我们新扩展功能时只需要继承Shape基类即可

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值