一、基礎知識
1. Composition(複合):表示has-a
1.1 Adapter:
1.2 內存角度:
1.3 Composition(複合)關係下的構造和析構
2. Delegation / Composition by reference(委託)
2.1 Handle/Body (pImpI / pointer to implementation)
3. Inheritance(繼承):表示is-a
3.1 Inheritance(繼承)關係下的構造和析構
3.2 Inheritance在和虛函數搭配使用時才真正體現出它的價值。例如下面的Templete Method,屬於23個設計模式之一。
4.複雜關係
4.1 Inheritance + Composition關係下的構造和析構
關係1:
關係2:
4.2 Delegation + Inheritance
功能最強大,但是怎麼應用?將在下面第二部分表現。
二、Delegation + Inheritance應用
1.觀察者模式Observer
1.1 Observer應用場景:
2.組合模式Composite
2.1 Composite應用場景:windows的文件夾與文件系統,文件夾中又有文件。
3.原型模式Prototype
3.1 Prototype應用場景:原型模式是通過已經存在的對象的接口快速方便的創建新的對象。