Seven principles of design patterns dependent reversal learning principles

First, the Dependency Inversion principle

  Basic introduction :

      (1) layer module should not rely on the underlying module, which should depend both abstract ( dependent interface or abstract class, do not rely on specific subclass )

      (2) abstract should not rely on the details, the details should depend Abstract

      (3) Dependency Inversion (inversion) The central idea is oriented programming interface

      Purpose (4) using an interface or abstract class is to develop a good standard, and not related to any particular operation, the task to show the details of their implementation classes to complete.

      (5) Dependency Inversion principle is based on the design concept: the details of the relative variability, abstract things much more stable. Abstract-based architecture to build than detail-based architecture is much more stable. In Java, abstract refers to a multi-interface or abstract class, details refers to a specific category.

Second, case presentations 

 Test scenarios : Person receiving a message class

1. Basic code implementation

 Analysis : complete functionality Person receiving messages

  ① Advantages: easy, simple, easy to implement

  ② drawback: if the object we have obtained is micro-channel, SMS it, you need to add the corresponding class while also increasing the Person class method for receiving corresponding (currently only supports Person to receive Email) .

2, an improved version of code implementation

  Improvements : The Dependency Inversion principle, to introduce an abstract interface IReceiver, indicate the recipient, such Person classes and interfaces IReceiver occurrence dependence, where so than the benefits? Email, micro-letters, text messages belonging to the recipient (IReceiver) visible, each of which can implement an interface IReceiver, thus meet Dependency Inversion Principle, the core is oriented interfaces , dependent on the details of abstraction, rather than rely on the abstract details. Designed to be more flexible scalability.

 Third, the dependence of transfer mode

  When Dependency Inversion often transmitted dependence relation, the following three ways:

1, the interface transfer

2, passing the resulting

3, setter way of passing

 IV Summary

1, the lower layer module must try to abstract class interface, or both, or has a better stability program.

2, the declared type of the variable is possible abstract class or interface

3, follow the Richter substitution principle when inheritance  

Guess you like

Origin www.cnblogs.com/rmxd/p/12539269.html