UML Class Diagram Details

Source: Internet
Author: User

Class diagram is the relationship between classes and classes, is used to deduce code design, later used to optimize the structure of the code, looking for design unreasonable. On-line interpretation of Class diagram and class diagram of the drawing, often expressed very obscure, or examples of rigid, difficult to concrete operation.

Class diagrams generally appear in the detailed design process and are mainly used to describe the relationships among the classes in each module of the system. In other words, after reading the class diagram, you should be able to visualize the implementation of the code in general. Can do this, can say to see the class diagram. The relationship between classes is generally divided into 3 kinds, namely, dependency, correlation and inheritance. First, the diagram of each relationship is shown:

Dependency, the Wikipedia explanation is that the weak connection between classes and classes, only at a certain point in time, has a call relationship. From the code implementation point of view, generally divided into three kinds: the object as a method parameter, the object as a local variable, call the static method of the class. The class that is called within the parentheses of a class function is dependent on itself. Because this relationship is weakly connected, from the point of view of high cohesion and low coupling, such code is optimally organized, whether it is object-oriented or process-oriented.

There is a special phenomenon in dependence, that is, interdependence. In the face of dependency interpretation, interdependence can be understood as: A-class function can invoke local variables of Class B functions, and vice versa. A more general scientific name should be closures. While support for dynamic languages is different, Java does support the anonymous inner class. This structure has the advantage of handling callbacks. In other words: You see that there are interdependent, no-arrows dashed lines in the class diagram, and we can conclude that there is a callback relationship.

An association is a strong connection relationship in which the code behaves as a member variable of another class as an object of one class. The association has two common associations: composition and aggregation. These two differences are very obvious: in Class A There is a member variable Class B instance B, if the life cycle of B is always within the Class A, then a is associated with B, and is a combinatorial relationship. If Class A generates B and passes it to Class C, then Class B is associated with a,c and is an aggregation relationship because the life cycle of B is no longer mastered by a alone.

Find an online map (figure (2)) and get a little bit of the difference.

In fact, I think these two situations are not easily confused when they are really coded, because they have different roles. The combined approach is primarily used for code reuse, where competitors are inherited and used for class construction. The aggregation method is mainly used for similar factory patterns, generating some instances conforming to specifications, filling in attributes, and using them for other classes for code runtime.

Inheritance, unlike associations and dependencies, is the way that new classes are created according to the types of existing classes. From the line is divided into general and implementation. Common we use generalization, while the use of implementations usually emphasizes that this is a final class. In the Java programming idea, there is a classic judgment as to whether inheritance or composite code should be used: if you need to move up from a new class to a base class, inherit if necessary, or you should use a combination.

In summary, in order to make the code high-cohesion low-coupling, this 3 way, the dependence is better than the association is better than inheritance. In terms of multiplexing classes, the combination is more flexible than inheritance.

UML Class Diagram Details

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.