May be the questions sounds newbie, but I can not distinguish the difference between agregating 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:
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:
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.
-
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.
-
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.
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.
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.
-
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.
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
0 comments:
Post a Comment