[Reprint] The difference between generalization, implementation, dependency, and association

Source: Internet
Author: User

Original link: http://www.cnblogs.com/linjiqin/archive/2013/11/09/3415440.html

The dependency in traditional application design refers generally to the relationship between classes, so let's review the relationship between classes:
A, to achieve
Represents the implementation of a class-to-interface.
The implementation of the UML diagram uses a dashed line with a hollow triangular arrow pointing to the interface, as follows:

B, generalization
Represents an inheritance relationship between a class and a class, and an inheritance relationship between an interface and an interface.
The implementation of UML diagrams uses a solid line with a hollow triangular arrow pointing to the base class, as follows:

C, Reliance
Acting as a parameter in a function (use a) is a connection between a class and a class, which means that one class relies on the definition of another class, and one class change affects the other class. For example, if a depends on B, then B is represented as a local variable, a method's parameter, or a call to a static method. The common dependencies for Television (TV) depending on channel (channel) are as follows:
(1) Class B passes in the form of a parameter to the method of Class A.
(2) Class B exists in the method of Class A in the form of a local variable.
(3) Class A calls the static method of Class B.
The implementation of the UML diagram uses a dashed line with arrows pointing to the dependent class, as follows:

D, Association
behaves as a variable (has a), a join between a class and a class that enables a class to know the properties and methods of another class. For example, if a relies on B, then B is represented as a global variable, such as the person class and the company class.

Association relationships have two-way associations and one-way associations:
1. Bidirectional Association: Two classes are aware of the public properties and operations of another class.
2, one-way Association: Only one class knows the public properties and operations of another class.
Most associations should be unidirectional, one-way relationships easier to establish and maintain, and help find classes that can be taken.
The implementation of UML diagrams uses a solid line to connect the same or not homogeneous, as follows:

E, aggregation
is a kind of association relation, is weak association relation. The aggregation relationship is the relationship between the whole and the individual. The two classes of the common relational 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 part. At the same time, it is a weak "owning" relationship. At this point between the whole and the part is separable, they can have their own life cycle, some can belong to more than one whole object, can also be shared for multiple whole objects, such as computer and CPU, the relationship between the company and the staff, the code level, and the association is consistent, can only be differentiated from the semantic level.
The implementation of UML diagrams is represented by a line with an open-minded diamond, as follows:

F, combination
is a kind of association relationship, and is stronger than the aggregation relationship. It requires that an object representing the whole in an ordinary aggregation relationship be responsible for the life cycle of the object that represents the part. Composition (combinatorial relationship) is a strong "owning" relationship, which embodies the relationship between the strict part and the whole, and the life cycle of the part and the whole is consistent. He also embodies the relationship between the whole and the part, but at this time the whole and the part is not divided, the whole life cycle end also means that part of the life cycle end; For example, you and your brain, window and frame, must be attached to the window when creating a frame in the window. When the window disappears, the frame disappears; At the code level, it is consistent with the correlation and can only be differentiated from the semantic level;
The implementation of UML diagrams uses a line with a solid diamond to represent

The degree of strength of several relationships is: combination > Aggregation > Association > Dependency .

Differences in aggregation and composition:
The aggregation relationship is the "has-a" relationship, the combinatorial relationship is the "contains-a" relationship, and the aggregation relationship indicates that the relationship between the whole and the part is weaker, but the combination is stronger; The object representing part of the aggregation relationship is independent of the lifetime of the object that represents the aggregation thing. Deleting an aggregation object does not necessarily delete the object that represents the part of the thing. Once the composition object is deleted, the object representing part of the thing is deleted as well. We use plain examples to illustrate the differences between aggregation and composition. "Guopoguwang", the country went out, the home naturally did not, "state" and "home" is clearly a combination of relations. On the contrary, the relationship between the computer and its peripherals is the aggregation, because the relationship between them is relatively loose, the computer is gone, the peripherals can exist independently, and can be connected to other computers. In an aggregation relationship, parts can exist independently of the aggregation, and some of the ownership can be shared by several aggregates, such as printers that can be shared by a wide range of colleagues in the Office

The difference between aggregation and composition is different in semantics and implementation, and the combination of two objects has a great correlation between their lifetimes, and the grouped objects are created after the composition object is created, and destroyed before the composite object is destroyed. In general, the combined object cannot be separated from the composition object and can only belong to a composite object, such as a version of a document, and must depend on the existence of the document and only one document. Aggregations are different, objects that are aggregated can belong to multiple aggregation objects, such as an employee may belong to more than one company

Differences between associations and aggregations:
The difference between association and aggregation is mainly semantically, the associated two objects are generally equal, such as you are my friend, aggregation is generally not equal, for example, a company contains a lot of employees, its implementation is similar.

[Reprint] The difference between generalization, implementation, dependency, and association

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.