[企业框架设计模式]设计模式七大原则之开闭原则

开闭原则

目录

开闭原则

基本介绍

应用实例

例1优缺点

 改进思路


基本介绍

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

应用实例

例1

package 开闭原则01;

public class example {
    public static void main(String[] args) {
        GraphicEditor graphicEditor=new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
    }
}
//这是一个用于绘图的类,即使用方
class GraphicEditor{
    //接收Shape对象,然后根据type,来绘制不同的图形
    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(" 绘制圆形 ");
    }
}
//创建基类(符合里氏替换原则)
class Shape{
    int m_type;
}
//矩形继承基类
class Rectangle extends Shape{
    Rectangle(){
        super.m_type=1;
    }
}
//圆形继承基类
class Circle extends Shape{
    Circle(){
        super.m_type=2;
    }
}

 

例1优缺点

  1. 优点是比较好理解,简单易操作
  2. 缺点是违反了设计模式的ocp原则,即对扩展开放(提供方),对修改关闭(使用方)。即当我们给类增加新功能的时候,尽量不修改代码,或者尽可能少修改代码
  3. 比如我们要新增一个图形种类:三角形,我们需要做如下(子例)修改,修改的地方较多

 子例:

package 开闭原则01;

public class example {
    public static void main(String[] args) {
        GraphicEditor graphicEditor=new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawTriangle(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(" 新增绘制三角形 ");
    }
}
//创建基类(符合里氏替换原则)
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;
    }
}

 改进思路

把创建Shape类做成抽象类,并提供一个抽象的draw方法,让子类去实现即可,这样我们有新的图形种类时,只需要让新的图形类继承Shape,并实现draw方法即可。

使用方的代码不需修改--------满足了开闭原则

 例2

package 开闭原则02;

public class example {
    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 others());      //为“新增其他图形”类开辟内存空间
    }
}
//创建一个绘制图像的类,用于调用其他绘制图形的类方法
class GraphicEditor{
    public void drawShape(Shape s){
        s.draw();
    }
}
//创建抽象基类
abstract class Shape{
    public abstract void draw();
}

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

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

class Triangle extends Shape{
    @Override
    public void draw() {
        System.out.println(" 绘制三角形 ");
    }
}
//新增其他图形
class others extends Shape{
    @Override
    public void draw() {
        System.out.println(" 绘制其他图形 ");
    }
}

上一篇:吴恩达深度学习-第二周编程作业


下一篇:23种设计模式之开闭原则