Implementing Composition in Java

此生再无相见时 提交于 2020-05-13 04:18:22

问题


class Book {
    private Chapter[] chapters = new Chapter[5];
 }

class Chapter {
    private Book book;
}

Is this the correct way to implement the above relationship? I need explanation on this. Thanks.


回答1:


That is not enough.

In Composition relationship, If whole instance is destroyed, the part instance should also be destroyed immediately.

You should have some codes (some machanisms) to do that.

For example if you push the instances of Chapters from external the class (for example by using a constructor), you should be careful to delete those instances when the Book instance is deleted.

If your instances are created within the Book class (by new ...), there is no need to do something and your Chapter instances will be deleted with Book instance.

In this reference: Object Prime, Third Edition (by Scott W. Ambler, 2004)
in section (13.4.12.7 Implementing Composition)

As you might have guessed, aggregation and composition associations are handled exactly the same way as associations. The main difference, from a programming point-of-view, is aggregation implies a tighter relationship between the two classes than association does, and composition implies an even tighter relationship still. Although Fig. 13.11 does not include composition associations, the association between Seminar and Course is tight, in fact, at least as tight as you would see with composition (unfortunately the sentence rule does not make sense in this case). In Fig. 13.31, you see the result of this closeness in the implementation of the remove() method in the Course class—when a course is removed, its seminars are also removed. This type of lifecycle management code is typical within composition hierarchies.

/**
 * Remove a course
 *
 * @postcondition The course and its seminars will be removed
 */
public void remove() 
{
  if (getSeminars() != null) {
    // Clone the original set because we can't safely remove
    // the items from the set while we iterate over it
    HashSet set = (HashSet) getSeminars().clone();
    Iterator iterator = set.iterator();
    // Remove each seminar of this course
    while (iterator.hasNext()) {
      Seminar seminar = (Seminar) iterator.next();
      // Remove the seminar from the collection
     getSeminars().remove(seminar);
    }
  }
  // Remove the instance from permanent storage
  // Persistence code ...
}


Consider this example:

class Person {
   private final Brain brain;
   Person(Brain humanBrain) {
      brain = humanBrain;
   }
}

And in other parts of code we can define like this:

Brain b = new Brain(); 
       // or we have an instance of Brain in other scopes
       // not exactly in this scope

Person p1 = new Person(b);
Person p2 = new Person(b);

So, in this code, we can set one instance of Brain to two different Persons.

Note: In composition, we should manage the life cycle of instances. Only defining a private final of any class, do not show the Composition between them.

For example the below example can be a Composition. Because instances of Part deleted when the whole is deleted:

public class House {    
   private final Room room;

   public House() {    
       room = new Room();
   }
}

In Composition:
The whole may directly be responsible for creation or destruction of the part. Or it may use a "part" that has been already created and managed from external of class (by other parts of code). In this case, deletion of part should be managed by external code and the part should be deleted immediately after the whole deletion.

We should establish a mechanism to delete part when the whole is deleted. If we do not delete the part and use it in other wholes it is Aggregation or Association.



来源:https://stackoverflow.com/questions/48338288/implementing-composition-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!