On 5 kinds of relationships between classes in UML class diagrams

Source: Internet
Author: User
Tags dashed line

what is a UML class diagram? A class diagram shows a set of classes, interfaces, collaborations, and relationships between them. In UML, problem domains are gradually transformed, modeled by classes, and constructed by programming languages to implement the system. Classes, together with their relationships, form the class diagram, which can also include elements such as interfaces, packages, and so on, including objects, chains, and so on.


the relationship between classes and classesthe relationship between classes and classes can be divided into the following five types according to the strength of the relationship:
Dependency relationship (Dependency)---Association Relationship (Association)---aggregation (Aggregation)---combination (composition)---generalization (generalization)

Dependent RelationshipsA dependency is a relationship with the least coupling in five relationships. Use a dashed line with arrows, as shown in:


Code:

public class Person {public void call () {Phone phone = new Phone ();}}

public class Phone {}


Explanation: There is no relationship between person and Phone, but because of occasional needs, person needs to use phone, in the person class in the specific method to instantiate the phone, then person depends on the phone. Holding the phone class is a method of the person class, not the person class, which is the most important.



Association Relationshipan association relationship is a solid line plus an arrow representation. Indicates that the relationships between classes are stronger than dependencies, as shown in:


Code:
public class Person {//header is a property of human public head Head;public man () {//TODO auto-generated constructor stub}}

public class Head {public head () {//TODO auto-generated constructor stub}}


Explanation: Head is used as an internal property of the person class, and once the person class is instantiated and a head instance is instantiated, their dependency is stronger. The most important difference from correlation is that dependency is used in the inner concrete method of the class to another class, and the association is used as an internal property, and the dependency is instantiated only when the specific method is called, such as when the person just called phone instantiation. The association, when a person is new, instantiates a head class as an internal property of the person.


Aggregation RelationshipsThe aggregation relationship is represented by a hollow diamond plus arrow, as shown in:

Code:
public class Persongroup {public person person;//passes the person as a constructor parameter into public persongroup (person person) {//TODO auto-generated constructor Stubthis.person = person;}}

public class Person {public person () {//TODO auto-generated constructor stub}}


Explanation: The person class is the parameter of the Persongroup constructor method, so that the person class can exist completely out of the Persongroup class and not die because of the extinction of the Persongroup instance, just like in real life, People don't hang out because they're out of the crowd ...


Combinatorial RelationshipsThe composition relationship is represented by a solid diamond plus arrow, as shown in:

Code:
public class Person {public Foot foot;public person () {//TODO auto-generated constructor stub//instantiate in constructor method Foot = new Foot () ;}}

public class Foot {public Foot () {//TODO auto-generated constructor stub}}


Explanation:The foot class is specifically instantiated in the constructor method of the person class, and once the person instance is generated, the foot instance is also generated, and when the instance of the person dies, its foot instance dies, just like in real life, generating a man, and will accompany the generation of the feet, But if that person hangs up, its feet will naturally ... (It sounds strange-.-)


Generalization RelationshipsA generalization relationship typically contains the inheritance relationship between a class and a class and the implementation of a class to an interface, as shown in:Inheritance:
Interface:



Summary: Previously has been the difference between them vaguely, through the review and summed up the personal experience, I hope that everyone's understanding also helpful

On 5 kinds of relationships between classes in UML class diagrams

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.