// X has a Y // A class contains other classes as members // A stronger variety of aggregation, the part object may belong to only one whole // Expected to live and die with the whole (delete whole -> delete part) classX { public: Y a; // 1; Composition Y b[10]; // 0..10; Composition };
classX { public: X() { a = new Y[10]; }
~X() { delete[] a; }
private: Y* a; // 0..10; Composition };
classX { private: std::vector<Y> v; // Composition of std::vector<Y> not composition of Y; };
Aggregation
1 2 3 4 5 6
// X has a Y // A class contains other classes as members // Cascading delete is often // An aggregated instance can be shared // Following Larman OOAD: use of aggregation is NOT recommended // No example here use Association instead of Aggregation
Inheritance
1 2 3 4 5 6 7 8 9 10 11
// X is a Y // A class is derived from another class classY {
};
classX : public Y {
};
Class Template
1 2 3 4 5 6 7 8
// Class Template (Y use parameterized class X) template<classT> classX {
};
X<Y> a;
程序设计中模块间通信简化为类间通讯,了解类之间的关系后可以进一步设计模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classPT : public PTAPI, // pt write interface and implement public UI_CALL_BACK_SINK // ui write interface pt write implement { public: // PTAPI // also need write call back private: UIAPI ui_api_; // use to control ui // also use for call back };
classUI : public UIAPI, // ui write interface and implement public PT_CALL_BACK_SINK // pt write interface ui write implement { public: // UIAPI // also need write call back private: PTAPI pt_api_; // use to control pt // also use for call back };