UML diagram:
Inherited
Refers to the ability of a class (called a subclass, sub-interface) to inherit the functionality of another class (called the parent class, the parent interface) and to augment its own new functionality, which is the most common relationship between a class and a class or interface and interface, in Java where such relationships are explicitly identified by the keyword extends, In the design of the general is not controversial;
650) this.width=650; "Src=" http://p.blog.csdn.net/images/p_blog_csdn_net/sfdev/EntryImages/20090218/ Generalization.jpg "width=" 293 "height=" 204 "style=" border:0px; "/>
Realize
Refers to a class class implementation of the interface interface (can be multiple) function, the implementation is the most common relationship between the class and interface, in Java such relationships through the keyword implements clearly identified, at design time is generally not controversial;
650) this.width=650; "Src=" http://p.blog.csdn.net/images/p_blog_csdn_net/sfdev/EntryImages/20090218/ Realization.jpg "width=" 121 "height=" 203 "style=" border:0px; "/>
Depend on
It can be simply understood that a class A uses another Class B, which is contingent, temporary, and very weak, but changes in class B affect A; For example, if someone wants to cross the river and borrow a boat, then the relationship between the man and the ship is dependent; At the code level, For Class B as a parameter is used by Class A in a methods method;
650) this.width=650; "Src=" http://p.blog.csdn.net/images/p_blog_csdn_net/sfdev/EntryImages/20090218/ Dependence.jpg "width=" 430 "height=" "style=" border:0px;/>
Association
He embodies a strong dependency between the two classes, or the semantic level of the class and interface, such as me and my friends; this relationship is more than relying on the contingency, the relationship is not temporary, generally long-term, and the relationship between the two sides is generally equal, the association can be one-way, two-way At the code level, the associated class B appears as a class attribute in the association Class A, or the association Class A refers to a global variable of the type being associated with Class B;
650) this.width=650; "Src=" http://p.blog.csdn.net/images/p_blog_csdn_net/sfdev/EntryImages/20090218/ Association.jpg "width=" 430 "height=" "style=" border:0px;/>
Polymerization
Aggregation is a special case of association relationship, he embodies the whole and part, the relationship of ownership, that is, the relationship between the whole and the part is has-a, they can have their own life cycle, some can belong to multiple whole objects, can also be shared for multiple whole objects, such as computer and CPU, The relationship between the company and its employees, and the expression at the code level, and the association is consistent, can only be differentiated from the semantic level;
650) this.width=650; "Src=" http://p.blog.csdn.net/images/p_blog_csdn_net/sfdev/EntryImages/20090218/ Aggregation.jpg "width=" 430 "height=" 108 "style=" border:0px; "/>
Combination
The combination is also a special case of the relationship, he embodies a contains-a relationship, which is stronger than aggregation, also known as strong aggregation, he also embodies the relationship between the whole and part, but at this time the whole and part is not divided, the whole life cycle end also means that part of the life cycle end , such as you and your brain; At the level of the code, the relationship is consistent and can only be differentiated from the semantic level;
650) this.width=650; "Src=" http://p.blog.csdn.net/images/p_blog_csdn_net/sfdev/EntryImages/20090218/ Composition.jpg "width=" 430 "height=" 106 "style=" border:0px; "/>
Write the code for the singleton pattern.
public class Singleton {private static Singleton uniqueinstance = null; Private Singleton () {//Exists only to defeat instantiation. } public static Singleton getinstance () {if (uniqueinstance = = null) {uniqueinstance = new Singleton (); } return uniqueinstance; }//Other methods ...}
Java Programmer's interview book (design mode section)