In OOP, what is the difference between composition (denoted by filled diamond in UML) and association (denoted by empty diamond in UML) relationship between classes. I'm a bit confused. What is aggregation? Can I have a convincing real world example?
在OOP中,组合(由UML中的填充菱形表示)和类之间的关联(由UML中的空菱形表示)之间的区别是什么。我有点困惑。什么是聚合?我可以有一个令人信服的现实世界的例子吗?
8 个解决方案
#1
COMPOSITION
Imagine a software firm that is composed of different Business Units (or departments) like Storage BU, Networking BU. Automobile BU. The life time of these Business Units is governed by the lifetime of the organization. In other words, these Business Units cannot exist independently without the firm. This is COMPOSITION. (ie the firm is COMPOSED OF business units)
想象一下,一个软件公司由不同的业务单位(或部门)组成,如Storage BU,Networking BU。汽车BU。这些业务单位的生命周期由组织的生命周期决定。换句话说,没有公司,这些业务单位不能独立存在。这是组成。 (即公司是业务部门的组成部分)
ASSOCIATION
The software firm may have external caterers serving food to the employees. These caterers are NOT PART OF the firm. However, they are ASSOCIATED with the firm. The caterers can exist even if our software firm is closed down. They may serve another firm! Thus the lifetime of caterers is not governed by the lifetime of the software firm. This is typical ASSOCIATION
软件公司可能有外部餐饮供应商为员工提供食物。这些餐饮服务商不是公司的一部分。但是,他们与公司有关。即使我们的软件公司关闭,餐饮服务也可以存在。他们可能会为另一家公司服因此,餐饮者的生命周期不受软件公司的生命周期的支配。这是典型的协会
AGGREGATION
Consider a Car manufacturing unit. We can think of Car as a whole entity and Car Wheel as part of the Car. (at this point, it may look like composition..hold on) The wheel can be created weeks ahead of time, and it can sit in a warehouse before being placed on a car during assembly. In this example, the Wheel class's instance clearly lives independently of the Car class's instance. Thus, unlike composition, in aggregation, life cycles of the objects involved are not tightly coupled.
考虑一个汽车制造单位。我们可以将Car视为一个整体,将Car Wheel视为汽车的一部分。 (此时,它可能看起来像组合物。)可以提前几周创建*,它可以在装配前放在汽车上之前放在仓库里。在这个例子中,Wheel类的实例显然独立于Car类的实例。因此,与组合不同,在聚合中,所涉及的对象的生命周期不是紧密耦合的。
#2
Here go a few examples:
这里举几个例子:
-
I am an employee of a company, hence I am associated to that company. I am not part of it, nor do I compose it, but am related to it, however.
我是一家公司的员工,因此我与该公司有联系。我不是它的一部分,也不构成它,但是它与它有关。
-
I am composed of organs, which unless are transplanted, will die with me. This is composition, which is a very strong bind between objects. Basically objects are composed by other objects. The verb says everything.
我是由器官组成的,除非被移植,否则会死于我。这是组合,它是对象之间非常强大的绑定。基本上,对象由其他对象组成。动词说明了一切。
-
There is also another less bound kind of composition, called aggregation. An aggregation is when objects are composed by other objects, but their life cycles are not necessarily tied. Using an extreme example, a Lego toy is an aggregation of parts. Even though the toy can be dismantled, its parts can be recombined to make a different toy.
还有另一种不那么受限制的组合,称为聚合。聚合是当对象由其他对象组成时,但它们的生命周期不一定是绑定的。使用极端的例子,乐高玩具是零件的集合。即使玩具可以拆卸,其部件也可以重新组合以制作不同的玩具。
#3
Owning and using.
拥有和使用。
Composition: the object with the reference owns the object referred to, and is responsible for its "lifetime", its destruction (and often creation, though it may be passed in). Also known as a has-a relationship.
组合:具有引用的对象拥有所引用的对象,并且负责其“生命周期”,它的破坏(并且经常创建,尽管它可以被传入)。也被称为has-a关系。
Association: the object with the reference uses the object referred to, may not be an exclusive user, and isn't responsible for he referred-to object's lifetime. Also known as a uses-a relationship.
关联:带引用的对象使用引用的对象,可能不是独占用户,并且不负责他引用的对象的生命周期。也称为使用 - 关系。
The OP comments:
OP意见:
Can you provide a real world example. Also, what is aggregation? – Marc
你能提供一个真实世界的例子吗?另外,什么是聚合? - 马克
Aggregation: an Association that is from whole to part, and that can't be cyclic.
聚合:从整体到部分,不能循环的协会。
Examples:
Composition: a Car has-an Engine, a Person has-an Address. Basically, must have, controls lifetime.
组成:汽车有一个引擎,一个人有一个地址。基本上,必须有,控制一生。
Association: A Car has-a Driver, some class instance has-an ErrorLogger. Lifetime not controlled, may be shared.
关联:汽车有一个驱动程序,一些类实例有一个错误日志程序。终身不受控制,可以共享。
Aggregation: A DOM (Document Object Model, that is the objects that make up a tree of HTML elements) Node has-a (an array of) child Nodes. The Node is top (well, higher) level; it "contains" its children, they don't contain it.
聚合:DOM(文档对象模型,即构成HTML元素树的对象)节点具有一个(子阵列)子节点。节点是*(好,更高)级别;它“包含”它的孩子,它们不包含它。
#4
I believe that a code-based example can help to illustrate the concepts given by the above responses.
我相信基于代码的示例可以帮助说明上述响应给出的概念。
import java.util.ArrayList;
public final class AssoCia
{
public static void main( String args[] )
{
B b = new B();
ArrayList<C> cs = new ArrayList();
A a = new A( b, cs );
a.addC( new C() );
a.addC( new C() );
a.addC( new C() );
a.listC();
}
}
class A
{
// Association -
// this instance has a object of other class
// as a member of the class.
private B b;
// Association/Aggregation -
// this instance has a collection of objects
// of other class and this collection is a
// member of this class
private ArrayList<C> cs;
private D d;
public A(B b, ArrayList<C> cs)
{
// Association
this.b = b;
// Association/Aggregation
this.cs = cs;
// Association/Composition -
// this instance is responsible for creating
// the instance of the object of the
// other class. Therefore, when this instance
// is liberated from the memory, the object of
// the other class is liberated, too.
this.d = new D();
}
// Dependency -
// only this method needs the object
// of the other class.
public void addC( C c )
{
cs.add( c );
}
public void listC()
{
for ( C c : cs )
{
System.out.println( c );
}
}
}
class B {}
class C {}
class D {}
#5
Usually, composition means that the lifetime of the contained object is bounded by that of the container, whereas association is a reference to an object which may exist independently.
通常,组合意味着所包含对象的生命周期受容器的生命周期限制,而关联是对可独立存在的对象的引用。
However, this is just the practice I've observed. I hate to admit it, but ploughing through the UML2 spec isn't high on my list of fun stuff to do!
然而,这只是我观察到的做法。我讨厌承认这一点,但是在我的有趣的事情列表中,通过UML2规范的努力并不高!
#6
Independent existence.
An Invoice is composed of line items.
发票由订单项组成。
What's a line item that's not on an invoice? It's -- well -- it's nothing. It can't exist independently.
什么是不在发票上的订单项?它 - 嗯 - 没什么。它不能独立存在。
On the other hand, an Invoice is associated with a Customer.
另一方面,发票与客户相关联。
Customer has an independent existence, with or without an invoice.
客户独立存在,有或没有发票。
If the two things have independent existence, they may be associated.
如果这两件事有独立存在,那么它们可能是相关联的。
If one thing cannot exist independently, then it is part of a composition.
如果有一件事不能独立存在,那么它就是作品的一部分。
#7
Composition is a stricter relationship than aggregation. Composition means that something is so strongly related to something else that they cannot basically exist independently, or if they can, they live in different contexts.
组合是一种比聚合更严格的关系。构图意味着某些东西与其他东西如此密切相关,以至于它们基本上不能独立存在,或者如果它们可以,它们就生活在不同的环境中。
Real world example: you define a GUI window, and then a text field where to write something. Between the class defining the GUI and the class defining the text field there's composition. Together, they compose a widget which can be seen as an entity on its own. Suppose you delete the window, and you delete the text field as well.
真实世界的例子:你定义一个GUI窗口,然后在一个文本字段中写一些东西。在定义GUI的类和定义文本字段的类之间有组合。它们共同构成了一个可以单独看作实体的小部件。假设您删除了窗口,并删除了文本字段。
Aggregation is different, in the sense that the link between the two entities is temporary, unstable, and occasional. A real world example. Suppose you have a database of objects containing multiple data instances. Now you run some filter to collect the data instances obeying a given criterium, and the resulting instances are pushed into a graphical list so that the user can see them. When the graphical widget receives the objects, it can form an aggregation of these entities, and present them. If the user closes the window with the graphical list, and the latter get deleted, the data objects should not be deleted. Maybe they are displayed somewhere else, or you still need them.
聚合是不同的,因为两个实体之间的链接是临时的,不稳定的,偶尔的。一个现实世界的例子。假设您有一个包含多个数据实例的对象数据库。现在,您运行一些过滤器来收集服务于给定标准的数据实例,并将生成的实例推送到图形列表中,以便用户可以看到它们。当图形小部件接收到对象时,它可以形成这些实体的聚合,并呈现它们。如果用户使用图形列表关闭窗口,并且后者被删除,则不应删除数据对象。也许它们会显示在其他地方,或者你仍然需要它们。
Also, in general, composition is defined at creation time. Aggregation is instead defined later in the object lifetime.
而且,通常,在创建时定义组成。而是在对象生存期中稍后定义聚合。
#8
Composition means a part of the entity state is encapsulated by another type but it is conceptualy part of the entity state. For example you may have a address type and a employee entity type that includes a address.
组合意味着实体状态的一部分由另一种类型封装,但它是实体状态的概念的一部分。例如,您可能具有地址类型和包含地址的员工实体类型。
Association means that a entity type is assocciated with another entity type but the assocciated entity is conceptualy not part of the entity state. For example a employee may be assocciated with a company.
关联意味着实体类型与另一实体类型相关联,但关联实体概念上不是实体状态的一部分。例如,员工可能与公司相关联。
#1
COMPOSITION
Imagine a software firm that is composed of different Business Units (or departments) like Storage BU, Networking BU. Automobile BU. The life time of these Business Units is governed by the lifetime of the organization. In other words, these Business Units cannot exist independently without the firm. This is COMPOSITION. (ie the firm is COMPOSED OF business units)
想象一下,一个软件公司由不同的业务单位(或部门)组成,如Storage BU,Networking BU。汽车BU。这些业务单位的生命周期由组织的生命周期决定。换句话说,没有公司,这些业务单位不能独立存在。这是组成。 (即公司是业务部门的组成部分)
ASSOCIATION
The software firm may have external caterers serving food to the employees. These caterers are NOT PART OF the firm. However, they are ASSOCIATED with the firm. The caterers can exist even if our software firm is closed down. They may serve another firm! Thus the lifetime of caterers is not governed by the lifetime of the software firm. This is typical ASSOCIATION
软件公司可能有外部餐饮供应商为员工提供食物。这些餐饮服务商不是公司的一部分。但是,他们与公司有关。即使我们的软件公司关闭,餐饮服务也可以存在。他们可能会为另一家公司服因此,餐饮者的生命周期不受软件公司的生命周期的支配。这是典型的协会
AGGREGATION
Consider a Car manufacturing unit. We can think of Car as a whole entity and Car Wheel as part of the Car. (at this point, it may look like composition..hold on) The wheel can be created weeks ahead of time, and it can sit in a warehouse before being placed on a car during assembly. In this example, the Wheel class's instance clearly lives independently of the Car class's instance. Thus, unlike composition, in aggregation, life cycles of the objects involved are not tightly coupled.
考虑一个汽车制造单位。我们可以将Car视为一个整体,将Car Wheel视为汽车的一部分。 (此时,它可能看起来像组合物。)可以提前几周创建*,它可以在装配前放在汽车上之前放在仓库里。在这个例子中,Wheel类的实例显然独立于Car类的实例。因此,与组合不同,在聚合中,所涉及的对象的生命周期不是紧密耦合的。
#2
Here go a few examples:
这里举几个例子:
-
I am an employee of a company, hence I am associated to that company. I am not part of it, nor do I compose it, but am related to it, however.
我是一家公司的员工,因此我与该公司有联系。我不是它的一部分,也不构成它,但是它与它有关。
-
I am composed of organs, which unless are transplanted, will die with me. This is composition, which is a very strong bind between objects. Basically objects are composed by other objects. The verb says everything.
我是由器官组成的,除非被移植,否则会死于我。这是组合,它是对象之间非常强大的绑定。基本上,对象由其他对象组成。动词说明了一切。
-
There is also another less bound kind of composition, called aggregation. An aggregation is when objects are composed by other objects, but their life cycles are not necessarily tied. Using an extreme example, a Lego toy is an aggregation of parts. Even though the toy can be dismantled, its parts can be recombined to make a different toy.
还有另一种不那么受限制的组合,称为聚合。聚合是当对象由其他对象组成时,但它们的生命周期不一定是绑定的。使用极端的例子,乐高玩具是零件的集合。即使玩具可以拆卸,其部件也可以重新组合以制作不同的玩具。
#3
Owning and using.
拥有和使用。
Composition: the object with the reference owns the object referred to, and is responsible for its "lifetime", its destruction (and often creation, though it may be passed in). Also known as a has-a relationship.
组合:具有引用的对象拥有所引用的对象,并且负责其“生命周期”,它的破坏(并且经常创建,尽管它可以被传入)。也被称为has-a关系。
Association: the object with the reference uses the object referred to, may not be an exclusive user, and isn't responsible for he referred-to object's lifetime. Also known as a uses-a relationship.
关联:带引用的对象使用引用的对象,可能不是独占用户,并且不负责他引用的对象的生命周期。也称为使用 - 关系。
The OP comments:
OP意见:
Can you provide a real world example. Also, what is aggregation? – Marc
你能提供一个真实世界的例子吗?另外,什么是聚合? - 马克
Aggregation: an Association that is from whole to part, and that can't be cyclic.
聚合:从整体到部分,不能循环的协会。
Examples:
Composition: a Car has-an Engine, a Person has-an Address. Basically, must have, controls lifetime.
组成:汽车有一个引擎,一个人有一个地址。基本上,必须有,控制一生。
Association: A Car has-a Driver, some class instance has-an ErrorLogger. Lifetime not controlled, may be shared.
关联:汽车有一个驱动程序,一些类实例有一个错误日志程序。终身不受控制,可以共享。
Aggregation: A DOM (Document Object Model, that is the objects that make up a tree of HTML elements) Node has-a (an array of) child Nodes. The Node is top (well, higher) level; it "contains" its children, they don't contain it.
聚合:DOM(文档对象模型,即构成HTML元素树的对象)节点具有一个(子阵列)子节点。节点是*(好,更高)级别;它“包含”它的孩子,它们不包含它。
#4
I believe that a code-based example can help to illustrate the concepts given by the above responses.
我相信基于代码的示例可以帮助说明上述响应给出的概念。
import java.util.ArrayList;
public final class AssoCia
{
public static void main( String args[] )
{
B b = new B();
ArrayList<C> cs = new ArrayList();
A a = new A( b, cs );
a.addC( new C() );
a.addC( new C() );
a.addC( new C() );
a.listC();
}
}
class A
{
// Association -
// this instance has a object of other class
// as a member of the class.
private B b;
// Association/Aggregation -
// this instance has a collection of objects
// of other class and this collection is a
// member of this class
private ArrayList<C> cs;
private D d;
public A(B b, ArrayList<C> cs)
{
// Association
this.b = b;
// Association/Aggregation
this.cs = cs;
// Association/Composition -
// this instance is responsible for creating
// the instance of the object of the
// other class. Therefore, when this instance
// is liberated from the memory, the object of
// the other class is liberated, too.
this.d = new D();
}
// Dependency -
// only this method needs the object
// of the other class.
public void addC( C c )
{
cs.add( c );
}
public void listC()
{
for ( C c : cs )
{
System.out.println( c );
}
}
}
class B {}
class C {}
class D {}
#5
Usually, composition means that the lifetime of the contained object is bounded by that of the container, whereas association is a reference to an object which may exist independently.
通常,组合意味着所包含对象的生命周期受容器的生命周期限制,而关联是对可独立存在的对象的引用。
However, this is just the practice I've observed. I hate to admit it, but ploughing through the UML2 spec isn't high on my list of fun stuff to do!
然而,这只是我观察到的做法。我讨厌承认这一点,但是在我的有趣的事情列表中,通过UML2规范的努力并不高!
#6
Independent existence.
An Invoice is composed of line items.
发票由订单项组成。
What's a line item that's not on an invoice? It's -- well -- it's nothing. It can't exist independently.
什么是不在发票上的订单项?它 - 嗯 - 没什么。它不能独立存在。
On the other hand, an Invoice is associated with a Customer.
另一方面,发票与客户相关联。
Customer has an independent existence, with or without an invoice.
客户独立存在,有或没有发票。
If the two things have independent existence, they may be associated.
如果这两件事有独立存在,那么它们可能是相关联的。
If one thing cannot exist independently, then it is part of a composition.
如果有一件事不能独立存在,那么它就是作品的一部分。
#7
Composition is a stricter relationship than aggregation. Composition means that something is so strongly related to something else that they cannot basically exist independently, or if they can, they live in different contexts.
组合是一种比聚合更严格的关系。构图意味着某些东西与其他东西如此密切相关,以至于它们基本上不能独立存在,或者如果它们可以,它们就生活在不同的环境中。
Real world example: you define a GUI window, and then a text field where to write something. Between the class defining the GUI and the class defining the text field there's composition. Together, they compose a widget which can be seen as an entity on its own. Suppose you delete the window, and you delete the text field as well.
真实世界的例子:你定义一个GUI窗口,然后在一个文本字段中写一些东西。在定义GUI的类和定义文本字段的类之间有组合。它们共同构成了一个可以单独看作实体的小部件。假设您删除了窗口,并删除了文本字段。
Aggregation is different, in the sense that the link between the two entities is temporary, unstable, and occasional. A real world example. Suppose you have a database of objects containing multiple data instances. Now you run some filter to collect the data instances obeying a given criterium, and the resulting instances are pushed into a graphical list so that the user can see them. When the graphical widget receives the objects, it can form an aggregation of these entities, and present them. If the user closes the window with the graphical list, and the latter get deleted, the data objects should not be deleted. Maybe they are displayed somewhere else, or you still need them.
聚合是不同的,因为两个实体之间的链接是临时的,不稳定的,偶尔的。一个现实世界的例子。假设您有一个包含多个数据实例的对象数据库。现在,您运行一些过滤器来收集服务于给定标准的数据实例,并将生成的实例推送到图形列表中,以便用户可以看到它们。当图形小部件接收到对象时,它可以形成这些实体的聚合,并呈现它们。如果用户使用图形列表关闭窗口,并且后者被删除,则不应删除数据对象。也许它们会显示在其他地方,或者你仍然需要它们。
Also, in general, composition is defined at creation time. Aggregation is instead defined later in the object lifetime.
而且,通常,在创建时定义组成。而是在对象生存期中稍后定义聚合。
#8
Composition means a part of the entity state is encapsulated by another type but it is conceptualy part of the entity state. For example you may have a address type and a employee entity type that includes a address.
组合意味着实体状态的一部分由另一种类型封装,但它是实体状态的概念的一部分。例如,您可能具有地址类型和包含地址的员工实体类型。
Association means that a entity type is assocciated with another entity type but the assocciated entity is conceptualy not part of the entity state. For example a employee may be assocciated with a company.
关联意味着实体类型与另一实体类型相关联,但关联实体概念上不是实体状态的一部分。例如,员工可能与公司相关联。