UML class diagram link mode and C ++ Code Description

Source: Internet
Author: User
The author of the original article http://blog.sina.com.cn/s/blog_5ea0192f0100dyvr.htmlis not detailed. Thank you! The relational patterns in UML class diagrams mainly include: generalization, implementation, association, aggregation, and dependency.

1. Generalization)
Generalized relationship: it is an inherited relationship that represents a general and special relationship. It specifies how child classes feature all features and behaviors of the parent class. tigers are a type of animal. They have the characteristics of tigers and are also common to animals. see:

UseCodeThe statement is as follows:

Program code
// Animal. h
Class canimal
{
Public:
// Implement
Virtual hresult eatsomething ()
{
// Do something
}
};

// Tiger. h
# Include "animal. H"
Class ctiger: Public canimal
{
// Do something
};

2. Implementation)
Implementation relationship: it is the relationship between a class and an interface, indicating that the class is the implementation of all features and behaviors of the interface. See:

The Code is as follows:

Program code
// Animal. h
Class canimal
{
Public:
// Interface
Virtual hresult eatsomething () = 0;
};

// Tiger. h
# Include "animal. H"
Class ctiger: Public canimal
{
// Do something
};

Note: The difference between generalization and implementation is whether the subclass inherits the implementation of the parent class. If there is inheritance, the relation is generalization, and vice versa.

3. Association)
Ing: a type of link that can be divided into two-way Association, one-way Association, and self-Association.
Two-way association means that both parties have references of the other party and can call the Public attributes and methods of the other party. See:

The Code is as follows:

Program code
// Husband. h
# Include "wife. H"
Class chusband
{
Public:
Cwife * pwife;
};

// Wife. h
# Include "husband. H"
Class cwife
{
Public:
Chusband * phuband;
};

The relationship between husband and wife is fair, and the public attributes of the other party can be used.

One-way association means that only one party has a reference from the other. In this way, only the other party can call the Public attributes and methods of the other party. For example:

The Code is as follows:

Program code
// Husband. h
Class chusband
{
Public:
Int nmoney;
Void goshopping ();
};

// Wife. h
# Include "husband. H"
Class cwife
{
Public:
Chusband * phuband;
};

The wife has a husband and can use the attributes of the other party, such as money, to allow the other party to do what they can do, such as shopping.

Self-Association refers to having a reference of itself. For example

The Code is as follows:

Program code
// Singleman. h
Class csingleman
{
Public:
Csingleman * psingleman;
};

4. Aggregation and composition)
Aggregate correlation: the relationship between the whole and the part, and the part can be separated from the whole and exists independently. If the car and the tire are the relationship between the whole and the part, the tire can still exist.
Composite relationship: it is the relationship between the whole and the part, but the part cannot leave the whole and exists independently. If the company and the Department are the relationship between the whole and the part, there is no department without the company.

The Code is as follows:

Program code
// Car. h
# Include "tyre. H"
Class ccar
{
Public:
Ctyre;
};

// Tyre. h
# Include "car. H"
Class ctyre
{
// Do something
};

Program code
// Company. h
# Include "department. H"
Class ccompany
{
Public:
Cdepartment;
};

// Tyre. h
# Include "company. H"
Class cdepartment
{
// Do something
};

5. Dependency)
Dependency: it is a usage relationship, that is, the implementation of a class requires the assistance of another class. Therefore, try not to use bidirectional mutual dependencies. For example:

The Code is as follows:

Program code
// Car. h
Class ccar
{
// Do something
};

// Person. h
# Include "car. H"
Class cperson
{
Void movefast (ccar & pcar );
};

People's fast movement requires assistance from vehicles, but this dependency is relatively weak, that is, people can use other tools instead of using vehicles, different from association, people do not have to own this car as long as they use it.

 

 

This is my coupling strength: (not necessarily true)

Dependency -- Association -- aggregate -- Composition -- Generalization)

 

 

//////////////////////////////////////// //////////////////////////////////////// //////////

Association, combination, aggregation, and dependency comparison

Types of relationships between classes: generalization, dependency, association, aggregation, and composition ).
Aggregation and composition belong to association and are special Association associations. Generalization (generalization) represents the inheritance or implementation relationship (is ). The specific form is the inheritance relationship between classes, the inheritance relationship between interfaces, and the Implementation relationship between classes and interfaces.


The Association correlation is represented as a variable (Has ). A join between a class and a class. It allows a class to know the attributes and methods of another class. For example, if a depends on B, B is the global variable of. Associations are bidirectional and unidirectional. Two-way Association: both classes know the public attributes and operations of the other class. One-way Association: only one class knows the public attributes and operations of the other class. Most associations should be one-way, which makes it easier to establish and maintain one-way relationships and helps you find classes that can be taken.

Aggregat ion is a strongly correlated relationship. The aggregation relationship is the relationship between the whole and the individual. The two classes of the common association relationship are at the same level, while the two classes of the aggregation relationship are at different levels. One is the whole, and the other is the part. At the same time, it is a weak "ownership" relationship. Object A can contain object B, but object B is not an integral part of object. Specifically, if A is aggregated by B, it means that a contains a Global Object with B, but B can be created at the time when a is created.

Composition is a type of association relationship, which is stronger than the aggregation relationship. It requires that the object representing the whole in a common aggregation relationship be responsible for representing the lifecycle of some objects. Composition (composite relationship) is a strong "ownership" relationship, reflecting the strict relationship between the parts and the whole, and consistent with the overall life cycle. If a is composed of B, it means that a contains a Global Object of B, and B is created at the moment when a is created.

Dependency (dependency) is represented as a parameter in the function (Use ). Is the connection between classes, indicating that one class depends on the definition of another class. Changes to one class will affect the other class. For example, if a depends on B, B is embodied as a local variable, a method parameter, or a static method call.

Related Article

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.