Synthesis of seven principles of design reuse pattern principle

      It is synthesized by the combination of the principle of multiplexing / multiplexing principles of the polymerization, to make use of a combination of an object instead of inheritance to achieve multiplexing


      Synthesis multiplexed in principle is a new object is in use by the association (relationships including Combined and polymerization) some existing objects, to become part of the new object; new object call by delegating to an existing object reaches multiplexing method the purpose of the function with. Briefly: To try to use combinations of multiplexing / polymeric relationship (relationship), less inheritance.

       In object-oriented design, two methods can reuse existing design and implemented in different environments, i.e. by a combination relationship between the polymerization or by inheritance , you should first consider the combination / polymerizable, composition / polymerization can make the system more flexibility, reduce the coupling between class and class, a relatively small impact on the change in one class other classes caused; secondly before considering inheritance, when using inheritance, the need for strict compliance with Richter substitution principle, the effective use of inheritance would help understanding of the problem, reduce complexity, and the abuse of inheritance but will increase the difficulty and complexity of building and maintaining the system, and therefore require careful use inheritance to reuse.

      The main problem to be multiplexed by the multiplexing inheritance inheritance that will destroy encapsulation system, implementation details that inherits the base class will subclass exposed to, since the internal details subclass of the base class is typically visible, Therefore, this multiplex, also known as "white box" multiplexing, if the base class is changed, then the implementation subclass also had to change, inherited from the base class comes to realize is static, change can not happen at runtime, There is not enough flexibility, and inheritance can only be used in limited circumstances.

      Here we give an example to understand the synthesis of multiplexing principles:

. 1      public  class A
 2      {
 . 3          public  void Method1 ()
 . 4          {
 . 5              Console.WriteLine ( " I method " );
 . 6          }
 . 7          public  void Method2 ()
 . 8          {
 . 9              Console.WriteLine ( " I Method II " );
 10          }
 . 11      }
 12 is  
13 is      public  class B: A
 14      {
 15  
16      }

 

         There are two methods of class A, class B just need to call these two methods, we might think of the direct successor to the first, so "more with less", but as the business development, function more and more complex, the need to increase class A other methods, such as method3, no connection with the class B, coupling will increase, combined with the core is multiplexed with the associated principles, we can rely on, polymerization association method synthesis, reduce coupling, and to improve maintainability reduce maintenance costs.

. 1      public  class A
 2      {
 . 3          public  void Method1 ()
 . 4          {
 . 5              Console.WriteLine ( " I method " );
 . 6          }
 . 7          public  void Method2 ()
 . 8          {
 . 9              Console.WriteLine ( " I Method II " );
 10          }
 11      }
 12 is  
13 is      // depend 
14      public  class B
 15      {
 16         public  void Mthod1 (A A)
 . 17          {
 18 is              (Console.WriteLine " call Method A " );
 19          }
 20      }
 21 is  
22 is      // polymerizable 
23 is      public  class C
 24      {
 25          Private A A;
 26 is  
27          public  void SetA (A Al)
 28          {
 29              A = Al;
 30          }
 31 is      }
 32   
33 is      // synthesis 
34     public class D
35     {
36         public A a = new A();
37 
38     }

 

Guess you like

Origin www.cnblogs.com/yangda/p/11954137.html