[UML] Several relationships between classes in UML

Source: Internet
Author: User
Classes may have the following relationships: Association, dependency, aggregation, aggregation, composition, and generalization, also known as inheritance), implementation (realization ). 1: Association refers to the relationship between two classes. For example, a customer and an order can only belong to one customer. A customer may have multiple orders. It can be divided into one-way and two-way directions. It can be divided into one-to-one, one-to-multiple, and many-to-many based on the corresponding quantity. The corresponding UML diagram is as follows:
The associations are represented by solid lines and arrows. It shows that the relationship between customer and order is a two-way one-to-multiple relationship. The corresponding Java code is as follows:
class Customer {  private Integer id;  private String name;  private Set<Order> orders;  public Set<Order> getOrders() {   return orders;  }  public void setOrders(Set<Order> orders) {   this.orders = orders;  } } class Order {  private Integer id;  private float money;  private Customer customer;  public Customer getCustomer() {   return customer;  }  public void setCustomer(Customer customer) {   this.customer = customer;  } }
The relationship between customer and order is a two-way one-to-multiple relationship. Therefore, there should be a set of order in customer, and the attribute of customer should be in order. 2: dependency refers to the call relationship between classes. Class A refers to the attributes or methods of Class B, or Class A is responsible for instantiating Class B, so Class A depends on Class B. Unlike associations, you do not need to define B-type attributes in Class. For example, if a bicycle or a pump is inflated by a pump, you need to call the inflation method of the pump. The corresponding UML diagram is shown below: The dependency is represented by a dotted line + arrow. It shows that the relationship between the bicycle and pump is dependent on the pump. The corresponding Java code is as follows:
Class bicycle {public void expand (pump) {pump. blow () ;}} class pump {public void blow () {system. out. println ("inflatable ...... ");}}

A pump does not belong to a specific bicycle. A pump can provide inflatable services for multiple bicycles. You do not need to define the attributes of the pump type in the bicycle, but pass a parameter of the pump type to the bicycle method.


3: aggregation is the relationship between the whole and the part. For example, the computer and the motherboard are a whole, and the motherboard is a part of it. The main board, graphics card, display and other components constitute a computer. The corresponding UML diagram is as follows:

Hollow diamond + solid line are used for aggregation. The display computer is composed of mainboard and displaycard. The corresponding Java code is as follows:

Class computer {private mainboard; private displaycard; Public void on () {system. out. println ("Enable computer ...... ");} public void close () {system. out. println ("turn off the computer ...... ");} public void run () {system. out. println ("the computer is running ...... ") ;}} class mainboard {public void control () {system. out. println ("Computer Control ...... ") ;}} class displaycard {public void display () {system. out. println ("computing and display data ...... ");}}
A computer consists of a motherboard, a video card, and other components. Therefore, the mainboard and displaycard attributes are defined in the computer. Classes in aggregation can be independent. For example, a motherboard can be in the or B state. That is to say, it still makes sense after this motherboard leaves computer. 4: the class in the combination is also the relationship between the whole and the part. Unlike aggregation, the class in the combination cannot be opposite. For example, a person may consist of a head, a hand, a leg, and a trunk. If the head leaves the person, it makes no sense. The corresponding UML diagram is shown below: The combination uses solid diamond and solid line representation. People is composed of head, hand, and leg. The corresponding Java code is as follows:
Class People {Private head; private hand; private leg; Public void think () {head. think ();} public void holdthing () {hand. holdthing ();} public void walk () {leg. walk () ;}} class head {public void think () {system. out. println ("thinking ...... ") ;}} class hand {public void holdthing () {system. out. println ("get something ...... ") ;}} class leg {public void walk () {system. out. println ("walking ...... ");}}
People and Head, hand, and leg are inseparable. Leaving people has no practical significance. The head, hand, and leg attributes are defined in people. A combination can also be seen as a special form of aggregation. The code for aggregation is almost the same as the code for combination. The Code alone cannot distinguish whether the two classes are aggregated or composite. Therefore, it is necessary to distinguish it based on the actual business environment. For example, if a car or tire owner buys a car, it must have been made up of tires. In this business, the tires and cars are in a combination and they are separated without practical significance. In a car repair shop, cars can change tires. Therefore, in the business environment of a car repair shop, cars and tires are aggregated. It is of business significance to leave the car. 5: The generalization is better understood, that is, there is an inheritance relationship between the two classes. For example, people and students inherit people. Apart from having common attributes and methods of people, students also have learning methods. The corresponding UML diagram is shown below: the generalization is represented by a hollow triangle + solid line. Student inherits people. The corresponding Java code is as follows:
Class People {protected string name; protected string sex; protected date birthday; Public void eat () {system. out. println (name + "eating ...... ");} public void drink () {system. out. println (name + "drinking water ...... ");} public void sleep () {system. out. println (name + "resting ...... ") ;}} class student extends people {public void Study () {system. out. println (name + "learning ...... ");}}

Student inherits from people and has an additional study method.

 

6: A class implements an interface. The corresponding UML diagram is as follows:

It is represented by triangle arrows and dotted lines. It indicates that both cardriver and planedriver have implemented the driver interface. The corresponding Java code is as follows:

Public interface driver {void drive ();} class cardriver implements driver {public void drive () {system. out. println ("driving a car ...... ") ;}} class planedriver implements driver {public void drive () {system. out. println ("driving a plane ...... ");}}
It is worth noting that the relationships of association, dependency, aggregation, and combination are easy to confuse. When an association, dependency, aggregation, or combination relationship exists between object A and object B, object a may call the method of object B. These are their similarities. They also have their own features. For two relatively independent objects a and B, when the instance of an object A has a fixed ing relationship with the instance of B, the two objects are associated. The Code defines B type attributes in. For two relatively independent objects a and B, when an object A is responsible for constructing an instance of object B or calling services provided by object B, the two objects are mainly dependent. The expression in the Code is that parameters of the B type are passed into the method of A, rather than defining properties of the B type in. There is no obvious difference between aggregation, combination, and association in the code. It mainly depends on the actual business environment and determines the relationship between the codes based on the actual business environment. The two classes are in different business environments, and their relationships may be different.

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.