Design Pattern --- Prototype

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.
}

 

posted @ 2013-01-22 09:41  walfud  阅读(121)  评论(0编辑  收藏  举报