/
main.cpp
71 lines (53 loc) 路 1.4 KB
/
main.cpp
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
61
62
63
64
65
66
67
68
69
70
71
/*
* @FileName : prototype/main.cpp
* @CreateAt : 2022/4/9
* @Author : Inno Fang
* @Email : innofang@yeah.net
* @Description: Simple implementation of Prototype pattern
*/
#include <iostream>
#include <string>
class Prototype {
public:
Prototype() : _value("DEFAULT") {}
virtual ~Prototype() = default;
void setValue(const std::string &value) {
_value = value;
}
virtual void show() = 0;
virtual Prototype *clone() = 0;
protected:
std::string _value;
};
class ConcretePrototype : public Prototype {
public:
ConcretePrototype() = default;
~ConcretePrototype() = default;
ConcretePrototype(const ConcretePrototype &rhs) : Prototype(rhs) {
_value = rhs._value;
}
void setId(const size_t &id) {
_id = id;
}
void show() override {
std::cout << "ConcretePrototype with value (" << _value << ") has id: " << _id << std::endl;
}
ConcretePrototype *clone() override {
return new ConcretePrototype(*this);
}
private:
size_t _id{};
};
int main() {
Prototype *type_a = new ConcretePrototype();
type_a->show();
type_a->setValue("Concrete type A");
type_a->show();
ConcretePrototype *type_a_with_id = new ConcretePrototype();
type_a_with_id->show();
type_a_with_id->setId(123);
type_a_with_id->show();
delete type_a_with_id;
delete type_a;
return 0;
}