设计模式之开闭原则

记录一下设计模式开闭原则的学习:

1,概念

开闭原则,在面向对象编程领域中,规定“软件中的对象,模块,函数等等)应该对于扩展是开放的,但是对于修改是封闭的”,这意味着一个实体是允许在不改变它的源代码的前提下变更它的行为,通常是定义一个抽象类,在其中定义抽象方法,定义子类去实现其中的抽象方法,这样在不修改源代码的情况下,拓展新的功能。

2,案例演示

1,不采用开闭原则进行设计

//main方法
public class Ocp {
    public static void main(String[] args) {
        GraphicEditor ge=new GraphicEditor();
        ge.drawRectangle(new Rectangle());
        ge.drawCircle(new Circle());
        ge.drawTriangle(new Triangle());
    }
}
//绘制图形的方法函数
public class GraphicEditor {
    public void drawShape (shape s){
           if(s.my_type == 1) drawRectangle(s);
           else if(s.my_type ==2) drawCircle(s);
           else if(s.my_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基类与拓展类
public class shape {
    int my_type;
}
public class Rectangle extends shape{
    Rectangle(){
        super.my_type=1;
    }
}
public class Circle extends shape{
    Circle(){super.my_type =2;}
}
public class Triangle extends shape{  //需要增加的类
       Triangle(){
           super.my_type=3;
       }
}

运行结果如下:

"C:\Program Files (x86)\Java\jdk1.8.0_151\bin\java.exe" "-javaagent:C:\Program 
绘制矩形
绘制圆形
绘制三角形

在此方法下,若要增加新的绘图功能则需要修改绘制图形的方法函数GraphicEditor里面的代码,均以注释标出。

2,采用开闭原则进行设计

       将shape类改进为抽象类,并提供一个抽象方法draw,让子类去实现即可,这样有新图形种类时,只需要让新的图形类继承shape,并实现draw方法即可。

 

public class opcdemo {
    public static void main(String[] args) {
        GraphicEditor ge=new GraphicEditor();
        ge.drawShape(new Rectangle());
        ge.drawShape(new Circle());
        ge.drawShape(new Triangel());
    }
}
class GraphicEditor{
    public void drawShape(Shape s){s.draw();}
}

    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 Triangel extends Shape{     //仅需增加子类
    Triangel(){
        super.m_type=3;
    }
    @Override
    public void draw() {
        System.out.println("绘制三角形");
    }
}

运行结果:

绘制矩形
绘制圆形
绘制三角形


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
开闭原则是指一个软件实体(类、模块、函数等)应该对扩展开放,对修改关闭。在23种设计模式,违反开闭原则的模式包括以下几种: 1. 简单工厂模式:每次添加新产品都需要修改工厂类的代码,违反了开闭原则。 2. 工厂方法模式:每次添加新产品都需要创建一个新的工厂类,违反了开闭原则。 3. 抽象工厂模式:每次添加新产品族都需要修改抽象工厂类的代码,违反了开闭原则。 4. 建造者模式:每次添加新部件都需要修改建造者类的代码,违反了开闭原则。 5. 原型模式:每次添加新原型都需要修改原型管理器类的代码,违反了开闭原则。 6. 适配器模式:每次添加新的接口都需要修改适配器类的代码,违反了开闭原则。 7. 桥接模式:每次添加新的实现都需要修改抽象部分的代码,违反了开闭原则。 8. 装饰器模式:每次添加新的装饰者都需要修改被装饰者的代码,违反了开闭原则。 9. 代理模式:每次添加新的代理都需要修改代理类的代码,违反了开闭原则。 10. 外观模式:每次添加新的子系统都需要修改外观类的代码,违反了开闭原则。 11. 组合模式:每次添加新的组合都需要修改组合类的代码,违反了开闭原则。 12. 模板方法模式:每次添加新的实现都需要修改模板方法的代码,违反了开闭原则。 这些设计模式虽然违反了开闭原则,但是它们都为代码提供了更好的可扩展性和可维护性,具体情况需要根据实际情况进行权衡和选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值