这是一个示例代码,显示了如何在C ++中使用装饰设计模式的示例,虽然代码看起来很简单,但它让我感到困惑的是它为什么正常工作..
代码示例:
#include <iostream>
using namespace std;
// 1. "lowest common denom"
class Widget
{
public:
virtual void draw() = 0;
};
class TextField: public Widget
{
// 3. "Core" class & "is a"
int width, height;
public:
TextField(int w, int h)
{
width = w;
height = h;
}
/*virtual*/
void draw()
{
cout << "TextField: " << width << ", " << height << '\n';
}
};
// 2. 2nd level base class
class Decorator: public Widget // 4. "is a" relationship
{
Widget *wid; // 4. "has a" relationship
public:
Decorator(Widget *w)
{
wid = w;
}
/*virtual*/
void draw()
{
wid->draw(); // 5. Delegation
}
};
class BorderDecorator: public Decorator
{
public:
// 6. Optional embellishment
BorderDecorator(Widget *w): Decorator(w){}
/*virtual*/
void draw()
{
// 7. Delegate to base class and add extra stuff
Decorator::draw();
cout << " BorderDecorator" << '\n';
}
};
class ScrollDecorator: public Decorator
{
public:
// 6. Optional embellishment
ScrollDecorator(Widget *w): Decorator(w){}
/*virtual*/
void draw()
{
// 7. Delegate to base class and add extra stuff
Decorator::draw();
cout << " ScrollDecorator" << '\n';
}
};
int main()
{
// 8. Client has the responsibility to compose desired configurations
Widget *aWidget = new BorderDecorator(new BorderDecorator(new ScrollDecorator(new TextField(80, 24))));
aWidget->draw();
}
输出产生:
TextField: 80, 24
ScrollDecorator
BorderDecorator
BorderDecorator
我不能得到的部分是,据我所知,代码永远不会到达
cout << " BorderDecorator" << '\n';
cout << " ScrollDecorator" << '\n';
的作用是什么
Decorator::draw();
看起来像递归电话..如果我的问题在这里有点混乱,请道歉。
答案 0 :(得分:1)
在从Decorator
派生的类中,对Decorator::draw()
的调用是对基类方法的显式调用,即使它被声明为virtual
。它根本不是一个递归调用,而是一种调用基类功能,然后在之后(或之前)添加更多功能的方法。