这是什么设计模式?
几年前,我曾经创建这样的接口:
class Base
{
public:
virtual ~Base
{
}
void foo()
{
doFoo();
}
private:
virtual void doFoo() = 0;
};
那么派生将是:
class Derived : public Base
{
public:
virtual ~Derived()
{
}
private:
virtual void doFoo()
{
}
};
我确信我在某处看到了这是一种设计模式,但现在我在任何地方都找不到它,并且不记得它是如何调用的。
那么,这种设计模式怎么称呼呢?
Several years ago, I used to create interfaces like this :
class Base
{
public:
virtual ~Base
{
}
void foo()
{
doFoo();
}
private:
virtual void doFoo() = 0;
};
then a derived would be :
class Derived : public Base
{
public:
virtual ~Derived()
{
}
private:
virtual void doFoo()
{
}
};
I am sure I saw this as a design pattern somewhere, but now I can not find it anywhere, and can not remember how it is called.
So, how is this design pattern called?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
foo
方法不应该是虚拟的。在这种情况下,设计模式称为 NVI - 非虚拟接口Your
foo
method shouldn't be virtual. And in this case the design pattern is called NVI - non-virtual interface这是模板方法模式。维基百科相关摘录:
我已经看到这种模式大量用于“强制”调用基类实现(通常必须在派生类中显式完成)。
This is the template method pattern. Relevant excerpt from Wikipedia:
I've seen this pattern used a lot to "enforce" calling the base class implementation (which normally has to be done explicitly in the deriving class).