A simple understanding of association and dependency, combination and aggregation

Source: Internet
Author: User
  1. The relationship between classes indicates the strong relationship between classes, and dependency indicates the weak relationship between classes;
  2. From the point of view of the time between classes, Association indicates the "persistent" Relationship between classes. This relationship generally indicates an important relationship between businesses and needs to be saved, or "persistence" is required, or it needs to be stored in the database. For example, student and class in the student management system, which class A student object belongs to is an important business relationship. If this relationship is not saved, the system cannot manage it. In addition, dependency indicates a "temporary and temporary" Relationship between classes, which does not need to be saved, such as student class and stueditscreen (student logon interface) A dependency between classes. The stueditscreen class depends on the student class and displays and edits student information based on the student object information.
  3. The relationship between the design classes follows the principle: first, judge whether there is a "Link" between the classes. If it is not a "dependency", if it is not an association, is dependency
  4. Dependency is generally one of the following situations: A. classa has a classb parameter type, which becomes coupling; b. The parameter type of a method in classa is a property of classb. This situation becomes tightly coupled. The implementation of a method in C and classa instantiates classb; d. the type of the return value of a method in classa is classb. If one of the above four situations occurs, the two classes may be dependent.
  5. Dependency: it is the connection between classes, and dependency is always one-way. Dependency indicates that one class depends on the definition of another class. In the following example, Class A depends on Class B, C, and D.

    Java code
    1. PublicClassA {
    2. PublicBGetb (CC,DD ){
    3. EE=NewE ();
    4. BB=NewB (C,D,E );
    5. }
    6. }

  6. Association is a structural relationship, indicating that the object of one thing is associated with the object of another thing. Given a connection to two types of associations, you can navigate from one class object to another class object.
    The association can have a direction, that is, navigation. Generally, the navigation is bidirectional and no arrows need to be marked online. In most cases, navigation is unidirectional and can be indicated by an arrow. Association is generally represented as an attribute (member variable) in the Code. For example, in the following example, Class A is associated with Class B.

    Java code
    1. PublicClassA {
    2. PrivateBB;
    3. }

    If B is also associated with a, they are two-way associations.

    Java code
    1. PublicClass B {
    2. Private;
    3. }
  7. Aggregation: a type of strong association. Aggregation is the relationship between the whole and the individual. For example, the relationship between the automobile and the engine type is the relationship between the whole and the individual.

    Like the association relationship, the aggregation relationship is also implemented through instance variables.

    The two classes involved in an association are at the same level. In an aggregation relationship, the two classes are at an unequal level. One represents the whole, and the other represents the part. (Association and aggregation are only syntactically inseparable. You need to check the logical relationship between involved classes .)

  8. Composition: a type of association, which is stronger than an aggregation relationship. It requires that the objects represented in common aggregation relationships are responsible for representing the lifecycle of some objects, and the compositing relationships cannot be shared.

    The whole object is responsible for keeping the object alive. In some cases, it is responsible for destroying some objects. An object that represents the whole can pass objects that represent some parts to another object, and the latter is responsible for the lifecycle of this object. In other words, it means that some objects can only have a composite relationship with one object at each time point, and the latter is responsible for its lifecycle.

    For example, the relationship between geese and geese is an aggregation relationship, and the relationship between geese and wings is a combination relationship.

    The aggregate link class contains another class as the parameter.

    In the goosegroup constructor, goose is used as a parameter to pass in the value. Goose can exist independently of the geese.

    Classes in the composite relationship contain the instantiation of another class.

    Before instantiating goose, you must first instantiate the wings class (WINGS). The two classes are closely coupled. They have the same life cycle wings class (WINGS) it cannot exist independently from Goose

    Different information Encapsulation

    In the aggregation relationship, the client can understand both the geese and geese at the same time, because they are independent
    In the composite relationship, the client only recognizes the geese and does not even know the existence of the wings, because the wings are tightly encapsulated in the geese.

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.