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.
The composition is achieved by using an instance variable that refers to other objects. If an object contains the other object and the contained object cannot exist without the existence of that object, then it is called composition.
A composition in Java between two objects associated with each other exists when there is a strong relationship between one class and another. Other classes cannot exist without the owner or parent class. For example, A 'Human' class is a composition of Heart and lungs.
Composition is one of the fundamental concepts in object-oriented programming. It describes a class that references one or more objects of other classes in instance variables. This allows you to model a has-a association between objects. You can find such relationships quite regularly in the real world.
Composition in java is the design technique to implement has-a relationship in classes. We can use java inheritance or Object composition in java for code reuse.
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 Chapter
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With