[创建型模式] 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;

}

你可能感兴趣的:(prototype)