Diagram of the six-middle relationship between a UML class and a class

Source: Internet
Author: User

 

I used EA to draw a diagram of the design pattern:
 

Six relationships in UML:
Dependency

 

Dependency

 

[Di 'pend sans NSI]
Association

 

Association

 

[Province, S Province U province I 'ei province limit N]
Aggregation relationship

 

Aggregation

 

[Too many gr records when GE has reached limit N]
Combination Composition [, K queue MP comment 'zi queue limit N]
Implementation

 

Realization

 

[, RI: Limit l comment 'ze Comment comment limit N]
Generalization

 

Generalization

 

[, D When en then r then l then 'ze then Jun N]

 

Six relationships in UML:
    1. Association: connects model elements and links to instances, expressed by a solid line;
    2. Dependency: indicates that an element depends on another element in some way. It is expressed by a dotted line and an arrow;
    3. Aggregation: the relationship between the whole and the part, expressed by a solid line and a hollow diamond;
    4. Composition: indicates the relationship between the whole and the part, expressed by a solid line and a solid diamond;
    5. Generalization (inheritance): represents the general and special relationship, expressed by a solid line and a hollow arrow;
    6. Implementation: represents the relationship between classes and interfaces, expressed by a dotted line and a hollow arrow;

Note: general relations and implementation relations are collectively referred to as general relations;
In a word, a general link is represented by inheritance or implementation (is a), and a link, an aggregation link, or a combination is represented by a member variable (Has ), the dependency is represented by the parameters in the function (Use );

Six relationships in UML are described in detail:

1. Relationship:
Meaning: links between classes. Associations enable a class to know the attributes and methods of another class. They generally contain the meanings of "know" and "understand ".
Embodiment: in C #, associations are implemented through member variables;
Direction: bidirectional or unidirectional;
Figure: solid line + arrow; arrow points to the associated class;
For example, fishermen must know the weather conditions before they can go abroad.
// Associated company employees
Public class company
{
Private employee;
Public Employee getemployee ()
{
Return employee;
}
Public void setemployee (employee Employee)
{
This. Employee = employee;
}
// Company operation
Public void run ()
{
Employee. startworking ();
}
}

// A associates with B
Class
{
B = new B ();
}
Class B
{
}

It represents a strong dependency between two classes or between classes and interfaces, such as my friends and I; this relationship is more dependent than the dependency relationship, with no dependency relationship. The relationship is not temporary and is generally long-term. In addition, the relationship between the two parties is generally equal, and the relationship can be one-way and two-way; shown inCodeLevel: For associated Class B to appear in the form of class attributes in associated Class A, it may also be that associated Class A references a global variable of the type of associated Class B;

 

2. Dependency:
Meaning: it refers to the connection between a class and a class, indicating that a class depends on the definition of another class. Dependency only describes a usage and usage relationship between the class;
Embodiment: in C #, local variables, method/function parameters, or calls to static methods;
Direction: unidirectional;
Figure: dotted line + arrow;
For example, people depend on water and air, and cars depend on gasoline.
// Rowing by people, depending on the ship
Public class person
{
// Rowing
Public void oarage (boat)
{
Boat. oarage ();
}
}

// A depends on B
Class
{
Public void function (B)
{}
}
Class B
{
}

It can be simply understood that a class A uses another class B, and this relationship is contingent, temporary, and very weak, but changes in Class B will affect a. For example, if someone wants to cross the river and borrow a ship, the relationship between people and the ship is dependent. It is manifested in the Code layer, class B is used as a parameter by Class A in a method;

 

3. Aggregation relationship:
Meaning: it is a type of association, a type of strong association, an aggregation relationship is the relationship between the whole and the individual/part, and the two classes of the association relationship are at the same level, the two classes of the aggregation relationship are at different levels. One is the whole, and the other is the individual/part. In the aggregation relationship, objects representing the individual/part may be shared by multiple objects representing the whole;
Embodiment: In C ++, the aggregation relationship is implemented by using the aggregate's (array) pointer as an internal Member;
Direction: unidirectional;
Figure: hollow diamond + solid line + arrow; arrow points to aggregated class, that is, arrow points to individual/part;
For example, a duck group has an aggregation relationship with a duck. A car consists of an engine, a tire, and other parts. Because a car breaks down, there is no broken engine, and tires and other parts can still be used.

Aggregation is a special case of association. It reflects the relationship between the whole and the part, that is, the relationship between has-a. At this time, the whole and the part can be separated, they can have their own lifecycles, some of which can belong to multiple objects, or share multiple objects, such as the relationship between computers and CPUs, and between companies and employees; it is manifested at the code level and is consistent with the Association, which can only be distinguished at the semantic level;

 

4. Composite relationship:
Meaning: it is also a type of association, but it is stronger than the aggregation relationship. the composite relationship requires that the object representing the whole in the aggregate relationship be responsible for representing the entire lifecycle of the object of the individual/part; the composite relationship cannot be shared; in the composite relationship, if the entire object is destroyed or damaged, the objects that represent the individual/part of the object will be destroyed or destroyed, and will be clustered in the composite relationship, objects representing the individual/part of the object may be shared by multiple objects representing the whole, but may not be destroyed or destroyed as an object representing the whole is destroyed or damaged;
Embodiment: in C #, composite relationships are implemented through member variables;
Direction: unidirectional;
Figure: solid diamond + solid line + arrow; arrow points to the object representing the individual/part, that is, the object of the combined class;
For example, a person is composed of various organs, such as the head, limbs, and so on, because a person has the same life cycle as these organs, and the person is dead, these organs are also suspended;

A combination is also a special case of association. It represents a contains-a relationship, which is stronger than aggregation, also known as strong aggregation; it also shows the relationship between the whole and the part, but the whole and the part are inseparable at this time, and the end of the overall life cycle means that part of the life cycle ends; for example, you and your brain; it is manifested at the code level and is consistent with the Association, which can only be distinguished at the semantic level;

 

5. Generalized relationship:
Meaning: it indicates the relationship between a more generalized element and a more specific element, that is, the inheritance relationship of classes;
Embodied in: in C #, generalized relationships are implemented through class inheritance;
Direction: unidirectional; Child class inherits parent class;
Figure: hollow arrow + solid line; arrow pointing to parent class;
For example, animals can be divided into mammals, spine animals, and crawlers.

A class (called a subclass or sub-interface) inherits the functions of another class (called parent class or parent interface) and can add its own new functions, inheritance is the most common relationship between classes or interfaces. in Java, such relationships are identified by the keyword extends, which is generally not controversial during design;

 

6. Implementation relationship:
Meaning: it specifies a contract between two entities; that is, one entity defines a contract, and the other entity guarantees the performance of the contract;
Embodiment: in C #, the implementation relationship is implemented through the class implementation interface, that is, a class implements an interface;
Direction: unidirectional; sub-class implementation interface;
Figure: hollow arrow + dotted line; Arrow refers to interface;
For example, Donald Duck (object) can speak (Interface), because the average duck does not speak, so it does not bring the method of speaking to the average duck; Superman (object) can fly (Interface)

It refers to the function of a class to implement interface interfaces (which can be multiple). Implementation is the most common relationship between classes and interfaces. in Java, such a relationship is identified by the keyword implements, it is generally not controversial during design;

 

Summary:There is no doubt about the relationship between inheritance and implementation. They reflect the vertical relationship between a category and a class, or between a class and an interface; the other four relationships reflect the reference and horizontal relationships between classes and classes, or between classes and interfaces, which is difficult to distinguish, there are many relationships between things. It is very difficult to prepare for positioning. As mentioned above, these relations are all semantic-level, so they cannot be fully differentiated from each other at the code level; in general, the strength of the last several relationships is: Combination> aggregation> association> dependency.

 

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.