Design Patterns series of seven principles - Dependency Inversion principle

Look at the concept

① high-level modules should not depend on low-level modules, both of which should rely on their abstract

② abstract should not rely on the details, the details should depend abstract

③ Dependency Inversion is the core idea: oriented programming interface

④ Dependency Inversion is based on the design concept: the details of the relative variability, abstract things much more stable. Abstract-based architecture to build a stable foundation than detail-built architecture . In java, abstract refers to the interface or is an abstract class, details of that specific category

using an interface or abstract class purpose is to develop a good standard , but does not involve any specific operation, to show the details of their implementation classes to complete.

 

Dependencies There are three delivery methods

① Interface

② construction method transfer

③setter delivery method

Notes and details

① lower layer module to try to use an abstract class or an interface, or both, so that better stability program

② declare type variables as much as possible is an abstract class or interface, so our variable references, and there is a buffer layer between the actual object, which will help the program expand and optimize

③ inheritance, the need to follow the principles in the replacement (next heading to explain)

Emphasize a word again: the core is dependent reversal of [oriented programming interface]

Look at some did not follow the Dependency Inversion principle of the code

. 1  // Miles Zhu 
2  public  class DependecyInversion {
 . 3  
. 4      // test 
. 5  public  static  void main (String [] args) {
 . 6          the Person Person = new new the Person ();
 . 7          person.receive ( new new In Email ());
 . 8      }
 . 9  
10  }
 . 11  
12 is  // first create a Email class, there are a method of directly printing word 
13 is  class Email {
 14      public String getInfo () {
 15          return "e-mail messages: hello, world";
 16      }
 17  }
 18 is  
. 19  
20 is  // then create a Person class, to send the message 
21 is  class Person {
 22 is      public  void the receive (In Email In Email) {
 23 is          System.out.println (email.getInfo ());
 24      }
 25 }

Interpretation: This code is to realize someone sending mail / SMS function. This code is very simple, Email create a first class, which has a corresponding method of transmitting information. Then create a Person class to invoke this method Emial class to send a message. But if this time, Person are not sending e-mail, but micro letter, QQ, SMS? This also requires an increase in the respective methods, etc. recive_wechat in Person. . . . Or to receive overloaded, but this design Person and too dependent on the classes. It is clear that in this way in the actual development is not feasible.

Solution: a design, and then send information to the appropriate class that implements this interface can, Person in need receive only depend on the interface can be. Specifically look at the code

. 1  public  class DependecyInversion {
 2  
. 3      public  static  void main (String [] args) {
 . 4          // client does not need to change 
. 5          the Person Person = new new the Person ();
 . 6          person.receive ( new new In Email ());
 . 7          
. 8          person.receive ( new new Weixin ());
 . 9      }
 10  
. 11  }
 12 is  
13 is  // definition of the interface 
14  interface the IReceiver {
 15      public String getInfo ();
 16  }
. 17  
18 is  class In Email the implements the IReceiver {
 . 19      public String getInfo () {
 20 is          return "e-mail message: Hello, World" ;
 21 is      }
 22 is  }
 23 is  
24  // increase the micro-channel 
25  class Weixin the implements the IReceiver {
 26 is      public String getInfo () {
 27          return "micro-channel message: Hello, OK" ;
 28      }
 29  }
 30  
31 is  // embodiment 2 
32  class the Person {
 33 is      //Here, we rely on the interface 
34 is      public  void the receive (the IReceiver Receiver) {
 35          System.out.println (receiver.getInfo ());
 36      }
 37 [ }

Emphasize a word again: the core is dependent reversal of [oriented programming interface]

Emphasize a word again: the core is dependent reversal of [oriented programming interface]

Emphasize a word again: the core is dependent reversal of [oriented programming interface]

Guess you like

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