[创建型模式] Prototype

Prototype.h

//
//  Prototype.h
//  Prototype
//
//  Created by Cheney Shen on 11-2-20.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#ifndef _PROTOTYPE_H_
#define _PROTOTYPE_H_

class Prototype
{
    public:
    virtual ~Prototype();
    virtual Prototype* Clone() const = 0;
    
    protected:
    Prototype();
    
    private:
    
};

class ConcretePrototype:public Prototype
{
    public:
    ConcretePrototype();
    ConcretePrototype(const ConcretePrototype& cp);
    ~ConcretePrototype();
    Prototype* Clone() const;
    
    protected:
    
    private:
    
};

#endif  //~_PROTOTYPE_H_

Prototype.cpp

//
//  Prototype.cpp
//  Prototype
//
//  Created by Cheney Shen on 11-2-20.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#include "Prototype.h"
#include <iostream>
using namespace std;

Prototype::Prototype()
{
    
}

Prototype::~Prototype()
{
    
}

Prototype* Prototype::Clone() const
{
    return 0;
}

ConcretePrototype::ConcretePrototype()
{
    
}

ConcretePrototype::~ConcretePrototype()
{
    
}

ConcretePrototype::ConcretePrototype(const ConcretePrototype& cp)
{
    cout<<"ConcretePrototype copy..."<<endl;
}

Prototype* ConcretePrototype::Clone() const
{
    return new ConcretePrototype(*this);
}

main.cpp

//
//  main.cpp
//  Prototype
//
//  Created by Cheney Shen on 11-2-20.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//
#include "Prototype.h"
#include <iostream>
using namespace std;

int main (int argc, const char * argv[]) {

    Prototype* p = new ConcretePrototype();
    Prototype* p1 = p->Clone();
    delete p1;
    p1 = NULL;
    return 0;
}

posted on 2011-03-10 03:05  Cheney Shen  阅读(158)  评论(0编辑  收藏  举报

导航