UML Class Diagram II

Source: Internet
Author: User

In software systems, classes do not exist in isolation, there are various relationships between classes and classes, and UML provides different representations for different types of relationships.

1. Related relationships

An Association (association) relationship is the most common relationship between a class and a class, and it is a structured relationship that is used to indicate that a class of objects is associated with another type of object, such as cars and tires, Master and apprentice, class and student, and so on. In UML Class diagrams, the classes that connect objects associated with a solid line are used to implement association relationships using programming languages such as Java, C #, and C + +, and the object of one class is typically used as a member variable of another class . When you use a class diagram to represent an association relationship, you can label the role name on the association line, typically using a verb or noun that represents the relationship between the two, representing the role name (sometimes the noun is the instance object name), and the relationship ends up representing two different roles, so you can include two role names in an association relationship, which is not required. Can be increased as needed, with the aim of making the relationships between classes more explicit.

such as in a login interface class LoginForm contains a JButton type of registration button Loginbutton, they can be expressed as an association relationship, code implementation can be defined in LoginForm a Property object named Loginbutton, Its type is JButton. 1 is shown below:

Figure 1 Association Relationship Instance

The corresponding Java code snippet in Figure 1 is as follows:

[Java]View PlainCopy
    1. Public class LoginForm {
    2. Private JButton Loginbutton; //defined as member variable
    3. ......
    4. }
    5. Public class JButton {
    6. ......
    7. }

In UML, association relationships often contain several forms:

(1) Bidirectional correlation

By default, associations are bidirectional. For example, a customer (customer) buys a product and owns a product, and conversely, a customer sells a product that is associated with it. Therefore, there is a two-way association between the customer class and the Product class, as shown in 2:

Figure 2 Bidirectional Association instance

The corresponding Java code snippet in Figure 2 is as follows:

[Java]View PlainCopy
    1. Public class Customer {
    2. Private product[] Products;
    3. ......
    4. }
    5. Public class Product {
    6. Private customer customer;
    7. ......
    8. }

(2) One-way correlation

The Association of a class can also be unidirectional, and a one-way association is represented by a solid line with arrows. For example: The customer has an address, and the customer class has a one-way association with the address class, as shown in 3:

Figure 3 One-way associative instance

The corresponding Java code snippet in Figure 3 is as follows:

[Java]View PlainCopy
    1. Public class Customer {
    2. private address address;
    3. ......
    4. }
    5. Public class Address {
    6. ......
    7. }

(3) Self-correlation

There may be some classes in the system where the Property object type is the class itself, and this special association relationship is called self-correlation. For example, the member of a node class is also an object of node type nodes, as shown in 4:

Figure 4 Self-correlating instances

The corresponding Java code snippet in Figure 4 is as follows:

[Java]View PlainCopy
    1. Public class Node {
    2. Private Node subnode;
    3. ......
    4. }

(4) Multiple sexual associations

The Multiplicity Association is also called the Multiplicity (multiplicity) Association, which represents the correspondence relation between the number of two related objects. in UML, the multiplicity of objects can be represented directly on the associated line by a number or a range of numbers.

There can be multiple multi-affinity relationships between objects, and the common multiplicity representation is shown in table 1:

Table 1 List of multiplicity representations

Presentation mode

Description of Multiplicity

1..1

Indicates that an object of another class is related only to an object of that class

0..*

Represents an object of another class that has a relationship to 0 or more objects of that class

1..*

Represents an object of another class that has a relationship to one or more objects of the class

0..1

Indicates that an object of another class does not have or is related only to an object of that class

M.. N

Represents an object of another class with a minimum of M, a maximum of N objects (m≤n)

For example, an interface (Form) can have 0 or more buttons (button), but a button can belong to only one interface, so an object of a form class may be associated with an object of 0 or more button classes. However, an object of a button class can only be associated with an object of a form class, as shown in 5:

Figure 5 A multi-sexual association instance

The corresponding Java code snippet in Figure 5 is as follows:

[Java]View PlainCopy < param name= "wmode" value= "Transparent" >
    1. Public class Form {
    2. Private button[] buttons; //Define a Collection object
    3. ......
    4. }
    5. Public class Button {
    6. ......
    7. }

(5) Aggregation relationship

An aggregation (Aggregation) relationship represents the relationship of a whole to a part. in an aggregation relationship, the member object is part of the overall object, but the member object can exist independently from the overall object. in UML, the aggregation relationship is represented by a straight line with a hollow diamond. For example, a car engine is an integral part of a car, but a car engine can exist independently, so the car and the engine are the aggregation relationships, as shown in 6:

Figure 6 Example of an aggregation relationship

When the code implements the aggregation relationship, the member object is usually injected into the overall object as a constructor, setter method, or business method parameter , and the corresponding Java code fragment in Figure 6 is as follows:

[Java]View PlainCopy
  1. Public class Car {
  2. private engine engine;
  3. //Construction injection
  4. Public Car (engine engine) {
  5. this.engine = engine;
  6. }
  7. //Set Value injection
  8. Public void Setengine (Engine engine) {
  9. this.engine = engine;
  10. }
  11. ......
  12. }
  13. Public class Engine {
  14. ......
  15. }

(6) Combined relationship

A combination (composition) Relationship also represents the overall and partial relationship between classes, but in a composite relationship the whole object can control the life cycle of the member object, and once the whole object does not exist, the member object will not exist, and the member object has a die relationship with the whole object . In UML, a composite relationship is represented by a straight line with a solid diamond. For example: The head (head) and Mouth (Mouth), the mouth is one of the components of the head, and if the head is gone, the mouth is not, so the head and mouth is a combination of relations, 7 is shown:

Figure 7 Example of a combined relationship

When the code implements a composition relationship, the member class is typically instantiated directly in the constructor of the overall class , and the corresponding Java snippet in Figure 7 is as follows:

[Java]View PlainCopy < param name= "wmode" value= "Transparent" >
    1. Public class Head {
    2. private Mouth Mouth;
    3. Public Head () {
    4. mouth = new Mouth (); //Instantiate member class
    5. }
    6. ......
    7. }
    8. Public class Mouth {
    9. ......
    10. }

UML Class Diagram II

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.