原型模式
C++
参考:https://www.cnblogs.com/Galesaur-wcy/p/15924300.html
#include <iostream>
#include <string>
using namespace std;
class WorkExprerience
{
private:
string workDate;
string company;
public:
WorkExprerience() {}
~WorkExprerience() {}
WorkExprerience(WorkExprerience *work)
{
this->workDate = work->workDate;
this->company = work->company;
}
string getWorkDate()
{
return workDate;
}
void setWorkDate(string workDate)
{
this->workDate = workDate;
}
string getCompany()
{
return company;
}
void setCompany(string company)
{
this->company = company;
}
WorkExprerience *Clone()
{
return new WorkExprerience(this);
}
};
class Prototype
{
protected:
string name;
string sex;
string age;
string timeArea;
string company;
public:
virtual void SetPersonalInfo(string sex, string age) = 0;
virtual void Display() = 0;
virtual Prototype &Clone() = 0;
};
class Resume : public Prototype
{
private:
WorkExprerience *workExp;
public:
Resume(string name)
{
this->name = name;
workExp = new WorkExprerience();
}
~Resume()
{
if (workExp != NULL)
delete workExp;
}
void SetPersonalInfo(string sex, string age)
{
this->sex = sex;
this->age = age;
}
void SetWorkExperience(string workDate, string company)
{
workExp->setCompany(company);
workExp->setWorkDate(workDate);
}
void Display()
{
cout << name << " " << age << " " << sex << " " << endl;
cout << "工作经历 " << workExp->getWorkDate() << " " << workExp->getCompany() << endl;
}
Resume &Clone()
{
Resume *clonedResume = new Resume(name);
clonedResume->SetPersonalInfo(sex, age);
return *clonedResume;
}
};
int main()
{
Resume *a = new Resume("big bird");
a->SetPersonalInfo("MAN", "22");
a->SetWorkExperience("1998-2000", "baidu");
Resume b = a->Clone();
b.SetWorkExperience("2000-2008", "google");
Resume c = a->Clone();
c.SetWorkExperience("2010-2018", "apple");
a->Display();
b.Display();
c.Display();
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char workDate[50];
char company[50];
} WorkExperience;
typedef struct
{
char name[50];
char sex[10];
char age[10];
char timeArea[50];
char company[50];
} PersonalInfo;
typedef struct Resume Resume;
struct Resume
{
PersonalInfo info;
WorkExperience *workExp;
void (*display)(Resume *);
Resume *(*clone)(Resume *);
};
void DisplayResume(Resume *resume);
Resume *CloneResume(Resume *original);
void InitWorkExperience(WorkExperience *exp)
{
strcpy(exp->workDate, "");
strcpy(exp->company, "");
}
void SetWorkExperience(WorkExperience *exp, const char *date, const char *company)
{
strcpy(exp->workDate, date);
strcpy(exp->company, company);
}
void DisplayWorkExperience(const WorkExperience *exp)
{
printf("工作经历 %s %s\n", exp->workDate, exp->company);
}
Resume *CreateResume(const char *name)
{
Resume *resume = (Resume *)malloc(sizeof(Resume));
strcpy(resume->info.name, name);
strcpy(resume->info.sex, "");
strcpy(resume->info.age, "");
strcpy(resume->info.timeArea, "");
strcpy(resume->info.company, "");
resume->workExp = (WorkExperience *)malloc(sizeof(WorkExperience));
InitWorkExperience(resume->workExp);
resume->display = DisplayResume;
resume->clone = CloneResume;
return resume;
}
void DestroyResume(Resume *resume)
{
if (resume)
{
if (resume->workExp)
free(resume->workExp);
free(resume);
}
}
void SetPersonalInfo(Resume *resume, const char *sex, const char *age)
{
strcpy(resume->info.sex, sex);
strcpy(resume->info.age, age);
}
void SetWorkExperienceForResume(Resume *resume, const char *date, const char *company)
{
SetWorkExperience(resume->workExp, date, company);
}
void DisplayResume(Resume *resume)
{
printf("%s %s %s \n", resume->info.name, resume->info.age, resume->info.sex);
DisplayWorkExperience(resume->workExp);
}
Resume *CloneResume(Resume *original)
{
Resume *cloned = CreateResume(original->info.name);
strcpy(cloned->info.sex, original->info.sex);
strcpy(cloned->info.age, original->info.age);
SetWorkExperienceForResume(cloned, original->workExp->workDate, original->workExp->company);
return cloned;
}
int main()
{
Resume *a = CreateResume("big bird");
SetPersonalInfo(a, "MAN", "22");
SetWorkExperienceForResume(a, "1998-2000", "baidu");
Resume *b = a->clone(a);
SetWorkExperienceForResume(b, "2000-2008", "google");
Resume *c = a->clone(a);
SetWorkExperienceForResume(c, "2010-2018", "apple");
a->display(a);
b->display(b);
c->display(c);
DestroyResume(a);
DestroyResume(b);
DestroyResume(c);
return 0;
}
总结
关键在于实现一个Clone函数!!!
与拷贝构造函数相比就是:方便扩展,动态灵活定义拷贝行为(而且可被继承)