Sort out the relationship between Class digrams in UML, umldigrams

Source: Internet
Author: User

Sort out the relationship between Class digrams in UML, umldigrams
What is a UML class diagram?

A class diagram shows a group of classes, interfaces, collaboration, and relationships between them. In UML, the problem domain will be gradually transformed. Classes are used for modeling and these classes are constructed using programming languages to implement the system. The relationships between classes constitute a class diagram. class diagrams can also contain interfaces, packages, and other elements, as well as objects, links, and other instances.

Relationship between classes 1. Dependency)

Unidirectional, indicating that one class depends on the definition of another class. Changes to one class will affect the other class, which is a "use a" relationship.

If A depends on B, B is represented as A local variable, method parameter, static method call, etc.

Public class Person {public void doSomething () {Card card = new Card (); // local variable ....}}
Public class Person {public void doSomething (Card card Card) {// method parameter ....}}
Public class Person {public void doSomething () {int id = Card. getId (); // call the static method ...}}
2. Association)

Unidirectional or bidirectional (we usually need to avoid using Bidirectional associations). It is a "has A" relationship. If A is unidirectional to associate B, it can be said that a has a B, generally, it is a global variable.

public class Person {      public Phone phone;            public void setPhone(Phone phone){                this.phone = phone;      }            public Phone getPhone(){                  return phone;      }  }  
3. Aggregation)

One-way, one type of association relationship. The difference between the association relationship is semantic. The two associated objects are usually equal, while aggregation is generally unequal, there is a general and local feeling, there is little difference in implementation

There is also a hollow diamond and arrow, and there is no arrow here

A Team is composed of persons with different lifecycles. The entire Team does not exist. Some of the Team still exists. Currently, the Team is disband, the Team is still in progress, and you can join other groups.

public class Team {      public Person person;            public Team(Person person){          this.person = person;      }  }  
4. Composition)

Unidirectional, a special aggregation relationship with strong Dependencies

There is also a solid diamond and arrow, and there is no arrow here

Head, Body, Arm, and Leg are combined into People, and their lifecycles are the same. If the whole does not exist, parts will also die.

public class Person {      public Head head;      public Body body;      public Arm arm;      public Leg leg;            public Person(){          head = new Head();          body = new Body();          arm = new Arm();          leg = new Leg();      }  }  
5. Inheritance)

Class implementation interface, class inheritance abstract class, class inheritance parent class all belong to this relationship

 

Implementation: The "like a" relationship. Class implementation interfaces belong to this relationship.

Generalization: the "is a" relation. The class inherits the abstract class. The class inherits the parent class.

Summary: The relationships between classes can be divided into the following five types based on their strength:

Dependency --- Association --- Aggregation --- Composition --- Generalization)

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.