UML class diagram

Source: Internet
Author: User

Relationship between classes (1)
In a software system, classes do not exist independently. Classes and classes have various relationships. for different types of relationships, UML provides different representations.
1. Association
Association is the most common relationship between a class and a class. It is a structured relationship used to indicate the relationship between a Class Object and another class object, such as cars and tires, masters and apprentices, classes and students. In a UML class diagram, a class is connected to an object associated with a real line. When Java, C #, C ++, and other programming languages are used to realize the Association, A Class Object is usually used as a member variable of another class. When a class chart is used to represent an association, you can mark the role name on the Association line. Generally, a verb or noun representing the relationship between the two is used to represent the role name (sometimes this term is the Instance Object Name ), the two ends of a link represent two different roles. Therefore, a link can contain two role names. The role name is not required and can be added as needed, the purpose is to make the relationship between classes clearer.
For example, if a logon interface type LoginForm contains a Registration button loginButton of the JButton type, they can be expressed as associations. During code implementation, you can define an attribute object named loginButton in LoginForm, its type is JButton. 1:

Figure 1 associated instance
Figure 1 shows the corresponding Java code snippet:
[Java]
Public class LoginForm {
Private JButton loginButton; // It is defined as a member variable.
......
}
 
Public class JButton {
......
}
In UML, the Association usually includes the following forms:
(1) bidirectional Association
By default, associations are bidirectional. For example, a Customer buys a Product and owns the Product. On the contrary, a Customer is always associated with the sold Product. Therefore, there is a bidirectional relationship between the Customer class and the Product class, as shown in Figure 2:

Figure 2 two-way Association instance
The Java code snippets corresponding to Figure 2 are as follows:
[Java]
Public class Customer {
Private Product [] products;
......
}
 
Public class Product {
Private Customer customer;
......
}
(2) unidirectional Association
Class associations can also be one-way, and one-way associations are represented by solid lines with arrows. For example, if a Customer has an Address, the Customer class and the Address class have one-way association, as shown in Figure 3:

Figure 3 unidirectional Association instance
Figure 3 shows the corresponding Java code snippet:
[Java]
Public class Customer {
Private Address address;
......
}
 
Public class Address {
......
}
(3) Self-Association
In the system, the property object type of some classes may be the class itself. This special Association is called self-Association. For example, a Node member is an object of the Node type, as shown in figure 4:

Figure 4 auto-join instance
Figure 4 shows the corresponding Java code snippet:
[Java]
Public class Node {
Private Node subNode;
......
}
(4) Multiple associations
Multiple associations, also known as Multiplicity, indicate the correspondence between two correlated objects in quantity. In UML, the diversity of objects can be expressed directly in a number or a number range in the associated line.
There can be multiple multi-correlation relationships between objects, as shown in table 1:
Table 1 multiple representation list
Representation
Description of multiple features
1 .. 1
Indicates that one object of another class is only related to one object of the class.
0 ..*
Indicates that one object of another class is related to zero or multiple objects of the class.
1 ..*
Indicates that an object of another class is related to one or more objects of the class.
0 .. 1
Indicates that one object of another class does not or is only related to one object of the class.
M.. n
Indicates that an object of another class has a relationship with the minimum m of the class and a maximum of n objects (m ≤ n)
For example, a Form can have zero or more buttons, but a Button can only belong to one interface. Therefore, objects of a Form class can be associated with zero or multiple Button objects, but objects of a Button class can only be associated with objects of a Form class, as shown in Figure 5:

Figure 5 multiple associated instances
Figure 5 shows the corresponding Java code snippet:
[Java]
Public class Form {
Private Button [] buttons; // defines a collection object
......
}
 
Public class Button {
......
}
(5) Aggregation relationship
The aggregate relationship indicates the relationship between the whole and the part. In an aggregation relationship, a member object is a part of the entire object, but the member object can exist independently of the entire object. In UML, the aggregate relationship is expressed in a straight line with a hollow diamond. For example, an automobile Engine is a component of a Car, but an automobile Engine can exist independently. Therefore, an automobile and an Engine are aggregated, as shown in Figure 6:

Figure 6 aggregation link instance
When the code implements the aggregation relationship, the member object is usually injected into the whole object as a parameter of the constructor, Setter, or business method. The Java code snippet corresponding to figure 6 is as follows:
[Java]
Public class Car {
Private Engine engine;
 
// Construct the injection
Public Car (Engine engine ){
This. engine = engine;
}

// Set value Injection
Public void setEngine (Engine engine ){
This. engine = engine;
}
......
}
 
Public class Engine {
......
}
(6) composite relationship
The Composition relationship also indicates the relationship between the whole and the part of the class. However, in the Composition relationship, the whole object can control the lifecycle of the member object. Once the whole object does not exist, the member object also does not exist, and the member object and the whole object have the same relationship between life and death. In UML, the composite relationship is represented by a straight line with a solid diamond. For example, a person's Head and Mouth are one of the parts of the Head. If the Head is gone, the Mouth is gone. Therefore, the Head and Mouth are in a combination, 7:

Figure 7 composite link instance
When the code implements the composite relationship, the member class is usually directly instantiated in the constructor of the overall class. The Java code snippet corresponding to Figure 7 is as follows:
[Java]
Public class Head {
Private Mouth mouth;
 
Public Head (){
Mouth = new Mouth (); // instantiate a member class
}
......
}
 
Public class Mouth {
......
}
Author: LoveLion

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.