保持一个类的对象和聚合它们有什么区别?

时间:2022-02-16 13:21:24

I can not distinguish the difference between aggregating and holding. What does it mean in terms of, let`s say, C++?
I suppose when the object of class A holds (or instantiates) objects of class B, it uses it to perform some functions by itself.
For example:

我无法区分聚合和持有之间的区别。就C ++来说,这意味着什么呢?我想当A类的对象持有(或实例化)B类对象时,它会使用它来自己执行某些功能。例如:

class A {
 int state;
 public: 
  A(int s): state(s) {}
  int inc() { return state++; }
};
class B {
  int app;
  string s;
public:
  B(): app(0), s("") {}
  B(int A, const string& str): app(A), s(str) {}
  void f(int p);
  ~B() { app = 0; s = ""; }
};
void B::f(int p)
{
   A mA(p);
   app = mA.inc();
}

And the aggregation of object of class A would be like this:

而A类对象的聚合将如下所示:

class B{
 A t;
 //...
}

Please give me a link to a web-site or to a place in a book where I can find clear definitions in terms of OO language what exactly each kind of relationship between classes is.

请给我一个指向网站或书籍中某个地方的链接,在那里我可以找到关于OO语言的明确定义,确切地说每个类之间的关系是什么。

3 个解决方案

#1


1  

Holding another class means that a class is associated with another class through a parent child relationship. For example a Path has a list of points. The Path is the parent of the List of Points which is the parent of the individual points.

持有另一个类意味着一个类通过父子关系与另一个类相关联。例如,Path有一个点列表。 Path是Points List的父级,它是各个点的父级。

Aggregating means taking different classes and putting them behind a interface so they appear as one class. For example a FileDialog will have several button class, a text input class, a listview/treeview class and so on. But to the rest of the system it just had methods to be activate, maybe assign a default filename, and retrieve the rest.

聚合意味着使用不同的类并将它们放在接口之后,使它们显示为一个类。例如,FileDialog将具有多个按钮类,文本输入类,listview / treeview类等。但是对于系统的其余部分,它只需要激活方法,可以分配默认文件名,然后检索其余文件。

The fact it is comprised of all the other classes is immaterial to other classes using it as a filedialog.However it works by aggregating all the classes to perform the expected behavior.

它由所有其他类组成的事实对于使用它作为filedialog的其他类是无关紧要的。但是它通过聚合所有类来执行预期的行为。

#2


1  

IMO, if I understood correctly, you are asking for a definition of Composition vs. Aggregation. Aggregation or holding is a collection of entities. Composition has a tighter constraint. Think of average marks awarded to student: when we compute an average, we cannot exclude any item. Composition is similar to this. No item can be missed out. Aggregation on the other hand is more loosely defined.

IMO,如果我理解正确,你要求定义组合与聚合。聚合或保留是实体的集合。构图具有更严格的约束。考虑给予学生的平均分数:当我们计算平均分时,我们不能排除任何项目。组成与此类似。没有项目可以错过。另一方面,聚合的定义更为宽松。

An interesting analogy would be that of a quiver full of arrows and a car. A quiver is an Aggregation of arrows, quiver can exist without arrows; Car is an Composition (Sum of parts). And lets not argue that a car without a wheel is still a car :)

一个有趣的比喻是一个充满箭头和汽车的箭袋。箭袋是箭的聚合体,箭可以不带箭头存在;汽车是一种组合(零件总和)。并且不要争辩说没有*的汽车仍然是一辆车:)

As for references: http://www.bletchleypark.net/algorithms/software/oop.html

至于参考文献:http://www.bletchleypark.net/algorithms/software/oop.html

#3


0  

It's very much about ownership and visibility. There are different names for these relationships, but you should consider these two points:

这非常关乎所有权和知名度。这些关系有不同的名称,但您应该考虑以下两点:

  • Does the parent have strict ownership over the child, such that when the parent dies, the child should die also?

    父母是否对孩子拥有严格的所有权,这样当父母去世时,孩子也会死亡?

  • Is the child visible to other entities besides the parent?

    除了父母之外,其他实体是否可以看到孩子?

Answers to these questions will help you clarify these relationships.

这些问题的答案将帮助您澄清这些关系。

#1


1  

Holding another class means that a class is associated with another class through a parent child relationship. For example a Path has a list of points. The Path is the parent of the List of Points which is the parent of the individual points.

持有另一个类意味着一个类通过父子关系与另一个类相关联。例如,Path有一个点列表。 Path是Points List的父级,它是各个点的父级。

Aggregating means taking different classes and putting them behind a interface so they appear as one class. For example a FileDialog will have several button class, a text input class, a listview/treeview class and so on. But to the rest of the system it just had methods to be activate, maybe assign a default filename, and retrieve the rest.

聚合意味着使用不同的类并将它们放在接口之后,使它们显示为一个类。例如,FileDialog将具有多个按钮类,文本输入类,listview / treeview类等。但是对于系统的其余部分,它只需要激活方法,可以分配默认文件名,然后检索其余文件。

The fact it is comprised of all the other classes is immaterial to other classes using it as a filedialog.However it works by aggregating all the classes to perform the expected behavior.

它由所有其他类组成的事实对于使用它作为filedialog的其他类是无关紧要的。但是它通过聚合所有类来执行预期的行为。

#2


1  

IMO, if I understood correctly, you are asking for a definition of Composition vs. Aggregation. Aggregation or holding is a collection of entities. Composition has a tighter constraint. Think of average marks awarded to student: when we compute an average, we cannot exclude any item. Composition is similar to this. No item can be missed out. Aggregation on the other hand is more loosely defined.

IMO,如果我理解正确,你要求定义组合与聚合。聚合或保留是实体的集合。构图具有更严格的约束。考虑给予学生的平均分数:当我们计算平均分时,我们不能排除任何项目。组成与此类似。没有项目可以错过。另一方面,聚合的定义更为宽松。

An interesting analogy would be that of a quiver full of arrows and a car. A quiver is an Aggregation of arrows, quiver can exist without arrows; Car is an Composition (Sum of parts). And lets not argue that a car without a wheel is still a car :)

一个有趣的比喻是一个充满箭头和汽车的箭袋。箭袋是箭的聚合体,箭可以不带箭头存在;汽车是一种组合(零件总和)。并且不要争辩说没有*的汽车仍然是一辆车:)

As for references: http://www.bletchleypark.net/algorithms/software/oop.html

至于参考文献:http://www.bletchleypark.net/algorithms/software/oop.html

#3


0  

It's very much about ownership and visibility. There are different names for these relationships, but you should consider these two points:

这非常关乎所有权和知名度。这些关系有不同的名称,但您应该考虑以下两点:

  • Does the parent have strict ownership over the child, such that when the parent dies, the child should die also?

    父母是否对孩子拥有严格的所有权,这样当父母去世时,孩子也会死亡?

  • Is the child visible to other entities besides the parent?

    除了父母之外,其他实体是否可以看到孩子?

Answers to these questions will help you clarify these relationships.

这些问题的答案将帮助您澄清这些关系。