/** * The combination of the dimit rule (high cohesion principle) and the dependency inversion principle (image extraction layer application) * (This idea runs through the entire design model) * From <Java and mode>. *CodeAnd Annotations: if it is not cold * Note: Zhang sanli 4 is a friend relationship, while Li siwang 5 is a friend relationship. Zhang Sanhe and Wang 5 are strangers. * To ensure that Zhang Sanhe and Wang wuzhong do not know each other, Zhang Sanhe can call the methods in Wang wuzhong. * Use Li Si as a relay to isolate Zhang San and Wang Wu First look at his UML diagram */
Public interface abstractstranger // specifies the target image, a stranger; { Abstract void operation3 (); } //--------------------------------- Public class someone // someone; (James) { Public void operation1 (friend) { Abstractstranger stranger = friend. Provide (); // Rishi replacement; Stranger. operation3 (); } } //--------------------------------- Public class friend // friend; (Li Si) { Private abstractstranger stranger = new stranger (); Public void operation2 () { System. Out. println ("operation2 "); } Public abstractstranger provide () { Return stranger; } } //--------------------------------- Public class stranger implements abstractstranger {// Stranger (Wang Wu ); Public void operation3 () { System. Out. println ("operation3 "); } } Dependency inversion principle: It depends on abstraction and not on specifics. 8.3 dependency reversal Principle Three Coupling Relationships: 1) Zero Coupling 2) specific coupling: occurs between two (instantiated) classes. 3) Abstract coupling: occurs between a specific class and an abstract class (or Java interface. What? Abstraction should not depend on details; details should depend on abstraction. Another statement: Do not implement programming for the interface. A specific Java class should only implement Java interfaces and Abstract METHODS declared in the Java class, and should not provide redundant methods. Static type of a variable: the type of the variable when it is declared. True Type of a variable: The True Type of the object referenced by the variable. The abstract type of the referenced object. In many cases, a JavaProgramYou need to reference an object. At this time, if this object has an abstract type, you should use this abstract typeStatic variable type. This isInterface Programming. Example: Egg x = new chicken (); Instead of: Chicken x = new chicken (); List employees = new vector (); Instead of: vector employees = new vector (); |