-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype design pattern.cpp
More file actions
66 lines (54 loc) · 1.42 KB
/
Copy pathprototype design pattern.cpp
File metadata and controls
66 lines (54 loc) · 1.42 KB
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
#include <iostream>
#include <string>
using namespace std;
// Prototype Interface
class Prototype {
public:
virtual Prototype* clone() const = 0;
virtual void use() const = 0;
virtual ~Prototype() = default;
};
// Concrete Prototype 1
class ConcretePrototype1 : public Prototype {
private:
string name;
public:
ConcretePrototype1(const string& name) : name(name) {}
Prototype* clone() const override {
return new ConcretePrototype1(*this);
}
void use() const override {
cout << "Using ConcretePrototype1: " << name << endl;
}
};
// Concrete Prototype 2
class ConcretePrototype2 : public Prototype {
private:
string name;
public:
ConcretePrototype2(const string& name) : name(name) {}
Prototype* clone() const override {
return new ConcretePrototype2(*this);
}
void use() const override {
cout << "Using ConcretePrototype2: " << name << endl;
}
};
// Client code
int main() {
// Create prototypes
Prototype* prototype1 = new ConcretePrototype1("Prototype1");
Prototype* prototype2 = new ConcretePrototype2("Prototype2");
// Clone prototypes
Prototype* clone1 = prototype1->clone();
Prototype* clone2 = prototype2->clone();
// Use clones
clone1->use();
clone2->use();
// Clean up
delete prototype1;
delete prototype2;
delete clone1;
delete clone2;
return 0;
}