[img]https://api5.yunpan.360.cn/intf.php?method=Share.getPublicThumbByNid&qid=108635719&nid=13770720612900526&size=800_600&devtype=web&v=1.0.1&rtick=14676155696459&share_qid=108635719&sign=bd35d146f155d7b61458af303d9f7965&[/img]
[img]http://chuantu.biz/t5/9/1465887995x3738746553.jpg[/img]
[img]http://chuantu.biz/t5/9/1465888029x3738746553.jpg[/img]
[color=red][b]头文件[/b][/color]
[color=red][b]实现[/b][/color]
[color=red][b]客户端[/b][/color]
[color=red][b]运行结果[/b][/color]
[img]http://chuantu.biz/t4/12/1462868650x3738746553.jpg[/img]
[img]http://chuantu.biz/t4/12/1462868673x3738746553.jpg[/img]
[img]http://chuantu.biz/t5/9/1465887695x3738746553.jpg[/img]
[color=red][b]结构类图[/b][/color]
[img]http://code.uml.com.cn/code/images/Prototype1.png[/img]
[color=red][b]用例模型[/b][/color]
[img]http://code.uml.com.cn/code/images/Prototype2.png[/img]
[img]http://chuantu.biz/t5/9/1465887739x3738746553.jpg[/img]
[color=red][b]设计类图[/b][/color]
[img]http://code.uml.com.cn/code/images/Prototype3.png[/img]
[color=red][b]顺序图[/b][/color]
[img]http://code.uml.com.cn/code/images/Prototype4.png[/img]
[img]http://chuantu.biz/t5/9/1465887995x3738746553.jpg[/img]
[img]http://chuantu.biz/t5/9/1465888029x3738746553.jpg[/img]
[color=red][b]头文件[/b][/color]
//PrototypePattern.h
#ifndef PROTOTYPE_PATTERN_H
#define PROTOTYPE_PATTERN_H
#include <Windows.h>
#include <string>
using namespace std;
namespace PrototypePattern
{
// Base Class
//
class AbstractPrototype
{
public:
AbstractPrototype();
virtual ~AbstractPrototype();
virtual AbstractPrototype* Clone() = 0;
};
//
class ConcretePrototypeA : public AbstractPrototype
{
public:
ConcretePrototypeA(string strName, int nId);
ConcretePrototypeA(ConcretePrototypeA* pPrototypeA);
virtual ~ConcretePrototypeA();
virtual AbstractPrototype* Clone();
private:
string m_strName;
int m_nID;
};
//
class ConcretePrototypeB : public AbstractPrototype
{
public:
ConcretePrototypeB(string strName, int nId);
ConcretePrototypeB(ConcretePrototypeB* pPrototypeA);
virtual ~ConcretePrototypeB();
virtual AbstractPrototype* Clone();
private:
string m_strName;
int m_nID;
};
//
void PrototypePattern_Test_A();
void PrototypePattern_Test_B();
}
#endif
[color=red][b]实现[/b][/color]
#include "PrototypePattern.h"
#include <iostream>
using namespace std;
namespace PrototypePattern
{
//
AbstractPrototype::AbstractPrototype()
{
}
AbstractPrototype::~AbstractPrototype()
{
}
//
ConcretePrototypeA::ConcretePrototypeA(string strName, int nId)
{
m_strName = strName;
m_nID = nId;
}
ConcretePrototypeA::ConcretePrototypeA(ConcretePrototypeA* pPrototypeA)
{
this->m_strName = pPrototypeA->m_strName;
this->m_nID = pPrototypeA->m_nID;
}
ConcretePrototypeA::~ConcretePrototypeA()
{
}
AbstractPrototype* ConcretePrototypeA::Clone()
{
cout << "ConcretePrototypeA::Clone\n";
cout << "m_strName=" << m_strName << "; m_nID =" << m_nID << ";\n";
return new ConcretePrototypeA(this);
}
//
ConcretePrototypeB::ConcretePrototypeB(string strName, int nId)
{
m_strName = strName;
m_nID = nId;
}
ConcretePrototypeB::ConcretePrototypeB(ConcretePrototypeB* pPrototypeA)
{
this->m_strName = pPrototypeA->m_strName;
this->m_nID = pPrototypeA->m_nID;
}
ConcretePrototypeB::~ConcretePrototypeB()
{
}
AbstractPrototype* ConcretePrototypeB::Clone()
{
cout << "ConcretePrototypeB::Clone\n";
cout << "m_strName=" << m_strName << "; m_nID =" << m_nID << ";\n";
return new ConcretePrototypeB(this);
}
//
void PrototypePattern_Test_A()
{
AbstractPrototype* pPrototype1 = new ConcretePrototypeA("ConcretePrototypeA", 1);
AbstractPrototype* pPrototype2 = pPrototype1->Clone();
AbstractPrototype* pPrototype3 = pPrototype2->Clone();
delete pPrototype3;
pPrototype3 = NULL;
delete pPrototype2;
pPrototype2 = NULL;
delete pPrototype1;
pPrototype1 = NULL;
}
void PrototypePattern_Test_B()
{
AbstractPrototype* pPrototype1 = new ConcretePrototypeA("ConcretePrototypeB", 2);
AbstractPrototype* pPrototype2 = pPrototype1->Clone();
AbstractPrototype* pPrototype3 = pPrototype2->Clone();
delete pPrototype3;
pPrototype3 = NULL;
delete pPrototype2;
pPrototype2 = NULL;
delete pPrototype1;
pPrototype1 = NULL;
}
}
[color=red][b]客户端[/b][/color]
#include "PrototypePattern.h"
#include <iostream>
using namespace std;
using namespace PrototypePattern;
void main()
{
PrototypePattern_Test_A();
PrototypePattern_Test_B();
}
[color=red][b]运行结果[/b][/color]
[img]http://chuantu.biz/t4/12/1462868650x3738746553.jpg[/img]
[img]http://chuantu.biz/t4/12/1462868673x3738746553.jpg[/img]
[img]http://chuantu.biz/t5/9/1465887695x3738746553.jpg[/img]
[color=red][b]结构类图[/b][/color]
[img]http://code.uml.com.cn/code/images/Prototype1.png[/img]
[color=red][b]用例模型[/b][/color]
[img]http://code.uml.com.cn/code/images/Prototype2.png[/img]
[img]http://chuantu.biz/t5/9/1465887739x3738746553.jpg[/img]
[color=red][b]设计类图[/b][/color]
[img]http://code.uml.com.cn/code/images/Prototype3.png[/img]
[color=red][b]顺序图[/b][/color]
[img]http://code.uml.com.cn/code/images/Prototype4.png[/img]