用Java实现组合 [英] Implementing Composition in Java
问题描述
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.
推荐答案
那还不够。
处于构图关系中,如果整个
实例被销毁,则 part
实例也应立即销毁。
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.
例如,如果您从类的外部推送 Chapter
s的实例(例如,使用构造函数) ,删除 Book
实例时,应小心删除这些实例。
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.
如果您的实例是在 Book
类(通过new ...),无需执行任何操作,您的 Chapter
实例将用 Book
实例删除。
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.
在本参考中:Object Prime,第三版(Scott W. Ambler,2004年)
部分( 13.4.12.7实现组成)
In this reference: Object Prime, Third Edition (by Scott W. Ambler, 2004)
in section (13.4.12.7 Implementing Composition)
您可能已经猜到,聚合和组成关联
的处理方式与关联完全相同。从编程的角度来看,
的主要区别在于聚合意味着两个类之间的
关系比关联更紧密,而
组成意味着关系仍然更加紧密。尽管图
13.11不包括构词关联,但是Seminar和Course之间的关联很紧密,实际上,至少和您
在构词中看到的紧密一样(不幸的是,句子规则不包含
在这种情况下有意义)。在图13.31中,您可以在课程
类中的remove()方法的实现中看到该
紧密度的结果-当删除课程时,其研讨会也将被删除。 这种
类型的生命周期管理代码在组合中很常见
层次结构。
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);
因此,在这段代码中,我们可以设置一个 Brain的实例
分配给两个不同的人员
。
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.
例如下面的示例可以是一个合成。因为删除整个
时删除了 Part
的实例,所以:
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();
}
}
组成: < br>
整个
可能直接负责创建或销毁部分
。或者,它可以使用已经从类外部(通过代码的其他部分)创建和管理的部分。在这种情况下,部分
的删除应由外部代码管理,并且部分
应在<$之后立即删除c $ c>整个删除。
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.
这篇关于用Java实现组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!