Seven Principles of Design Patterns series - the principle of opening and closing

The most important principle of the most basic: the other principles of practice, in order to follow the principle of opening and closing

① to open for extension (provider), closed for modification (consumer)

② When the software needs to change, as far as possible is achieved by extending, rather than modifying existing code to implement

③ other programming principle is to follow the principle of opening and closing

My understanding is sort of like oriented programming interface

For a classic painting graphics chestnuts:

. 1  public  class Ocp {
 2  
. 3      public  static  void main (String [] args) {
 . 4          // used to see the problems 
. 5          GraphicEditor graphicEditor = new new GraphicEditor ();
 . 6          graphicEditor.drawShape ( new new the Rectangle ());
 . 7          graphicEditor. the drawShape ( new new Circle ());
 . 8          graphicEditor.drawShape ( new new Triangle ());
 . 9      }
 10  
. 11  }
 12 is  
13 is  // class this is a drawing for [consumer] 
14  class{GraphicEditor
 15      // receiving Shape objects, according to type and then, to draw various graphics 
16      public  void the drawShape (Shape S) {
 . 17          IF (s.m_type ==. 1 )
 18 is              drawRectangle (S);
 . 19          the else  IF (s.m_type 2 == )
 20 is              the drawCircle (S);
 21 is          the else  IF (s.m_type ==. 3 )
 22 is              drawTriangle (S);
 23 is      }
 24  
25      // draw a rectangle 
26 is      public  void drawRectangle (the Shape R & lt) {
 27          System.out.println ( "draw a rectangle");
 28      }
 29  
30      // draw a circle 
31 is      public  void the drawCircle (the Shape R & lt) {
 32          System.out.println ( "draw a circle" );
 33      }
 34 is      
35      // draw a triangle 
36      public  void drawTriangle (the Shape R & lt) {
 37 [         System.out.println ( "drawing a triangle" );
 38 is      }
 39  }
 40  
41 is  // the Shape class, base class 
42 is  class the Shape {
 43 is      int m_type;
 44 is  }
 45  
46 is  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 }

If you add such a triangle, the original class will need to be modified.

Improved

 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 is  
14  // This class is a drawing for [consumer] 
15  class GraphicEditor {
 16      // receiving Shape object method calls the draw 
. 17      public  void the drawShape (Shape S) {
 18 is          s.draw ();
 . 19      }
 20 is  
21 is      
22 is  }
 23 is  
24  // the Shape class, base class 
25  abstract  class the Shape {
 26 is      int m_type;
 27      
28      public  abstract  void Draw (); // abstract method 
29  }
 30  
31 is  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 ( "drawing a triangle" );
 63      }
 64  }
 65  
66  // add a graphic 
67  class OtherGraphic the extends the Shape {
 68      OtherGraphic () {
 69          Super .m_type =. 4 ;
 70      }
 71 is  
72      @Override
 73 is      public  void draw () {
 74          // the TODO Auto-Generated Method Stub 
75          System.out.println ( "drawing other graphic" );
 76      }
 77 }

In fact, I still want to emphasize a word, this is actually oriented interface / abstract programming

In fact, I still want to emphasize a word, this is actually oriented interface / abstract programming

In fact, I still want to emphasize a word, this is actually oriented interface / abstract programming

 

Guess you like

Origin www.cnblogs.com/zyzblogs/p/11274432.html