What is main work of composition?
An object may be composed of other smaller objects, the relationship between the “part” objects and the “whole” object is known as Composition.
In UML, composition is depicted as a filled diamond and a solid line. It always implies a multiplicity of 1 or 0..1, as no more than one object at a time can have lifetime responsibility for another object.
The more general form, aggregation, is depicted as an unfilled diamond and a solid line. The image below shows both composition and aggregation. The C++ code below shows what the source code is likely to look like.
// Composition
class Car
{
private:
Carburetor* carb;
public:
Car() : carb(new Carburetor()) { }
virtual ~Car() { delete carb; }
};
// Aggregation
class Pond
{
private:
std::vector<Duck*> ducks;
};
In UML, composition is depicted as a filled diamond and a solid line. It always implies a multiplicity of 1 or 0..1, as no more than one object at a time can have lifetime responsibility for another object.
The more general form, aggregation, is depicted as an unfilled diamond and a solid line. The image below shows both composition and aggregation. The C++ code below shows what the source code is likely to look like.
// Composition
class Car
{
private:
Carburetor* carb;
public:
Car() : carb(new Carburetor()) { }
virtual ~Car() { delete carb; }
};
// Aggregation
class Pond
{
private:
std::vector<Duck*> ducks;
};
0 Comments
Please add nice comments or answer ....