Comprehensive Analysis of UML class diagram relationships)

Source: Internet
Author: User
Tags repetition

This article reprint casually, Please retain the Source: http://blog.csdn.net/dylgsy/

UML class diagram relationships include association, aggregation/combination, dependency, and generalization (inheritance ). The Association is divided into two-way Association, one-way Association, and self-Association. Let's take a look at what these relationships are and what their differences are.

1. Association

Bidirectional Association:
C1-C2: both parties know the existence of the other side, can call the other side of the Public attributes and methods.

This is described in the gof design patterns book: although this relationship is applicable in the analysis phase, we think it is too abstract to describe the class relationships in the design patterns, because the link must be mapped to an object reference or pointer during the design phase. Object Reference itself is directed and is more suitable for expressing the relationship we are discussing. Therefore, this relationship is rarely used during design, and associations are usually directed.

TheCodeYes:

Class C1
... {
Public:
C2*Thec2;

} ;

Class C2
... {
Public:
C1*Thec1;

} ;

Two-way association shows that both parties have a pointer to each other in the Code. Of course, it can also be a reference or a value.

Unidirectional Association:
C3-> C4: indicates the acquaintance relationship. C3 knows C4 and C3 can call the common attributes and methods of C4. There is no life cycle dependency. It is generally a reference.

The generated code is as follows:

Class C3
... {
Public:
C4*Thec4;

} ;

Class C4
... {

} ;

Code with one-way association shows that C3 has C4 pointer, while C4 has no knowledge about C3.

Self-Association (reverse Association ):
Reference yourself with your own reference.

The Code is as follows:

Class C14
... {
Public:
C14*Thec14;

} ;

That is, there is a reference within your own.

2. Aggregation/Combination

When there is an integral-partial relationship between classes, we can use combination or aggregation.

Aggregation: indicates that c9 aggregates C10, but C10 can leave C9 and exist independently (the existence of this class makes sense in the problematic domain of an application. For more information about this sentence, see the explanation in the combination below ).

The Code is as follows:

Class C9
... {
Public:
C10 thec10;

} ;

Class C10
... {

} ;

 

Combination (also known as inclusion): it is usually indicated by solid diamond and solid line arrows, as shown in. It indicates that C8 is tolerated by C7, and C8 cannot be independent from C7. However, this depends on the problem domain. For example, in the field of concerned about automobiles, tires must be combined in the automobile industry because it makes no sense when it leaves the automobile. However, in the shops where tires are sold, even if the tires leave the car, it makes sense, and this can be aggregated. In Agile development, we also said that if a combines B, A needs to know the lifecycle of B, that is, a may be responsible for generating or releasing B, or a knows the generation and release of B in some way.

Their code is as follows:

Class C7
... {
Public:
C8 thec8;

} ;

Class C8
... {
} ;

As you can see, code is the same as aggregation. The specific differences may only be distinguished by semantics.

3. Dependency

Dependency:
Some methods of C6 may be used in C5. You can also say that the assistance of C6 must be provided to complete all functions in C5. C5 depends on the definition of C6. It generally contains the header file of C6 in the header file of C5 class. Rose does not generate attributes for the dependency.

Note: Avoid bidirectional dependency. Generally, there should be no two-way dependency.

The code generated by Rose is as follows:

// C5.h
# Include " C6.h "

Class C5
... {

} ;

// C6.h
# Include " C5.h "

Class C6
... {

} ;

Although Rose does not generate attributes, it is generally a method in form that uses the object of B as a parameter (assume that a depends on B ). As follows:

# Include " B. H "
Class A
... {
VoidFunc (B&B );
}

What is the difference between dependency and aggregation \ combination and association?

Association is a kind of relationship between classes. For example, teachers teach students, husbands and wives, and water bottles are a kind of relationship. This relationship is very obvious and can be obtained directly through analysis in the problem field.

Dependency is a weak Association. When one class uses another class, but the relationship with the other class is not too obvious (it can be said that the class is "uses ), we can regard this relationship as dependency, and dependency can also be said to be an accidental relationship, rather than an inevitable relationship, that is, "I accidentally used it in a method, but in reality, I have little to do with it ". For example, I have nothing to do with a hammer, but I used it when I had to nail it. This is a dependency that relies on a hammer to complete the dingtalk.

A combination is a whole-part relationship. In the problem domain, this relationship is obvious and can be obtained through direct analysis. For example, tires are a part of a car, leaves are a part of a tree, and hands and feet are a part of the body. This relationship is very obvious as a whole-part relationship.

The preceding relationships (Association, aggregation/combination, and dependency) may appear in the code in the form of pointers, references, values, and so on in another class, but logically, they have the above differences.

It should also be noted that the so-called relationships are valid only in a problem domain. If the problem domain is left, these relationships may not be valid, for example, they may be in a problem domain, I am a carpenter and need to work with a hammer. The whole problem may be described as how to pin the table, chair, and cupboard with a hammer. Since the whole problem is described as follows, the relationship between me and the hammer is not only accidental dependencies, but the relationship between me and the hammer has become very close and may rise to a combination relationship (reminds me of the sword of martial arts novels, the sword is dead ...). This example may be a bit ridiculous, but it is also to illustrate the truth that, like links and classes, they are all established in a problem field and leave this problem field, they may no longer exist.

4. generalization (inheritance)

Generalized relationship: This is used if two classes have generalized relationships, such as parent and child, animals and tigers, plants and flowers.
The code generated by Rose is as follows:

# Include " C11.h "

Class C12: Public C11
... {
} ;

5. The template is mentioned here by the way.

The code for the above figure is as follows:

Template < Int >
Class C13
... {
} ;

Let's talk about the degree of repetition. In fact, after reading the above description, we should be clear about the relationship between the various relations and what the code is like, the so-called degree of repetition, it is just an extension above. For example, if a and B have a "one-to-many" repetition degree, there is a list in a that stores n references of object B, that's all.

Now, I have finished the class graph relationship above. I hope you will get something better. I have also spent a lot of time (drawing, generating code, writing to blog, alas, a sweat ). However, it is worth it if you can thoroughly understand the relationships of UML class diagrams. :)

Note 1:

Association relationship (Association) Describes the givenClass ObjectSemantic connections between individuals. Associations provide connections between different interaction objects. The remaining relationship is related to the description of the category,Instead of their instances.

From:The uniied Modeling Language Reference Manual

By James Rumbaugh, Ivar jakbson, Grady booch

Interpreted by ADAMS Wang

Therefore, if there is no object, there is no association relationship. InProgramIf the static method of the class is called but the instance of the class is not generatedThere is no association between objects.. HoweverBetween classesIs dependent.

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.