Four types of relationships between objects

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.


Preface

Relationships between objects: dependencies, associations, aggregations and combinations


1. Dependence

Dependency is the connection between classes. Definition of dependency representation 一个类依赖于另一个类. Use relationship . Generally, dependencies are reflected in the Java language as 局域变量, 方法的形参, or对静态方法的调用

自己想  局部变量 --- 形参 --- 调用另一个类的静态方法

2. Association

Association relationship is the connection between classes, which makes 一个类知道另一个类的属性和方法. Have relationships. Associations can be bidirectional or unidirectional. In the Java language, association relationships are generally 成员变量implemented using .

    所谓关联,某个对象长期持有**另一个对象的引用**
    而且这两者之前的关联是相互的,
    关联的两个对象彼此之间没有任何的强制性约束,
    只要两者同意,可以随时解除关联关系,
    而且它们在生命周期上没有任何约定,
    被关联的对象是共享,它还可以被其他对象关联。
成员变量  

3. Aggregation

Aggregation relationship is a type of association relationship 强的关联关系. Aggregation is the relationship between a whole and an individual . For example, the relationship between the automobile category and the engine category, tire category, and other parts categories is the relationship between the whole and the individual. Like association relationships, aggregation relationships are also implemented through instance variables. However, the two classes involved in the association relationship are on the same level, while in the aggregation relationship, the two classes are on unequal levels, one representing the whole and the other representing the part .


// 母亲
class Mother {
    
    
    // 母亲可以有自己孩子, 但是不确定什么时候生
    private Children myChildren;
}
// 孩子
class Children {
    
    
 
}

4. Composition

The combination relationship is a type of association relationship and is a stronger relationship than the aggregation relationship. It requires that the object representing the whole in an ordinary aggregation relationship is responsible for representing the life cycle of part of the object , and the combination relationship cannot be shared . The object representing the whole needs to be responsible for keeping the part object alive and in some cases annihilating the object responsible for the part. An object representing a whole can pass an object representing a part to another object, which is responsible for the life cycle of this object. In other words, the object representing the part can only be combined with one object at each moment, and the latter is exclusively responsible for the life cycle. Parts have the same life cycle as wholes .

 * 组合关系就是整体与部分的关系,部分属于整体,整体不存在,则部分一定不存在
 * 然后部分不存在整体依然可以生存
 * 部分存在于整体创建之后,部分销毁于整体销毁之前。
public class CompositionTest {
    
    
    /**
     * 菜刀
     */
    static class Kinfe {
    
    
        public static void cutting(String name) {
    
    
            System.out.println("切" + name);
        }
    }
    /**
     *  厨师
     */
    class Chef {
    
    
        Kinfe kinfe = new Kinfe();
        public void cutting(String vegetables) {
    
    
            kinfe.cutting(vegetables);
        }
    }
    public static void main(String[] args) {
    
    
        CompositionTest dependencyTest = new CompositionTest();
        Chef chef = dependencyTest.new Chef();
        chef.cutting("carrot");
    }
}

Summarize

The difference between dependence and association: dependence is a usage relationship, and association is an ownership relationship;

The difference between aggregation and combination: aggregation means that the individual can still exist without the whole; combination means that the individual and the whole cannot be separated, and the individual cannot exist alone without the whole;

The difference between dependence, association and aggregation, combination: dependence, association: the relationship between classes is on the same level; aggregation, combination: the relationship between classes is expressed as a whole and a part.

参考
https://blog.csdn.net/myb19950405/article/details/126709643

Study notes-for study only

Guess you like

Origin blog.csdn.net/weixin_44625361/article/details/127581835