美文网首页
原型模式

原型模式

作者: 钟离惜 | 来源:发表于2020-11-05 14:11 被阅读0次
    #include <iostream>
    using namespace std;
    
    class IPrototype
    {
    public:
        virtual void ShowClass() = 0;
        virtual IPrototype* Copy() = 0;
    };
    class ClassA: public IPrototype
    {
    public:
        virtual void ShowClass() { cout << "ClassA" << endl; }
        virtual IPrototype* Copy() { return new ClassA; }
    };
    class ClassB : public IPrototype
    {
    public:
        virtual void ShowClass() { cout << "ClassB" << endl; }
        virtual IPrototype* Copy() { return new ClassB; }
    };
    class ClassC : public IPrototype
    {
    public:
        virtual void ShowClass() { cout << "ClassC" << endl; }
        virtual IPrototype* Copy() { return new ClassC; }
    };
    
    int main()
    {
        IPrototype* calculate = NULL;
        IPrototype* copy = NULL;
        calculate = new ClassA;
        copy = calculate->Copy();
        calculate->ShowClass();
        copy->ShowClass();
        delete calculate;
        delete copy;
        calculate = new ClassB;
        copy = calculate->Copy();
        calculate->ShowClass();
        copy->ShowClass();
        delete calculate;
        delete copy;
        calculate = new ClassC;
        copy = calculate->Copy();
        calculate->ShowClass();
        copy->ShowClass();
        delete calculate;
        delete copy;
        calculate = NULL;
        copy = NULL;
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:原型模式

          本文链接:https://www.haomeiwen.com/subject/hzrxvktx.html