Prototype 模式

原型模式用于软件开发,当要创建的对象类型由原型实例确定时,该原型实例被克隆以生成新对象。这种方式应用于以标准方式创建对象的代价比较大时,采用这种方式。

实现 声明一个基类指定以个特殊的纯虚函数 virtual clone()方法。任何需要“多态构造函数”的类能力的类都需要从它派生出来并且实现clone()函数。

这儿有一个客户端代码,这个工厂模式,依赖于参数,查找具体的创建类。在具体类中,clone()方法调用,并且返回工厂方法。

如下 是一个例子:

Record 类,是一个纯虚类,包含纯虚函数clone()

CarRecord,BikeRecord,和 PersonRecord是Record类的具体的类

一个枚举类型 RecordType作为一个一对一的映射,去实现每一个Record class的实现。

RecordFactory 类有一个工厂方法 CreateRecord(...).这个方法需要一个枚举类型作为参数,并且依赖于该参数返回Record class 的具体实现。


/** Implementation of Prototype Method **/
#include <iostream>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;

/** Record is the base Prototype */
class Record
{
public:
	virtual ~Record() {}
	virtual void print() = 0;
	virtual shared_ptr<Record> clone() = 0;
};

/** CarRecord is a Concrete Prototype */
class CarRecord : public Record
{
private:
	string m_carName;
	int m_ID;

public:
	CarRecord(string carName, int ID) : m_carName(carName), m_ID(ID)
	{
	}

	void print() override
	{
		cout << "Car Record" << endl
		     << "Name  : "   << m_carName << endl
		     << "Number: "   << m_ID << endl << endl;
	}

	shared_ptr<Record> clone() override
	{
		return make_shared<CarRecord>(*this);
	}
};

/** BikeRecord is the Concrete Prototype */
class BikeRecord : public Record
{
private:
	string m_bikeName;
	int m_ID;

public:
	BikeRecord(string bikeName, int ID) : m_bikeName(bikeName), m_ID(ID)
	{
	}

	void print() override
	{
		cout << "Bike Record" << endl
		     << "Name  : " << m_bikeName << endl
		     << "Number: " << m_ID << endl << endl;
	}

	shared_ptr<Record> clone() override
	{
		return make_shared<BikeRecord>(*this);
	}
};

/** PersonRecord is the Concrete Prototype */
class PersonRecord : public Record
{
private:
	string m_personName;
	int m_age;

public:
	PersonRecord(string personName, int age) : m_personName(personName), m_age(age)
	{
	}

	void print() override
	{
		cout << "Person Record" << endl
			<< "Name : " << m_personName << endl
			<< "Age  : " << m_age << endl << endl;
	}

	shared_ptr<Record> clone() override
	{
		return make_shared<PersonRecord>(*this);
	}
};

/** Opaque record type, avoids exposing concrete implementations */
enum RecordType
{
	CAR,
	BIKE,
	PERSON
};

/** RecordFactory is the client */
class RecordFactory
{
private:
	unordered_map<RecordType, shared_ptr<Record>, hash<int> > m_records;

public:
	RecordFactory()
	{
		m_records[CAR]    = make_shared<CarRecord>("Ferrari", 5050);
		m_records[BIKE]   = make_shared<BikeRecord>("Yamaha", 2525);
		m_records[PERSON] = make_shared<PersonRecord>("Tom", 25);
	}

	shared_ptr<Record> createRecord(RecordType recordType)
	{
		return m_records[recordType]->clone();
	}
};
#include <iostream>
#include "prototype.cpp"
using namespace std;

int main()
{
    RecordFactory recordFactory;

	auto record = recordFactory.createRecord(CAR);
	record->print();

	record = recordFactory.createRecord(BIKE);
	record->print();

	record = recordFactory.createRecord(PERSON);
	record->print();
    return 0;
}

另外一个例子,为了实现该模式,声明一个抽象基类,指定一个纯虚函数 clone()作为成员变量。任何一个类想实现多态构造函数的能力必须继承该抽象基类,并且实现clone()函数。

客户端代码,不是通过触发new操作实现,而是调用clone()成员函数,调用工厂成员函数设计特殊的具体派生类。或者触发clone()机制


猜你喜欢

转载自blog.csdn.net/Docterzzz/article/details/80273832