官术网_书友最值得收藏!

Aggregation and composition

We encountered aggregation in the example of the Warehouse class. The Warehouse class stores an array of Products. In more general terms, it can be called an association, but to strongly emphasize the exact containment, we use the term aggregation or composition. In the case of aggregation, the class that contains an instance or instances of other classes could be instantiated without the aggregate. This means that we can create and use a Warehouse object without necessarily creating Product objects contained in the Warehouse.

Another example of aggregation is the Car and the Person. A Car can contain a Person object (as a driver or passenger) since they are associated with each other, but the containment is not strong. We can create a Car object without a Driver in it, as follows:

class Person; // forward declaration
class Engine { /* code omitted for brevity */ };
class Car {
public:
Car();
// ...
private:
Person* driver_; // aggregation
std::vector<Person*> passengers_; // aggregation
Engine engine_; // composition
// ...
};

The strong containment is expressed by composition. For the Car example, an object of the Engine class is required to make a complete Car object. In this physical representation, the Engine member is automatically created when a Car is created. 

The following is the UML representation of aggregation and composition:

When designing classes, we have to decide on their relationship. The best way to define the composition between the two classes is the has-a relationship test. A Car has-a Engine, because a car has an engine. Any time you can't decide whether the relationship should be expressed in terms of composition, ask the has-a question. Aggregation and composition are somewhat similar; they just describe the strength of the connection. For aggregation, the proper question would be can have a; for example, a Car can have a driver (of the Person type); that is, the containment is weak. 

主站蜘蛛池模板: 岑溪市| 滨州市| 大连市| 柞水县| 金昌市| 惠州市| 济阳县| 大石桥市| 景谷| 玛纳斯县| 绥宁县| 门头沟区| 康定县| 右玉县| 腾冲县| 樟树市| 贞丰县| 肇东市| 洱源县| 登封市| 左贡县| 龙岩市| 商洛市| 宁城县| 深圳市| 汉中市| 雷波县| 泽库县| 肃南| 库车县| 达孜县| 施甸县| 阳西县| 北碚区| 钦州市| 方城县| 罗山县| 乐陵市| 阿图什市| 酒泉市| 女性|