【发布时间】:2017-06-14 23:47:15
【问题描述】:
对于对象适配器设计,GoF 指出:
使得覆盖 Adaptee 行为变得更加困难。它需要继承 Adaptee 并使 Adapter 引用子类而不是 Adaptee 本身
我的问题是,当我们创建如下类时为什么需要这个子类化:
class Target {
public :
virtual void op() = 0 ;
} ;
class Adaptee {
public :
void adapteeOp() {cout<<"adaptee op\n" ;}
} ;
class Adapter : public Target {
Adaptee *adaptee ;
public :
Adapter(Adaptee *a) : adaptee(a) {}
void op() {
// added behavior
cout<<"added behavior\n" ;
adaptee->adapteeOp() ;
// more added behavior
cout<<"more added behavior\n" ;
}
} ;
main() { //client
Adapter adapter(new Adaptee) ;
adapter.op() ;
}
当我也能够覆盖此处的行为时,我无法理解 GoF 提到的子类化要求。
请解释我错过了什么。
【问题讨论】:
标签: c++ oop inheritance design-patterns polymorphism