一、基础知识
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应用场景:原型模式是通过已经存在的对象的接口快速方便的创建新的对象。