-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrototypeMethod.h
61 lines (49 loc) · 1.21 KB
/
PrototypeMethod.h
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
55
56
57
58
59
60
#pragma once
#include <stdio.h>
#include <iostream>
class ISplitterPrototype {
public:
virtual void split() = 0;
virtual ISplitterPrototype* clone() = 0; //通过克隆自己来创建指针
virtual ~ISplitterPrototype() { }
};
//具体类
class BinarySplitterPrototype : public ISplitterPrototype
{
public:
BinarySplitterPrototype() {
i = 1;
}
virtual ISplitterPrototype* clone() {
cout << "BinarySplitter::clone" << endl;
return new BinarySplitterPrototype(*this); //克隆自己,通过拷贝构造
}
void split() {
printf("i = %x\n", &i);
}
private:
int i;
};
class TxtSplitterPrototype : public ISplitterPrototype
{
virtual ISplitterPrototype* clone() {
cout << "BinarySplitter::clone" << endl;
return new TxtSplitterPrototype(*this); //克隆自己,通过拷贝构造
}
void split() {}
};
class PictureSplitterPrototype : public ISplitterPrototype
{
virtual ISplitterPrototype* clone() {
cout << "BinarySplitter::clone" << endl;
return new PictureSplitterPrototype(*this); //克隆自己,通过拷贝构造
}
void split() {}
};
class VideoSplitterPrototype : public ISplitterPrototype
{
virtual ISplitterPrototype* clone() {
return new VideoSplitterPrototype(*this); //克隆自己,通过拷贝构造进行深克隆
}
void split() {}
};