Java-aggregation and composition

Aggregation and composition in Java

1. Concept

1. Composition (Composition) represents the relationship of'Part-od': the parent class has subclasses, and the subclasses cannot exist independently of the parent

The part and the whole are innate, and the existence of the part depends on the whole. It means that there is a strong relationship between the whole and the part between the two objects, and the life cycle of the part cannot surpass the whole, or it cannot exist without the whole. The "parts" of the combined relationship cannot be shared among the whole.
For example: a certain organ of a person, once a person is born, the organ is there, and if a person dies, the organ has no meaning.

2. Aggregation represents the relationship of'Has-a': the parent class contains subclasses, and the subclasses can exist independently of the parent class, which
means that there is a weak relationship between the whole and part of the two objects, and the life cycle of part can Beyond the whole.
For example: you and your computer (or other items), the computer belongs to you, but you don’t own the computer at birth, the computer is produced by a certain manufacturer, and then you buy it and become part of you . After you die, the computer can be given to others to continue using! This is not called its survival, so this is aggregation.

Two, the difference

The life cycles of the two classes in the aggregation relationship are not synchronized, it is an aggregation relationship; the life cycles of the two classes in the composition relationship are synchronized; eg: aggregation relationship, when A is created, B may not be created; when A dies, B does not necessarily die. The class A{ private B;} class B{...} combination relationship. When an A object is created, a B object is also created; when the A object dies, the B object that is an attribute of A will also die. class A{private B b=new B();…}class B{…}

Three, for example

1. Combination

public class Person {
    
    
    Brain brain;
    public Person(){
    
    //组合
        brain=new Brain();
    }
 
    public static void main(String[] args) {
    
    
        Person person=new Person();
    }
 
}
class Brain{
    
    
 
 }

2. Aggregation

/**
 * Created by Administrator on 2017/10/27.
 */
public class Human {
    
    
    Computer computer;
    Human(Computer computer1){
    
    
        computer=computer1;
    }
 
    public static void main(String[] args) {
    
    
        Computer c=new Computer();//聚合
        Human h=new Human(c);
    }
}
class Computer{
    
    
 
}

However, whether an association is aggregation or combination needs to be determined based on actual business requirements. Why do you say that, because it may be the same superclass and subclass, in different business scenarios, the association relationship will change.
Take taxi software as an example.
If the company stipulates that drivers must have their own car to carry passengers when they enter the station, then Driver and Car are combined.

public class Driver {
    
    

    public Car car;

    public Driver() {
    
    //由司机自己提供
        car = new Car();
    }

    public void drive() {
    
    
        car.start();
    }

}

If the company stipulates that as long as the driver can drive, the company’s various vehicles are at your disposal to pick up passengers. At this time, Driver and Car are in an aggregate relationship

public class Driver {
    
    

    public Car car;

    public void setCar(Car car){
    
    //由公司提供,什么车我们不清楚
        this.car = car;
    }

    public void drive() {
    
    
        car.start();
    }

}

Continuously updating...
Article reference learning address: https://blog.csdn.net/mikimoto_/article/details/103029474

Guess you like

Origin blog.csdn.net/weixin_44325444/article/details/108325052