Class relationships in Java object-oriented design

Source: Internet
Author: User

the relationships between classesType:Generalization (Generalization),Dependency (Dependent Relationships),Association (Association Relationship),Aggregation (Aggregation Relationships),Composition (Combinatorial Relationships) .
generalization ( generalization ) is represented as an inheritance or implementation relationship (is a). The concrete form is the inheritance relation between the class and the class, the inheritance relation between the interface and the interface, and the implementation relation of the class to the interface.
inheritance, implementation of the relationship is relatively clear, and several other dependencies, related relationships to be combined with semantics (specific context) to understand. Dependency (Dependency) Relationshipis a join between classes and classes. A dependency represents a class that relies on the definition of another class. For example, a person can buy a car and a house, and the People class relies on the definition of the car class and the houses class, because it refers to car and home. Unlike associations, the person class does not have properties for car and house types, and car and house instances are passed to the Buy () method in a parametric way. In general, dependencies are represented in the Java language as local variables, method parameters, or calls to static methods. Association (association) Relationshipis a join between a class and a class that enables a class to know the properties and methods of another class. Associations can be bidirectional or unidirectional. In the Java language, association relationships are typically implemented using member variables. Aggregation (Aggregation) Relationshipsis a kind of association relation, is a strong association relation. Aggregation is the relationship between the whole and the individual. For example, the relationship between a car and an engine, a tyre, and other parts of a class is the relationship between the whole and the individual. As with associative relationships, aggregation relationships are also implemented through instance variables. However, the two classes involved in the association relationship are at the same level, while in the aggregation relationship, two classes are at the unequal level, one represents the whole and the other represents the part. combination (composition) Relationshipis a kind of correlation relation, is stronger than the aggregation relationship. It requires that an object representing the whole in an ordinary aggregation relationship is responsible for representing the life cycle of a part of the object, and the combined relationship cannot be shared. The object representing the whole needs to be responsible for maintaining part of the object and surviving, and in some cases, the object responsible for representing the part is annihilated. An object representing a whole can pass an object representing a part to another object, which is responsible for the life cycle of the object. In other words, the object representing a part can only be combined with one object at a time, and the latter is exclusively responsible for the life cycle. Part is the same as the whole life cycle. Summary:From correlation to the association between the relationship between the class of gradually stronger, dependent class modification on the dependency class is more and more important, the coupling between the two classes is more and more high. The distinction of several relationships should be based on the specific semantic environment, which is simple from the code level: Class A relies on class B to withdraw to a static method that contains a class B object or a class B in a local variable or method parameter in a, and a, B association relationship embodies a member variable of type B; The Class B aggregation relationship is embodied in Class A has a member variable of Class B and has a constructor passed into Class B object as a parameter, the Class A and group B are represented as member variables with type a B, and the Class A constructor has an instantiation of type B member variable, and the life cycle of Class A object strictly determines the life cycle of Class B member variable. To avoid bidirectional dependencies at design time, you should try to solve the bidirectional dependency method:The first approach is to translate, for example A and b depend on each other, then you can move the part of B that depends on a to project B, so that B does not need to continue to rely on a, as long as a depends on B, so as to eliminate the cyclic dependencyThe second approach is to move down, for example A and b depend on each other, and they all rely on C, then you can move the part B and a interdependent code to the project C, so that A and B are not dependent on each other, only continue to rely on C, but also to eliminate the cyclic dependency.



Source: >  

From for notes (Wiz)

Class relationships in Java object-oriented design

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.