Relationships between classes in a UML diagram: dependency, generalization, association, aggregation, combination, implementation

Source: Internet
Author: User

Class and Class Diagram1) A Class encapsulates data and behaviors and is an important part of object-oriented systems. It is a general term for object sets with the same attributes, operations, and relationships. 2) in the system, each class has certain responsibilities. Its responsibilities refer to the class's tasks, that is, the class's functions and obligations. A class can have multiple responsibilities. A well-designed class generally has only one responsibility. when defining a class, the class responsibilities are divided into class attributes and operations (I .e., methods ).

3) class attributes are the data responsibilities of the class, and class operations are the behavior responsibilities of the class.

1. Dependency)

Dependency ):Assume that Class A changes B, Class B depends on Class.

• Dependency is a type of use relationship. Changes to a specific thing may affect other things that use the same thing. Dependency is used to indicate that one thing uses another. In most cases, the dependency relationship is reflected in the method of a class using the object of another class as the parameter. • In UML, dependencies are represented by dotted lines with arrows, and the dependent party points to the dependent party.
public class Driver{    public void drive(Car car)    {        car.move();    }    ……}public class Car{    public void move()    {        ......    }    ……}

There are three cases of dependency:

1. Class A is a local variable in Class B (of a certain method;

2. Class A is a parameter in Class B methods;

3. Class A sends messages to Class B, which affects Class B changes;


Ii. Generalization)

Generalization ):A is the parent class of B and C. B and C have common classes (parent class) A, which indicates that a is B and C is generalized (or generalized)

• Generalization is the inheritance relationship, also known as the "is-a-kind-of" relationship. The Generalization relationship is used to describe the relationship between the parent class and the Child class, A parent class is also called a base class or a superclass, and a subclass is also called a derived class. In UML
The link is represented by a straight line with a hollow triangle. • During code implementation, the Object-oriented Inheritance mechanism is used to implement generalized relationships. For example, the extends keyword is used in Java and the colon is used in C ++/C #:.


public class Person {    protected String name;    protected int age;    public void move()     {        ……    }    public void say()    {        ……    }}public class Student extends Person {    private String studentNo;    public void study()     {        ……    }}

In UML, there are three requirements for generalized relationships:

1. The subclass and the parent class should be exactly the same. The attributes and operations of the parent class should be included in the subclass;

2. In addition to the information consistent with the parent class, sub-classes also include additional information;

3. You can use a parent class instance or a subclass instance;

3. Association)

Association ):Class relationships, such as customers and orders, each order corresponds to a specific customer, each customer corresponds to a specific order, and then the association between the basketball team and the team (as shown in ).


The "employee" and "employer" on the two sides of the Association indicate the relationship between the two, while the number indicates the restriction of the relationship between the two, which is the multiple correlation between the two. There are usually "*" (indicating all, not limited), "1" (indicating there is only one), "0... "(0 or more)," 0, 1 "(0 or one)," n... m "(meaning n to m)," m... * "(at least m ).

• Association is the most common relationship between classes. It is a structured relationship used to indicate the relationship between a Class Object and another class object. • In a UML class diagram, the class corresponding to an associated object is connected using a solid line. When using Java, C #, C ++, and other programming languages to realize the Association, generally, the object of a class is used as the attribute of another class. • When you use a class chart to represent an association, you can mark the role name on the Association line.
1) bidirectional Association:By default, associations are bidirectional.

public class Customer{    private Product[] products;    ……}public class Product{    private Customer customer;    ……}

2) Unidirectional Association:Class associations can also be unidirectional. Unidirectional associations are represented by solid lines with arrows.
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.

public class Node{    private Node subNode;    ……} 

 4)Reconnection Association:This is also known as multiplicity, which indicates the number of connections between objects of one class and objects of another class. In UML, you can add a number directly on the associated line to indicate the number of objects of another class corresponding to it.

Representation

Description of multiple features

1 .. 1

Indicates that one object of another class is related to only one class object.

0 ..*

Indicates that one object of another class is related to zero or multiple class objects.

1 ..*

Indicates that an object of another class is related to one or more class objects.

0 .. 1

Indicates that one object of another class does not or is only related to one class object.

M.. n

Indicates that an object of another class has a relationship with at least m and at most n class objects (m <= n)

public class Form{    private Button buttons[];    ……} public class Button{    …}

4. Aggregation)

Aggregation ):Indicates the relationship between the whole and the part. The whole and the part can be separated.

• Aggregation indicates the relationship between a whole and a part. After defining a whole class, we usually analyze the composition structure of the whole class to find out some member classes. The aggregation is formed between the whole class and the member class.
Link. • In an aggregation relationship, member classes are part of the overall class, that is, member objects are part of the overall object, but member objects can exist independently of the entire object. In UML, the aggregate relationship is expressed in a straight line with a hollow diamond.

public class Car{    private Engine engine;    public Car(Engine engine)   {        this.engine = engine;    }        public void setEngine(Engine engine)    {        this.engine = engine;    }    ……}public class Engine{    ……}

For example, a telephone includes a microphone.

The computer includes a keyboard and display. One computer can be used with multiple keyboards and multiple monitors. It is determined that the keyboard and display can be separated from the host. The host can select another keyboard and display to form a computer;

5. Composition)

Composition ):It is also the relationship between the whole and the part, but the whole and the part cannot be separated.

• Composition also indicates the relationship between the whole and the part of the class, but the partial and the whole of the combination have a unified survival period. If the object does not exist, some objects do not exist.
The relationship between them is the same as that between them. • In a composite relationship, a member class is a part of the overall class, and the overall class can control the lifecycle of the member class, that is, the existence of the member class depends on the overall class. In UML, the composite relationship is represented by a straight line with a solid diamond.

public class Head{    private Mouth mouth;    public Head()    {mouth = new Mouth();    }    ……}public class Mouth{    ……}

For example, if A company and A department are part of the company and the company is the whole, the Finance Department of Company A cannot be switched with the Finance Department of Company B. That is to say, Company A cannot be separated from its own Finance Department; human and human heart.


6. Implementation)

Implementation ):Is used to specify the class or build structure relationship between interfaces and real-line interfaces. interfaces are a set of operations, and these operations are used to specify a class or build a service.

• Interfaces can also have inheritance and dependency relationships similar to the relationships between classes, but there is also an implementation relationship between interfaces and classes. In this relationship, the class implements the interface, and the operations in the class implement the Operations declared in the interface. In UML, the implementation relationship between classes and interfaces is expressed by dotted lines with hollow triangles.

public interface Vehicle {    public void move();}public class Ship implements Vehicle{    public void move()     {    ……    }}public class Car implements Vehicle{    public void move()     {    ……    }}

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.