Java 代码中的聚合和组合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20304116/
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 and Composition in Java Code
提问by Nirav Kamani
As the Aggregation and Composition is related Association or we can say it gives the understanding of relationship between object or anything else.
由于聚合和组合是相关联的,或者我们可以说它给出了对对象或其他任何事物之间关系的理解。
I posted this question because i asked one question in Interview that what is the Composition and Aggregation.
我发布这个问题是因为我在面试中问了一个问题,什么是组合和聚合。
So as per my understanding i given my idea which is as follow.
所以根据我的理解,我给出了我的想法如下。
http://www.coderanch.com/t/522414/java/java/Association-Aggregation-Composition
http://www.coderanch.com/t/522414/java/java/Association-Aggregation-Composition
Aggregation, Association and Composition
Association vs. Aggregation vs. Composition in Java
and also visited much more.
并且还参观了更多。
and my basic explanation was related that Aggregation indicates loose relationship while Composition indicates Strong relationship and clear explanation related to this.
我的基本解释是聚合表示松散的关系,而组合表示强关系和与此相关的清晰解释。
But the Interviewer Insulted me and said this is the theoretical concept about which you saying i want the perfect Java code in which how they differ and also told if i will give you one small Application then how would you identify that this is the Aggregation and this is the Composition?
但是面试官侮辱了我,说这是一个理论概念,你说我想要完美的 Java 代码,它们之间有什么不同,还告诉我是否会给你一个小应用程序,那么你如何确定这是聚合和这个是什么?
So now i want to understand pure Technical Concept and Java code in which how they differ and what it is?
所以现在我想了解纯技术概念和 Java 代码,它们有何不同以及它们是什么?
回答by Nishant
Consider a simple example of a LinkedList
class. A LinkedList
is made up of Nodes
. Here LinkedList
class is the owning object. When it is destroyed, all the Nodes
contained in it are wiped off. If a Node
object is deleted from the list, the LinkedList
object will still exist.
考虑一个简单的LinkedList
类示例。ALinkedList
由Nodes
. 这里的LinkedList
类是拥有对象。当它被摧毁时,Nodes
它所包含的所有东西都会被抹去。如果Node
从列表中删除了一个对象,该LinkedList
对象将仍然存在。
Composition is implemented such that an object contains another object.
组合的实现使得一个对象包含另一个对象。
class LinkedList {
Node head;
int size;
}
This is composition.
这是组成。
Another example, consider a Circle
class which has a Point
class which is used to define the coordinates of its center. If the Circle
is deleted its center is deleted along with it.
另一个例子,考虑一个Circle
类,它有一个Point
用于定义其中心坐标的类。如果Circle
被删除,它的中心也会随之删除。
class Circle {
private float radius;
private Point center;
public Circle(Point center, float radius) {
this.center = center;
this.radius = radius;
}
public void setRadius(float radius) {
this.radius = radius;
}
public void setCenter(Point center) {
this.center = center;
}
public float getRadius() {
return radius;
}
public Point getCenter() {
return center;
}
class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void setY(int y) {
this.y = y;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public int getX() {
return x;
}
}
@Override
public String toString() {
return "Center: " + "(" + center.getX() + "," + center.getY() + ")" + "\nRadius: " + this.getRadius()
+ " centimeters";
}
}
Example of composition from java collections api : HashMap
has an Entry
class. If the HashMap
object is deleted all the Entry
objects contained in the map are deleted along with it.
来自 java 集合 api 的组合示例:HashMap
有一个Entry
类。如果HashMap
删除Entry
对象,则映射中包含的所有对象也将随之删除。
Aggregation is also a type of Object Compositionbut not as strong as Composition. It defines "HAS A" relationship. Put in very simple words if Class A has a reference of Class B and removing B doesn't affect the existence of A then it is aggregation. You must have used it at some point.
聚合也是一种对象组合,但不如组合强大。它定义了“HAS A”关系。用非常简单的话来说,如果 A 类有 B 类的引用并且删除 B 不会影响 A 的存在,那么它就是聚合。你一定在某个时候使用过它。
回答by Musa
A would like to add additional information to explained answers
A 想在解释的答案中添加其他信息
Aggregation is a special form of Assosation (Association is relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many)
聚合是一种特殊形式的关联(关联是两个独立类之间通过它们的对象建立的关系。关联可以是一对一、一对多、多对一、多对多)
- Aggregation represents Has-A relationship.
- It is a unidirectional association i.e. a one way relationship. For example, department can have employees but vice versa is not possible and thus unidirectional in nature.
- In Aggregation, both the entries can survive individually which means ending one entity will not effect the other entity.
- 聚合表示 Has-A 关系。
- 它是一种单向关联,即单向关联。例如,部门可以有员工,反之亦然是不可能的,因此本质上是单向的。
- 在聚合中,两个条目都可以单独存在,这意味着结束一个实体不会影响另一个实体。
Composition is a restricted form of Aggregation in which two entities are highly dependent on each other.
组合是聚合的一种受限形式,其中两个实体高度依赖彼此。
- It represents part-of relationship.
- In composition, both the entities are dependent on each other.
- When there is a composition between two entities, the composed object cannot exist without the other entity.
- 它代表部分关系。
- 在组合中,两个实体相互依赖。
- 当两个实体之间存在组合时,如果没有另一个实体,组合对象就不能存在。
Aggregation vs Composition
聚合与组合
- Dependency:Aggregation implies a relationship where the child can exist independently of the parent. For example, Department and Employee, delete the Department and the Employee still exist. whereas Composition implies a relationship where the child cannot exist independent of the parent. Example: Human and heart, heart don't exist separate to a Human.
- 依赖:聚合意味着一种关系,其中子可以独立于父存在。比如Department和Employee,删除Department,Employee仍然存在。而组合意味着一种关系,其中孩子不能独立于父母而存在。例子:人与心,心与人是分开存在的。
- Type of Relationship:Aggregation relation is “has-a” and composition is “part-of” relation.
- 关系类型:聚合关系是“has-a”,组合是“part-of”关系。
- Type of association:Composition is a strong Association whereas Aggregation is a weak Association.
- 关联类型:组合是强关联,而聚合是弱关联。
Reference: https://www.geeksforgeeks.org/association-composition-aggregation-java/
参考:https: //www.geeksforgeeks.org/association-composition-aggregation-java/