Prototype模式

Prototype模式通过复制原型(Prototype)而获得新对象创建的功能,这里Prototype本身就是“对象工厂”

prototype.h:

#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:

#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:

#include "prototype.h"

#include <iostream>
using namespace std;


int main(){

	Prototype* p = new ConcretePrototype();
	Prototype* p1 = p->Clone();

	return 1;
}




你可能感兴趣的:(设计模式)