设计模式七大原则之开放封闭原则

本文探讨了开闭原则在软件设计中的应用,通过对比修改前后的绘图类代码,展示了如何通过接口和抽象方法实现对扩展的开放和对修改的封闭,以适应新增图形类型的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 基本介绍
    1)开闭原则(Open Closed Principle)是编程中最基础、最重要的设计原则
    2)一个软件实体如类,模块和函数应该对扩展开放(对提供方),对修改关闭(对使用方)。用抽象构建框架,用实现扩展细节。
    3)当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化。
    4)编程中遵循其它原则,以及使用设计模式的目的就是遵循开闭原则。

  2. 先来一段代码,大家看看有什么问题

    package cn.chmcyz.openclose;
    
    /**
     * @author 谌涣谋
     * @date 2020/5/16 - 17:24
     */
    public class OpenCloseDemp {
    
    
        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,来绘制不同的图形
        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;
        }
    }
    
    

上面代码存在的问题:
1)违反了设计模式的 ocp 原则,即对扩展开放(提供方),对修改关闭(使用方)。即当我们给类增加新功能的时候,尽量不修改代码,或者尽可能少修改代码.
2)比如我们这时要新增加一个图形种类 三角形,我们需要做修改,修改的地方较多

解决方案
把创建 Shape 类做成接口,并提供一个抽象的 draw 方法,让具体的类去实现即可,这样我们有新的图形种类时,只需要让新的图形类,代码如下

package cn.chmcyz.openclose.improve;

/**
 * @author 谌涣谋
 * @date 2020/5/16 - 17:27
 */
public class OpenCloseImprove {
    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,来绘制不同的图形
    public void drawShape(Shape s) {
       s.draw();

    }


}

//Shape 接口
interface Shape {
    void draw();
}


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


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


//新增画三角形
class Triangle implements Shape {
    @Override
    public void draw() {
        System.out.println("绘制三角形");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值