设计模式系列之七大原则之——开闭原则

最重要最基础的一个原则:其他的原则实际上都是为了遵循开闭原则

①对扩展开放(提供方),对修改关闭(使用方)

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

③编程中其他的原则都是为了遵循开闭原则

我的理解是有点像面向接口编程

举一个最经典的画图形的栗子:

 1 public class Ocp {
 2 
 3     public static void main(String[] args) {
 4         //使用看看存在的问题
 5         GraphicEditor graphicEditor = new GraphicEditor();
 6         graphicEditor.drawShape(new Rectangle());
 7         graphicEditor.drawShape(new Circle());
 8         graphicEditor.drawShape(new Triangle());
 9     }
10 
11 }
12 
13 //这是一个用于绘图的类 [使用方]
14 class GraphicEditor {
15     //接收Shape对象,然后根据type,来绘制不同的图形
16     public void drawShape(Shape s) {
17         if (s.m_type == 1)
18             drawRectangle(s);
19         else if (s.m_type == 2)
20             drawCircle(s);
21         else if (s.m_type == 3)
22             drawTriangle(s);
23     }
24 
25     //绘制矩形
26     public void drawRectangle(Shape r) {
27         System.out.println(" 绘制矩形 ");
28     }
29 
30     //绘制圆形
31     public void drawCircle(Shape r) {
32         System.out.println(" 绘制圆形 ");
33     }
34     
35     //绘制三角形
36     public void drawTriangle(Shape r) {
37         System.out.println(" 绘制三角形 ");
38     }
39 }
40 
41 //Shape类,基类
42 class Shape {
43     int m_type;
44 }
45 
46 class Rectangle extends Shape {
47     Rectangle() {
48         super.m_type = 1;
49     }
50 }
51 
52 class Circle extends Shape {
53     Circle() {
54         super.m_type = 2;
55     }
56 }
57 
58 //新增画三角形
59 class Triangle extends Shape {
60     Triangle() {
61         super.m_type = 3;
62     }
63 }

这样如果新增一个三角形,原来的类都需要进行修改。

改善后

 1 public class Ocp {
 2 
 3     public static void main(String[] args) {
 4         //使用看看存在的问题
 5         GraphicEditor graphicEditor = new GraphicEditor();
 6         graphicEditor.drawShape(new Rectangle());
 7         graphicEditor.drawShape(new Circle());
 8         graphicEditor.drawShape(new Triangle());
 9         graphicEditor.drawShape(new OtherGraphic());
10     }
11 
12 }
13 
14 //这是一个用于绘图的类 [使用方]
15 class GraphicEditor {
16     //接收Shape对象,调用draw方法
17     public void drawShape(Shape s) {
18         s.draw();
19     }
20 
21     
22 }
23 
24 //Shape类,基类
25 abstract class Shape {
26     int m_type;
27     
28     public abstract void draw();//抽象方法
29 }
30 
31 class Rectangle extends Shape {
32     Rectangle() {
33         super.m_type = 1;
34     }
35 
36     @Override
37     public void draw() {
38         // TODO Auto-generated method stub
39         System.out.println(" 绘制矩形 ");
40     }
41 }
42 
43 class Circle extends Shape {
44     Circle() {
45         super.m_type = 2;
46     }
47     @Override
48     public void draw() {
49         // TODO Auto-generated method stub
50         System.out.println(" 绘制圆形 ");
51     }
52 }
53 
54 //新增画三角形
55 class Triangle extends Shape {
56     Triangle() {
57         super.m_type = 3;
58     }
59     @Override
60     public void draw() {
61         // TODO Auto-generated method stub
62         System.out.println(" 绘制三角形 ");
63     }
64 }
65 
66 //新增一个图形
67 class OtherGraphic extends Shape {
68     OtherGraphic() {
69         super.m_type = 4;
70     }
71 
72     @Override
73     public void draw() {
74         // TODO Auto-generated method stub
75         System.out.println(" 绘制其它图形 ");
76     }
77 }

其实我还是想强调一句话,这个其实就是面向接口/抽象编程

其实我还是想强调一句话,这个其实就是面向接口/抽象编程

其实我还是想强调一句话,这个其实就是面向接口/抽象编程

猜你喜欢

转载自www.cnblogs.com/zyzblogs/p/11274432.html