C++设计模式 ==> 原型模式

简介

        所谓原型模式就是用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象。举一个简单的例子,一个人想要批量分发自己的名片,然而名片千篇一律,只是偶尔修改一些地方,但需求量大,所以需要不断实例化名片类,十分麻烦,所以这就有了原型模式。原型模式旨在以某一类为原型,利用Clone成员函数不断拷贝出新对象。
        在C++中,有浅拷贝和深拷贝两种拷贝模式,默认赋值采用浅拷贝的方法,但浅拷贝会共享分配的堆栈空间,析构时会两次释放空间导致程序崩溃,所以原型模式一般直接使用深拷贝,由拷贝构造函数实现。
        下面我们就以印名片为例子为大家讲解一下原型模式的用法。

图示

原型模式图示

代码实现

////////////////////////////
//
// FileName : ProtoTypeDefine.h
// Editor : PeterZheng
// Date : 2018/8/16 18:00
//
////////////////////////////

#pragma once

#ifndef PROTOTYPE_H_
#define PROTOTYPE_H_

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <windows.h>

#endif

#define MAX_LENGTH 255

//克隆抽象类
class ProtoType
{
public:
	virtual ProtoType* Clone() = 0;
};

//克隆具体实现类(名片类)
class ConcreteProtoType
{
public:
	int age;
	char* name;
	char* school;
	ConcreteProtoType(int a, const char* name, const char* school)
	{
		this->age = a;
		this->name = new char[MAX_LENGTH];
		this->school = new char[MAX_LENGTH];
		ZeroMemory(this->name, MAX_LENGTH);
		ZeroMemory(this->school, MAX_LENGTH);
		strcpy_s(this->name, MAX_LENGTH, name);
		strcpy_s(this->school, MAX_LENGTH, school);
	}
	ConcreteProtoType(const ConcreteProtoType& pt)
	{
		this->age = pt.age;
		this->name = new char[MAX_LENGTH];
		this->school = new char[MAX_LENGTH];
		ZeroMemory(this->name, MAX_LENGTH);
		ZeroMemory(this->school, MAX_LENGTH);
		strcpy_s(this->name, MAX_LENGTH, pt.name);
		strcpy_s(this->school, MAX_LENGTH, pt.school);
	}
	~ConcreteProtoType() {}
	ConcreteProtoType* Clone()
	{
		return new ConcreteProtoType(*this);
	}
	void Show()
	{
		std::cout << "Name: " << this->name << std::endl << "Age: " << this->age << std::endl << "School: " << this->school << std::endl;
	}
};

////////////////////////////
//
// FileName : ProtoTypeDemo.cpp
// Editor : PeterZheng
// Date : 2018/8/16 19:22
//
////////////////////////////

#include "ProtoTypeDefine.h"

using namespace std;

int main(void)
{
	ConcreteProtoType *mp1 = new ConcreteProtoType(28, "XiaoMin", "CMU");
	ConcreteProtoType *mp2 = mp1->Clone();
	//名片的学校需要改为MIT
	ConcreteProtoType *mp3 = new ConcreteProtoType(28, "XiaoMin", "MIT");
	ConcreteProtoType *mp4 = mp3->Clone();
	mp1->Show();
	cout << endl;
	mp2->Show();
	cout << endl;
	mp3->Show();
	cout << endl;
	system("pause");
	return 0;
}
posted @ 2018-08-24 21:27  倚剑问天  阅读(179)  评论(0编辑  收藏  举报