The relationship between classes and classes

Source: Internet
Author: User
Tags dashed line

I. The relationship of succession

Inheritance refers to the ability of a class (called a subclass, sub-interface) to inherit the functionality of another class (called a parent class, parent interface) and to increase its own new functionality. In UML class diagram design, inheritance is represented by a solid line with a hollow triangular arrow, from a subclass to a parent class, or a sub-interface to a parent interface.

Ii. Realization of the relationship

Implementations refer to a class class that implements the interface interface (which can be multiple), and implementation is the most common relationship between classes and interfaces. In the UML class diagram design, the implementation is represented by a dashed line with a hollow triangular arrow, from the class to the implemented interface.

iii. Dependency Relationship

Simply understood, dependency is that a class A uses another Class B, which is accidental, temporary, and very weak, but changes in class B affect Class A. For example, if someone wants to cross the river, they need to borrow a boat, and the relationship between the man and the ship is dependent. At the code level, for Class B as a parameter, Class A is used in a method. In UML class diagram design, a dependency relationship is represented by a dashed line with an arrow pointing to Class B.

Iv. Related Relationships

Correlation is a strong dependency between the semantic levels of the two classes, such as me and my friends, who are more dependent and less dependent on the contingency, the relationship is not temporary, generally long-term, and the relationship between the two are generally equal. Associations can be one-way, two-way. At the code level, the associated class B appears in the association Class A as an attribute of the class, or the association Class A refers to a global variable of the type being associated with Class B. In the UML class diagram design, the association relation is represented by the arrow solid line that the association Class A points to the associated class B, and the roles and multiplicity tags of the two sides can be labeled at both ends of the association.

v. The relationship of aggregation

Aggregation is a special case of association, which embodies the relationship between the whole and the part, that is, the relationship of Has-a. At this point, the whole and the parts are separable, they can have their own life cycle, some can belong to more than one whole object, or can be shared for multiple whole objects. such as computer and CPU, the relationship between the company and employees, such as an aircraft carrier formation including the sea and air mother ship, the protection of ships, ship-borne aircraft and nuclear power attack submarine. Performance at the code level, and the association relationship is consistent, only from the semantic level to differentiate. In the UML class diagram design, the aggregation relationship is represented by a hollow diamond plus a solid line arrow.

vi. combination of relationships

The combination is also a special case of the association relationship, which embodies a contains-a relationship, which is stronger than aggregation, also known as strong aggregation. It also embodies the relationship between the whole and the part, but at this time the whole and the part are not divided, the whole life cycle end also means that part of the life cycle end, such as human and human brain. Performance at the code level, and the association relationship is consistent, only from the semantic level to differentiate. In the UML class diagram design, the composition relationship is represented by a solid diamond plus solid line arrow.

Vii. Summary

There is little doubt as to the inheritance and implementation of the two relationships, which represent a class and class, or a vertical relationship between a class and an interface. The other four kinds of relationships embody the class and class, or the reference between the class and interface, transverse relationship, is more difficult to distinguish, there are many things between the relationship to be accurate positioning is difficult. As mentioned earlier, these four relationships are semantic level, so from the code level does not completely distinguish between the various relationships, but in general, the following relationships are manifested by the degree of strength: Combination > Aggregation > Association > dependency.

Advantages and disadvantages of inheritance and composition

class inheritance and object composition are the two most commonly used techniques for multiplexing.

One: Inheritance

Inheritance is a relationship of is a, for example student inherits person, then student is a person. The advantage of inheritance is that subclasses can override the parent class's methods to easily implement extensions to the parent class.

The disadvantages of inheritance are as follows:

①: The inner details of the parent class are visible to the child class.

②: A method inherited by a subclass from a parent class is determined at compile time, so the behavior of a method inherited from a parent class cannot be changed during run time.

③: If the method of the parent class is modified (for example, if a parameter is added), the method of the subclass must be modified accordingly. So the subclass and the parent class is a high-coupling, which violates the object-oriented thought.

Two: Combination

The combination is the design of the class to add the object of the class to be combined into the class as its own member variable.

Advantages of the combination:

①: The current object can only invoke its method through the contained object, so the inner details of the contained object are not visible to the current object.

②: The current object is a low-coupling relationship with the contained object, and if you modify the code in the class that contains the object, you do not need to modify the code of the current object class.

③: The object that the current object can bind dynamically at run time. You can assign values to the contained objects by using the Set method.

Disadvantages of combination: ①: It is easy to produce too many objects. ②: To be able to combine multiple objects, the interface must be carefully defined.

Thus, the combination is more flexible and more stable than the inheritance, so it is preferred to use the combination at design time. Consider using inheritance only if the following conditions are met:

A subclass is a special type, not just a role of the parent class

An instance of a subclass does not need to become an object of another class

Subclass extension, instead of overwriting or invalidating the function of the parent class

The relationship between classes and classes

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.