# Item 38: 通過 composition(復合)模擬 "has-a"(有一個)或 "is-implemented-in-terms-of"(是根據……實現的)
作者:Scott Meyers
譯者:fatalerror99 (iTePub's Nirvana)
發布:http://blog.csdn.net/fatalerror99/
composition(復合)是在 objects of one type(一個類型的對象)包含 objects of another type(另一個類型的對象)時,types(類型)之間的關系。例如:
```
class Address { ... }; // where someone lives
class PhoneNumber { ... };
class Person {
public:
...
private:
std::string name; // composed object
Address address; // ditto
PhoneNumber voiceNumber; // ditto
PhoneNumber faxNumber; // ditto
};
```
此例之中,Person objects(對象)由 string,Address,和 PhoneNumber objects(對象)組成。在程序員中,術語 composition(復合)有很多同義詞。它也可以稱為 layering,containment,aggregation,和 embedding。
Item 32 解釋了 public inheritance(公有繼承)意味著 "is-a"(是一個)。composition(復合)也有一個含意。實際上,他有兩個含意。composition(復合)既意味著 "has-a"(有一個),又意味著 "is-implemented-in-terms-of"(是根據……實現的)。這是因為你要在你的軟件中處理兩個不同的 domains(領域)。你程序中的一些 objects(對象)對應你所模擬的世界里的東西,例如,people(人),vehicles(交通工具),video frames(視頻畫面)等等。這樣的 objects(對象)是 application domain(應用領域)的部分。另外的 objects(對象)純粹是 implementation artifacts(實現的產物),例如,buffers(緩沖區),mutexes(互斥體),search trees(搜索樹)等等。這些各類 objects(對象)定義應你的軟件的 implementation domain(實現領域)。當 composition(復合)發生在 application domain(應用領域)的 objects(對象)之間,它表達一個 has-a(有一個)的關系,當它發生在 implementation domain(實現領域),它表達一個 is-implemented-in-terms-of(是根據……實現的)的關系
上面的 Person class(類)示范了 has-a(有一個)的關系。一個 Person object(對象)has a(有一個)名字,一個地址,以及語音和傳真電話號碼。你不能說一個人 is a(是一個)名字或一個人 is an(是一個)地址。你可以說一個人 has a(有一個)名字和 has an(有一個)地址。大多數人對此區別不難理解,所以混淆 is-a(是一個)和 has-a(有一個)之間的角色的情況非常少見。
is-a(是一個)和 is-implemented-in-terms-of(是根據……實現的)之間的區別稍微有些棘手。例如,假設你需要一個類的模板來表現相當小的 objects(對象)的 sets,也就是說,排除重復的集合。因為 reuse(復用)是一件受人歡迎的事情,你的第一個直覺就是使用標準庫中的 set template(模板)。當你能使用已經被寫好的東西時,為什么還要寫一個新的 template(模板)呢?
不幸的是,set 的典型實現導致每個元素三個指針的開銷。這是因為 sets 通常被作為 balanced search trees(平衡搜索樹)來實現,這允許它們保證 logarithmic-time(對數時間)的 lookups(查找),insertions(插入)和 erasures(刪除)。當速度比空間更重要的時候,這是一個合理的設計,但是當空間比速度更重要時,對你的程序來說就有問題了。因而,對你來說,標準庫的 set 為你提供了不合理的交易。看起來你終究還是要寫你自己的 template(模板)。
reuse(復用)依然是一件受人歡迎的事情。作為 data structure(數據結構)的專家,你知道實現 sets 的諸多選擇,其中一種是使用 linked lists(線性鏈表)。你也知道標準的 C++ 庫中有一個 list template(模板),所以你決定(復)用它。
具體地說,你決定讓你的新的 Set template(模板)從 list 繼承。也就是說,Set<T> 將從 list<T> 繼承。畢竟,在你的實現中,一個 Set object(對象)實際上就是一個 list object(對象)。于是,你就像這樣聲明你的 Set template(模板):
```
template<typename T> // the wrong way to use list for Set
class Set: public std::list<T> { ... };
```
在這里,看起來每件事情都很好。但實際上有一個很大的錯誤。就像 Item 32 中的解釋,如果 D is-a(是一個)B,對于 B 成立的每一件事情對 D 也成立。然而,一個 list object(對象)可以包含重復,所以如果值 3051 被插入一個 list<int> 兩次,那個 list 將包含 3051 的兩個拷貝。與此對照,一個 Set 不可以包含重復,所以如果值 3051 被插入一個 Set<int> 兩次,那個 set 只包含該值的一個拷貝。因此一個 Set is-a(是一個)list 是不正確的,因為對 list objects(對象)成立的某些事情對 Set objects(對象)不成立。
因為這兩個 classes(類)之間的關系不是 is-a(是一個),public inheritance(公有繼承)不是模擬這個關系的正確方法。正確的方法是認識到一個 Set object(對象)可以 be implemented in terms of a list object(是根據一個 list 對象實現的):
```
template<class T> // the right way to use list for Set
class Set {
public:
bool member(const T& item) const;
void insert(const T& item);
void remove(const T& item);
std::size_t size() const;
private:
std::list<T> rep; // representation for Set data
};
```
Set 的 member functions(成員函數)可以極大程度地依賴 list 和標準庫的其它部分已經提供的機能,所以只要你熟悉了用 STL 編程的基本方法,實現就非常簡單了:
```
template<typename T>
bool Set<T>::member(const T& item) const
{
return std::find(rep.begin(), rep.end(), item) != rep.end();
}
template<typename T>
void Set<T>::insert(const T& item)
{
if (!member(item)) rep.push_back(item);
}
template<typename T>
void Set<T>::remove(const T& item)
{
typename std::list<T>::iterator it = // see Item 42 for info on
std::find(rep.begin(), rep.end(), item); // "typename" here
if (it != rep.end()) rep.erase(it);
}
template<typename T>
std::size_t Set<T>::size() const
{
return rep.size();
}
```
這些函數足夠簡單,使它們成為 inlining(內聯化)的合理候選者,可是我知道在堅定 inlining(內聯化)的決心之前,你可能需要回顧一下 Item 30 中的討論。
一個有說服力的觀點是,根據 Item 18 的關于將 interfaces(接口)設計得易于正確使用,而難以錯誤使用的論述,如果要遵循 STL container(容器)的慣例,Set 的 interface(接口)應該更多,但是在這里遵循那些慣例就需要在 Set 中填充大量 stuff(材料),這將使得它和 list 之間的關系變得曖昧不清。因為這個關系是本 Item 的重點,我們用教學的清晰性替換了 STL 的兼容性。除此之外,Set 的 interface(接口)的幼稚不應該遮掩關于 Set 的無可爭辯的正確:它和 list 之間的關系。這個關系不是 is-a(是一個)(雖然最初看上去可能很像),而是 is-implemented-in-terms-of(是根據……實現的)。
Things to Remember
* composition(復合)與 public inheritance(公有繼承)的意義完全不同。
* 在 application domain(應用領域)中,composition(復合)意味著 has-a(有一個)。在 implementation domain(實現領域)中意味著 is-implemented-in-terms-of(是根據……實現的)。
- Preface(前言)
- Introduction(導言)
- Terminology(術語)
- Item 1: 將 C++ 視為 federation of languages(語言聯合體)
- Item 2: 用 consts, enums 和 inlines 取代 #defines
- Item 3: 只要可能就用 const
- Item 4: 確保 objects(對象)在使用前被初始化
- Item 5: 了解 C++ 為你偷偷地加上和調用了什么函數
- Item 6: 如果你不想使用 compiler-generated functions(編譯器生成函數),就明確拒絕
- Item 7: 在 polymorphic base classes(多態基類)中將 destructors(析構函數)聲明為 virtual(虛擬)
- Item 8: 防止因為 exceptions(異常)而離開 destructors(析構函數)
- Item 9: 絕不要在 construction(構造)或 destruction(析構)期間調用 virtual functions(虛擬函數)
- Item 10: 讓 assignment operators(賦值運算符)返回一個 reference to *this(引向 *this 的引用)
- Item 11: 在 operator= 中處理 assignment to self(自賦值)
- Item 12: 拷貝一個對象的所有組成部分
- Item 13: 使用對象管理資源
- Item 14: 謹慎考慮資源管理類的拷貝行為
- Item 15: 在資源管理類中準備訪問裸資源(raw resources)
- Item 16: 使用相同形式的 new 和 delete
- Item 17: 在一個獨立的語句中將 new 出來的對象存入智能指針
- Item 18: 使接口易于正確使用,而難以錯誤使用
- Item 19: 視類設計為類型設計
- Item 20: 用 pass-by-reference-to-const(傳引用給 const)取代 pass-by-value(傳值)
- Item 21: 當你必須返回一個對象時不要試圖返回一個引用
- Item 22: 將數據成員聲明為 private
- Item 23: 用非成員非友元函數取代成員函數
- Item 24: 當類型轉換應該用于所有參數時,聲明為非成員函數
- Item 25: 考慮支持不拋異常的 swap
- Item 26: 只要有可能就推遲變量定義
- Item 27: 將強制轉型減到最少
- Item 28: 避免返回對象內部構件的“句柄”
- Item 29: 爭取異常安全(exception-safe)的代碼
- Item 30: 理解 inline 化的介入和排除
- Item 31: 最小化文件之間的編譯依賴
- Item 32: 確保 public inheritance 模擬 "is-a"
- Item 33: 避免覆蓋(hiding)“通過繼承得到的名字”
- Item 34: 區分 inheritance of interface(接口繼承)和 inheritance of implementation(實現繼承)
- Item 35: 考慮可選的 virtual functions(虛擬函數)的替代方法
- Item 36: 絕不要重定義一個 inherited non-virtual function(通過繼承得到的非虛擬函數)
- Item 37: 絕不要重定義一個函數的 inherited default parameter value(通過繼承得到的缺省參數值)
- Item 38: 通過 composition(復合)模擬 "has-a"(有一個)或 "is-implemented-in-terms-of"(是根據……實現的)
- Item 39: 謹慎使用 private inheritance(私有繼承)
- Item 40: 謹慎使用 multiple inheritance(多繼承)
- Item 41: 理解 implicit interfaces(隱式接口)和 compile-time polymorphism(編譯期多態)
- Item 42: 理解 typename 的兩個含義
- Item 43: 了解如何訪問 templatized base classes(模板化基類)中的名字
- Item 44: 從 templates(模板)中分離出 parameter-independent(參數無關)的代碼
- Item 45: 用 member function templates(成員函數模板) 接受 "all compatible types"(“所有兼容類型”)
- Item 46: 需要 type conversions(類型轉換)時在 templates(模板)內定義 non-member functions(非成員函數)
- Item 47: 為類型信息使用 traits classes(特征類)
- Item 48: 感受 template metaprogramming(模板元編程)
- Item 49: 了解 new-handler 的行為
- Item 50: 領會何時替換 new 和 delete 才有意義
- Item 51: 編寫 new 和 delete 時要遵守慣例
- Item 52: 如果編寫了 placement new,就要編寫 placement delete
- 附錄 A. 超越 Effective C++
- 附錄 B. 第二和第三版之間的 Item 映射