在c++中,原型设计模式和复制构造函数有什么不同

时间:2022-01-31 13:21:28

I am trying to understand when I should use Prototype design pattern. Here is example of Prototype as I understand it:

我正在尝试理解什么时候应该使用原型设计模式。以下是我理解的原型的例子:

class Prototype
{
public:
    virtual Prototype* clone() = 0;
...
};

class ConcretePrototype : public Prototype
{
public:
    Prototype* clone() override { ... }
};

// Usage:
ConcretePrototype proto;
auto protPtr = proto.clone();

Where is a question: Why this better than:

问题是:为什么这比:

class Obj
{
public:
    Obj();

    Obj(const Obj&);
    Obj& operator = (const Obj& other);
};

Obj o;
Obj o2 = o;

So when I actually should use Prototype?

那么我什么时候应该使用Prototype呢?

2 个解决方案

#1


9  

Copy constructor is an element of the language.

复制构造函数是语言的一个元素。

Prototype is a design pattern used to spawn (polymorphic) objects basing on some existing instance.

Prototype是一种基于一些现有实例生成(多态)对象的设计模式。

It would be difficult to use the former to implement the latter as copy constructor is intended to be used when knowing exact instance of the object, while prototype is used when there could be any possible implementation of some interface and you just want to obtain new object of exactly the same implementation, without resorting to some weird casting and checking methods.

很难用前者来实现后者使用拷贝构造函数的目的是当知道确切的实例对象,而原型时使用可能会有一些接口的任何可能的实现,你只是想获得完全相同的新对象实现,不需要采取一些奇怪的铸造和检查方法。

Lets assume that you have Interface I and implementations A and B. At some point you are given object i implementing I. Perhaps you would not want to modify it, instead you would prefer to obtain new instance and then introduce some modification to it. How could that be achieved, when you don't know exact class of i? Prototype pattern is one solution to that problem: I* i2 = i.clone();.

让我们假设您有接口I和实现A和b。在某个时候,您会得到一个对象I实现I。当你不知道i的确切类别时,这是怎么实现的呢?原型模式是这个问题的一个解决方案:I* i2 = I .clone();

#2


6  

Here is reference from GoF book:

以下是GoF书籍的参考:

clients that clone the prototype don't have to know about their concrete subclasses. Clients should never need to downcast the return value of Clone to the desired type.

克隆原型的客户不需要知道他们的具体子类。客户永远都不需要将克隆的返回值降至所需的类型。

在c++中,原型设计模式和复制构造函数有什么不同

For for you example, the client is NOT aware (or cannot access) of ConcretePrototype.

例如,客户端不知道(或不能访问)具象原型。

// Usage:
SomeMethod(PrototypeInterface iproto) // Thanks Prototype pattern
{
  // ConcretePrototype proto;     
  auto protPtr = iproto.clone(); // Gets correct object without calling concrete object's constructor explicitly.
}

Hope you understood the situation when this pattern is useful. Also Remember, there is no virtual constructor.

希望您理解此模式的使用情况。还要记住,没有虚拟构造函数。

#1


9  

Copy constructor is an element of the language.

复制构造函数是语言的一个元素。

Prototype is a design pattern used to spawn (polymorphic) objects basing on some existing instance.

Prototype是一种基于一些现有实例生成(多态)对象的设计模式。

It would be difficult to use the former to implement the latter as copy constructor is intended to be used when knowing exact instance of the object, while prototype is used when there could be any possible implementation of some interface and you just want to obtain new object of exactly the same implementation, without resorting to some weird casting and checking methods.

很难用前者来实现后者使用拷贝构造函数的目的是当知道确切的实例对象,而原型时使用可能会有一些接口的任何可能的实现,你只是想获得完全相同的新对象实现,不需要采取一些奇怪的铸造和检查方法。

Lets assume that you have Interface I and implementations A and B. At some point you are given object i implementing I. Perhaps you would not want to modify it, instead you would prefer to obtain new instance and then introduce some modification to it. How could that be achieved, when you don't know exact class of i? Prototype pattern is one solution to that problem: I* i2 = i.clone();.

让我们假设您有接口I和实现A和b。在某个时候,您会得到一个对象I实现I。当你不知道i的确切类别时,这是怎么实现的呢?原型模式是这个问题的一个解决方案:I* i2 = I .clone();

#2


6  

Here is reference from GoF book:

以下是GoF书籍的参考:

clients that clone the prototype don't have to know about their concrete subclasses. Clients should never need to downcast the return value of Clone to the desired type.

克隆原型的客户不需要知道他们的具体子类。客户永远都不需要将克隆的返回值降至所需的类型。

在c++中,原型设计模式和复制构造函数有什么不同

For for you example, the client is NOT aware (or cannot access) of ConcretePrototype.

例如,客户端不知道(或不能访问)具象原型。

// Usage:
SomeMethod(PrototypeInterface iproto) // Thanks Prototype pattern
{
  // ConcretePrototype proto;     
  auto protPtr = iproto.clone(); // Gets correct object without calling concrete object's constructor explicitly.
}

Hope you understood the situation when this pattern is useful. Also Remember, there is no virtual constructor.

希望您理解此模式的使用情况。还要记住,没有虚拟构造函数。