I. Dip introduction (Dip -- Dependency inversion principle): 1. High-level modules should not depend on low-level modules. Both of them should depend on abstraction.
2. abstraction should not depend on details, but on abstraction. The high-level module containsProgramIt is these high-level modules that distinguish all their applications from others, the changes to the lower-level modules will directly affect the higher-level modules, thus forcing them to make changes in turn.
Ii. Example: Negative example:
Disadvantage: If the coupling is too tight, changing light will affect toggleswitch. Solution 1:
Abstract light and inherit the class from light.
Advantage: toggleswitch depends on the abstract class light and has higher stability. bulblight and tubelight inherit from light and can be extended according to the "open-closed" principle. As long as the light does not change, the changes between bulblight and tubelight will not affect toggleswitch. Disadvantage: it is very difficult to use toggleswitch to control a TV. TV cannot be inherited from light. Solution 2: Advantages: more general and more stable.
Iii. Advantages of dip: The dependency relationships created by traditional procedural programming are dependent on details, which is poor because the policies are affected by changes in details. The dependency inversion principle makes the details and policies depend on abstraction. The stability of abstraction determines the stability of the system.
ThuHeuristic rules: 1. No variable should hold a pointer or reference pointing to a specific class.
2. No class should be derived from a specific class (beginning with abstraction and coming from a specific class)
3. No method should overwrite the implemented methods in any of its base classes.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service