【问题标题】:Are the "Prototype pattern" and the "Virtual constructor" the same patterns?“原型模式”和“虚拟构造函数”是相同的模式吗?
【发布时间】:2023-03-29 09:35:02
【问题描述】:

Virtual 构造函数 - 实现virtual function clone():

class X {
public:
     virtual X* clone() {
         return new X(*this);
     }
};

与原型设计模式的概念相同?

【问题讨论】:

  • 不完全是。实现原型设计模式需要克隆接口。
  • 谢谢。但是原型模式还有什么额外的含义呢?
  • 您使用可配置的 Factory 创建新的类实例,它使用单个实例作为原型进行克隆。你可以看到更深入的解释here

标签: c++ design-patterns prototype-pattern


【解决方案1】:

...与原型设计模式的概念相同?

不,它没有。 可克隆接口只是Prototype Design Pattern实现的一部分。

Prototype 的意义在于拥有一个Factory 来保存原型实例的实例,并且知道使用哪个作为克隆源来创建新实例。

【讨论】:

    猜你喜欢
    • 2011-04-16
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 2010-12-21
    • 2015-10-09
    • 1970-01-01
    • 2012-04-29
    • 2018-02-09
    相关资源
    最近更新 更多