首页 > 技术文章 > 设计模式七大原则-开闭原则

Rabcheng 2020-08-15 17:10 原文

基本介绍:

开闭原则是变成中最基础、最重要的设计原则。

一个软件实体,如类、模块、函数应该对扩展开放(对提供方来说,可以增加新功能),对修改关闭(对使用方来说,之前使用的代码没有修改)。用抽象构建框架,用实现扩展细节。

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

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

 

应用案例:

画图形的案例

方式一:

 1 package cn.rabcheng.openclosedprinciple;
 2 
 3 /**
 4  * @auther cheng
 5  * @create 2020-08-15 16:26
 6  * 开闭原则
 7  */
 8 public class OpenClosedPrinciple1 {
 9 
10     public static void main(String[] args) {
11 
12         //使用看看存在的问题
13         GraphicEditor graphicEditor = new GraphicEditor();
14         graphicEditor.drawShape(new Rectangle());
15         graphicEditor.drawShape(new Circle());
16         graphicEditor.drawShape(new Triangle());
17     }
18 }
19 
20 
21 //这是用来绘制图的类
22 class GraphicEditor{
23     public void drawShape(Shape shape){
24         //接受shape对象,然后根据type,来绘制不同的图形
25         if(shape.m_type == 1){
26             drawRectangle(shape);
27         }else if(shape.m_type == 2){
28             drawCircle(shape);
29         }else if(shape.m_type == 3){
30             drawTriangle(shape);
31         }
32     }
33 
34     private void drawTriangle(Shape shape) {
35         System.out.println("画三角形");
36     }
37 
38     private void drawCircle(Shape shape) {
39         System.out.println("画圆形");
40     }
41 
42     private void drawRectangle(Shape shape) {
43         System.out.println("画矩形");
44     }
45 
46 
47 }
48 
49 class Shape{
50     public int m_type;
51 }
52 
53 class Rectangle extends Shape{
54     Rectangle(){
55         super.m_type = 1;
56     }
57 }
58 
59 class Circle extends Shape{
60     Circle(){
61         super.m_type = 2;
62     }
63 }
64 
65 class Triangle extends Shape{
66     Triangle(){
67         super.m_type = 3;
68     }
69 }
70 画矩形
71 画圆形
72 画三角形

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

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

比如我们需要增加一个图形种类三角形,我们需要做以上修改,修改的地方较多

 

改进的思路:

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

使用方的代码就不需要修改

 

方式二:

package cn.rabcheng.openclosedprinciple;

/**
 * @auther cheng
 * @create 2020-08-15 16:54
 * 开闭原则
 */
public class OpenClosedPrinciple2 {
    public static void main(String[] args) {
        GraphicEditor2 graphicEditor2 = new GraphicEditor2();
        graphicEditor2.drawShape(new Rectangle2());
        graphicEditor2.drawShape(new Circle2());
        graphicEditor2.drawShape(new Triangle2());
    }
}

abstract class Shape2{
    int m_type;
    //抽象方法
    public abstract void draw();
}

class Rectangle2 extends Shape2{
    Rectangle2(){
        super.m_type = 1;
    }
    @Override
    public void draw() {
        System.out.println("画矩形");
    }
}

class Circle2 extends Shape2{
    Circle2(){
        super.m_type = 2;
    }

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

class GraphicEditor2{
    public void drawShape(Shape2 shape2){
        shape2.draw();
    }
}

class Triangle2 extends Shape2{
    @Override
    public void draw() {
        System.out.println("画三角形");
    }
}
画矩形
画圆形
画三角形

 

推荐阅读