class Prototype
{
public:
    virtual Prototype *Clone() = 0;
};
class PrototypeA : public Prototype
{
public:
    virtual Prototype *Clone() override
    {
        return new PrototypeA(*this);
    }
};
class PrototypeB : public Prototype
{
public:
    virtual Prototype *Clone() override
    {
        return new PrototypeB(*this);
    }
};

void foo(Prototype &p)
{
    // Prototype 模式优势在于减少 Factory 模式引入的大量子类.
    Prototype *bar = p->Clone();

    // ...

    // TODO: Release memory.
}

 

相关文章:

  • 2021-09-14
  • 2021-06-20
  • 2021-12-04
  • 2021-07-14
  • 2022-01-01
  • 2021-07-15
  • 2021-11-13
猜你喜欢
  • 2021-10-30
  • 2021-10-11
  • 2021-08-25
  • 2021-05-27
相关资源
相似解决方案