Kyle Chen's Blog

Action speaks louder than Words

0%

原型模式C++实现

原型模式有什么用?

原型模式怎么实现?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include <string.h>
using namespace std;

class Resume{
protected:
char* name;
public:
virtual Resume* clone() = 0;

};

class ConcreteResume:public Resume{
public:
ConcreteResume(char* str){
name = new char[strlen(str)+1];
strcpy(name,str);
}

ConcreteResume(const ConcreteResume& r1){
name = new char[strlen(r1.name)+1];
strcpy(name,r1.name);
}
ConcreteResume& operator&=(const ConcreteResume& r1){
if(this == &r1){return *this;}
delete[] name;
name = new char[strlen(r1.name)+1];
strcpy(name,r1.name);
return *this;
}
ConcreteResume* clone() override{
return new ConcreteResume(*this);
}
//debug
void PinrtInfo(){
for(int i=0;i<strlen(name);i++){
cout<<name[i]<<endl;
}
}
};

int main(){

char str[] = "happy";
ConcreteResume r1(str);
ConcreteResume r2(r1);
char str1[] = "helloworld";
ConcreteResume r3(str1);
r3 = r1;
// r3.PinrtInfo();
ConcreteResume *r4 = r3.clone();
r4->PinrtInfo();

}