Add Prototype Pattern
parent
d6ead5544a
commit
e539d4bbb4
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
|
@ -0,0 +1,70 @@
|
|||
#ifndef __PROTOTYPE_PATTERN__
|
||||
#define __PROTOTYPE_PATTERN__
|
||||
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
using namespace std;
|
||||
|
||||
//work model类
|
||||
class WorkModel
|
||||
{
|
||||
public:
|
||||
char *modelName;
|
||||
void setWorkModelName(char *iName){
|
||||
this->modelName = iName;
|
||||
}
|
||||
};
|
||||
|
||||
//抽象原型类PrototypeWork
|
||||
class PrototypeWork
|
||||
{
|
||||
public:
|
||||
PrototypeWork(){}
|
||||
virtual PrototypeWork *clone() = 0;
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
//具体原型类ConcreteWork
|
||||
class ConcreteWork :public PrototypeWork
|
||||
{
|
||||
public:
|
||||
ConcreteWork(){}
|
||||
ConcreteWork(char* iName, int iIdNum, char* modelName){
|
||||
this->name = iName;
|
||||
this->idNum = iIdNum;
|
||||
this->workModel = new WorkModel();
|
||||
this->workModel->setWorkModelName(modelName);
|
||||
}
|
||||
|
||||
ConcreteWork *clone(){
|
||||
ConcreteWork *work = new ConcreteWork();
|
||||
work->setName(this->name);
|
||||
work->setIdNum(this->idNum);
|
||||
work->workModel = this->workModel;
|
||||
return work;
|
||||
}
|
||||
|
||||
void setName(char* iName){
|
||||
this->name = iName;
|
||||
}
|
||||
void setIdNum(int iIdNum){
|
||||
this->idNum = iIdNum;
|
||||
}
|
||||
void setModel(WorkModel *iWorkModel){
|
||||
this->workModel = iWorkModel;
|
||||
}
|
||||
//打印work信息
|
||||
void printWorkInfo(){
|
||||
printf("name:%s\t\n", this->name);
|
||||
printf("idNum:%d\t\n", this->idNum);
|
||||
printf("modelName:%s\t\n", this->workModel->modelName);
|
||||
}
|
||||
private:
|
||||
char* name;
|
||||
int idNum;
|
||||
WorkModel *workModel;
|
||||
};
|
||||
|
||||
#endif //__PROTOTYPE_PATTERN__
|
|
@ -0,0 +1,52 @@
|
|||
#include "PrototypePattern.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
#if 0
|
||||
ConcreteWork *singleWork = new ConcreteWork("Single",1001,"Single_Model");
|
||||
printf("\nSingle的作业:\n");
|
||||
singleWork->printWorkInfo();
|
||||
|
||||
printf("\njungle直接抄作业……\n");
|
||||
ConcreteWork *jungleWork = singleWork;
|
||||
printf("\nJungle的作业:\n");
|
||||
jungleWork->printWorkInfo();
|
||||
|
||||
//抄完改名字和学号,否则会被老师查出来
|
||||
printf("\njungle抄完改名字和学号,否则会被老师查出来……\n");
|
||||
jungleWork->setName("jungle");
|
||||
jungleWork->setIdNum(1002);
|
||||
WorkModel *jungleModel = new WorkModel();
|
||||
jungleModel->setWorkModelName("Jungle_Model");
|
||||
jungleWork->setModel(jungleModel);
|
||||
|
||||
//检查下是否改对了
|
||||
printf("\nSingle的作业:\n");
|
||||
singleWork->printWorkInfo();
|
||||
printf("\nJungle的作业:\n");
|
||||
jungleWork->printWorkInfo();
|
||||
#endif
|
||||
|
||||
ConcreteWork *singleWork = new ConcreteWork("Single", 1001, "Single_Model");
|
||||
printf("\nSingle的作业:\n");
|
||||
|
||||
ConcreteWork *jungleWork = singleWork->clone();
|
||||
printf("\nJungle的作业:\n");
|
||||
|
||||
//抄完改名字和学号,否则会被老师查出来
|
||||
printf("\njungle抄完改名字和学号,否则会被老师查出来……\n");
|
||||
jungleWork->setName("jungle");
|
||||
jungleWork->setIdNum(1002);
|
||||
WorkModel *jungleModel = new WorkModel();
|
||||
jungleModel->setWorkModelName("Jungle_Model");
|
||||
jungleWork->setModel(jungleModel);
|
||||
|
||||
//检查下是否改对了
|
||||
printf("\nSingle的作业:\n");
|
||||
singleWork->printWorkInfo();
|
||||
printf("\nJungle的作业:\n");
|
||||
jungleWork->printWorkInfo();
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue