Java 聚合、关联和组合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4324704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Aggregation, Association and Composition
提问by Bukocen
I have such a simple example:
我有一个简单的例子:
public class Order
{
private ArrayList<Product> orders = new ArrayList<Product>();
public void add(Product p)
{
orders.add(p);
}
}
Is it aggregation or composition? I guess it's composition, because orders will be delated after delete of Order, right? Unfortunately it was a task and answer was different;/ Do you know why?
是聚合还是组合?我猜是组合,因为删除订单后订单会延迟,对吗?不幸的是,这是一项任务,答案是不同的;/你知道为什么吗?
second problem:
第二个问题:
public class Client extends Person
{
String adress = "";
Orders orders = new Orders();
public Client(String n, String sn)
{
name = n;
surName = sn;
}
public String getAddress()
{
return adress;
}
public Orders getOrders()
{
return this.orders;
}
}
Is it Association between Client and Orders? My teacher told me that this is association, but I was wondering why it's not a aggregation/composition - he told me that aggregation or composition occur only when one class contains few instances of different class - is that right? I guess not, because e.g. car contains ONE wheel and its aggregation I guess?
它是客户和订单之间的关联吗?我的老师告诉我这是关联,但我想知道为什么它不是聚合/组合——他告诉我聚合或组合只有当一个类包含几个不同类的实例时才会发生——对吗?我猜不是,因为例如汽车包含一个轮子,我猜是它的聚合?
What type of relation is that and why?
那是什么类型的关系,为什么?
回答by lalit
In Java truly speaking there is no composition, everything is an aggregation. The composition can exist in language like C++ where the objects can be declared on stack and live and die with the parent object. In Java, everything lives on heap.
在 Java 中真正说没有组合,一切都是聚合。组合可以存在于像 C++ 这样的语言中,其中对象可以在堆栈上声明并与父对象一起生存和死亡。在 Java 中,一切都存在于堆中。
For your second question, Order has an aggregation relationship with Product. The association relationship can be when we pass order as an argument to the methods in product. Product does the job with the passed reference but does not caches that reference in some child reference.
对于您的第二个问题,Order 与 Product 有聚合关系。关联关系可以是当我们将 order 作为参数传递给 product 中的方法时。Product 使用传递的引用来完成这项工作,但不会将该引用缓存在某个子引用中。
回答by Bill the Lizard
Your first example is aggregation. The variable orders
might be deleted when the Order instance is deleted, but each Product still has meaning and can exist outside the Order class.
你的第一个例子是聚合。orders
删除 Order 实例时,变量可能会被删除,但每个 Product 仍然有意义并且可以存在于 Order 类之外。
You're right in your second example. Because Client contains a (has-a) reference to Orders, this is composition (because orders
doesn't exist without a Client
).
你在你的第二个例子中是对的。因为 Client 包含对 Orders 的 (has-a) 引用,所以这是组合(因为orders
没有 a 就不存在Client
)。
Update to address your comment:
更新以解决您的评论:
Aggregation and composition are both different types of association, but they're specifictypes of association. In order for two classes to have just an association withoutaggregation or composition, they need a weaker link than the example given. Here's a (contrived) example:
聚合和组合都是不同类型的关联,但它们是特定类型的关联。为了让两个类只有一个没有聚合或组合的关联,它们需要一个比给出的例子更弱的链接。这是一个(人为的)示例:
class A {
String phrase = "These pretzels are making me thirsty.";
public String process(B b) {
// use a B object to do something
String tmp = b.doSomething(phrase);
// do more processing...
return tmp;
}
}
class B {
public String doSomething(String s) {
// do something with the input string and return
...
}
}
Here there is no composition or aggregation (A does not have it's own reference to a B object), but since an instance of B is used bya method in A, there is an association.
这里不存在组合物或聚集(A不具有它自己的参照B对象),但由于B的一个实例是使用由所述的一个方法,存在的关联。
回答by Bukocen
@Bill the Lizard
@比尔蜥蜴
Thank you for you explanation, but in your example of association there is still no reference/pointer in the one class to another...
感谢您的解释,但是在您的关联示例中,一个类中仍然没有指向另一个类的引用/指针......
I asked whether it's possible to have in class A field: B instanceOfB = new B() and tell that A and B are in Association (but not aggregation, nor composition!)
我问是否可以在 A 类字段中使用: B instanceOfB = new B() 并告诉 A 和 B 在关联中(但不是聚合,也不是组合!)
I am thinking about it, cause according what is said here it's not possible but... I found here: Difference between association, aggregation and compositionsuch example:
我正在考虑,因为根据这里所说的,这是不可能的,但是......我在这里找到:Difference between association,aggregation and composition这样的例子:
[Example:]
|A|----------->|B|
class A
{
private:
B* itsB;
};
This is given as an example of association and there is pointer to some other class... (I guess we can more or less treat reference in Java class in the same way we treat this pointer here) So in Java it would look like:
这是作为关联示例给出的,并且有指向其他类的指针......(我想我们或多或少可以像对待这里指针一样对待 Java 类中的引用)所以在 Java 中它看起来像:
[Example:]
|A|----------->|B|
class A
{
private:
B itsB;
};
does it mean we can have association in the example above in some cases? Is the difference in the way we think about classes? If A is car and B is wheel it would be aggregation, as car has wheel. If A is e.g. Person and B is e.g. Country it would be association, because Person doesn't have a country, but we maybe want to remember the name of the Country this person recently visited? Or if in class A there is something like "B itsB;" it means ALWAYS that this is aggregation or composition, no matter how we think about these classes?
这是否意味着在某些情况下我们可以在上面的示例中进行关联?我们思考课程的方式有什么不同吗?如果 A 是汽车,B 是轮子,那将是聚合,因为汽车有轮子。如果A 是例如Person 而B 是例如Country 那将是association,因为Person 没有国家,但我们可能想记住这个人最近访问过的国家的名称?或者,如果在 A 类中有类似“B itsB;”的东西。这意味着无论我们如何看待这些类,这始终是聚合或组合?
Is my way of thinking correct or am I talking bullshit, which is highly probable, cause I am just guessing ;)
我的思维方式正确还是我在胡说八道,这很有可能,因为我只是在猜测;)
回答by Ray Tayek
both aggregation and composition are associations. aggregation implies a whole/part relationship. composition is aggregation with a lifetime responsibility: http://ootips.org/uml-hasa.html
聚合和组合都是关联。聚合意味着整体/部分关系。组合是具有终生责任的聚合:http: //ootips.org/uml-hasa.html
in your example, the answer is probably aggregation since there is a whole/part relationship.
在您的示例中,答案可能是聚合,因为存在整体/部分关系。
normally your order would have line-items (as opposed to just products) and these line items would be considered composition.
通常,您的订单会包含订单项(而不仅仅是产品),而这些订单项将被视为组合。
回答by RamjeeAnna
I think, the first example is not aggregation but composition. Because here Order composes the Product. If order is deleted then product will be deleted automatically.
我认为,第一个例子不是聚合而是组合。因为这里的订单构成了产品。如果订单被删除,则产品将被自动删除。
class Product;
class Factory {
Product *product;
product = new Product();
product createProduct() {
return new(product);
}
};
class Product {
ArrayList<Order> *orders = new ArrayList<Order>();
void createOrder() {
orders.add(OrderInfomation);
}
}
class Order {
string orderID;
string orderName;
Customer customer;
};
class Customer {
string cusID;
string cusName;
string cusAddress;
};
Here Product can have same order type. And if product is deleted, then order will be deleted. So it is a composition. (highly strongly coupled or death relation)
这里的产品可以有相同的订单类型。如果产品被删除,那么订单将被删除。所以它是一个组合。(高度强耦合或死亡关系)
In your case, Order associates with product. One order can have any product order. So it is a association. If a order is deleted then a product will exists. (lightly coupled) Similarly customer and order has association relationship.
在您的情况下,订单与产品相关联。一个订单可以有任何产品订单。所以它是一个协会。如果订单被删除,则产品将存在。(轻耦合) 同样customer和order也有关联关系。
Factory and Product are aggregated. Even product deleted, Factory will exist.
工厂和产品是聚合的。即使删除了产品,工厂也会存在。