-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateMethod.cpp
More file actions
123 lines (105 loc) · 3.96 KB
/
Copy pathTemplateMethod.cpp
File metadata and controls
123 lines (105 loc) · 3.96 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// cppcheck-suppress-file [functionStatic]
// is a behavioral design pattern that defines the skeleton of an algorithm in the `superclass`
// but lets `subclasses` override specific steps of the algorithm without changing its structure.
// Appicability:
// (*) when you want to let clients extend only particular steps of an algorithm, but not the whole algorithm or its structure.
// (**) when you have several classes that contain almost identical algorithms with some minor differences.
// As a result, you might need to modify all classes when the algorithm changes.
// (**) when a behavior makes sense only in some classes of a class hierarchy, but not in others.
// UML: docs/uml/patterns_behavioral_templatemethod.drawio.svg
#include <iostream>
namespace
{
namespace TemplateMethod
{
class AbstractClass
{
public:
virtual ~AbstractClass() = default;
// Template Method (non-virtual => cannot be overridden)
// Defines the algorithm's skeleton and ensures subclasses cannot change the flow.
void templateMethod() const
{
baseOperation1();
abstractMethod1();
hookOperation1();
baseOperation2();
abstractMethod2();
hookOperation2();
}
protected:
// 1. Base operations: These have full implementations and cannot be overridden.
void baseOperation1() const
{
// Common logic step 1
std::cout << "[AbstractClass]\t Executed base operation - 1\n";
}
void baseOperation2() const
{
// Common logic step 2
std::cout << "[AbstractClass]\t Executed base operation - 2\n";
}
// 2. Hook methods: Subclasses may override them to extend behavior, but overriding is optional.
virtual void hookOperation1() const {}
virtual void hookOperation2() const {}
// 3. Abstract methods: Subclasses MUST provide implementations.
virtual void abstractMethod1() const = 0;
virtual void abstractMethod2() const = 0;
};
class ConcreteClass1 : public AbstractClass
{
public:
void abstractMethod1() const override
{
std::cout << "[ConcreteClass1]\t Implemented Operation - 2\n";
}
void abstractMethod2() const override
{
std::cout << "[ConcreteClass1]\t Implemented Operation - 2\n";
}
void hookOperation1() const override
{
std::cout << "[ConcreteClass1]\t Overridden Hook - 1\n";
}
};
class ConcreteClass2 : public AbstractClass
{
public:
void abstractMethod1() const override
{
std::cout << "[ConcreteClass2]\t Implemented Operation - 2\n";
}
void abstractMethod2() const override
{
std::cout << "[ConcreteClass2]\t Implemented Operation - 2\n";
}
};
namespace Client
{
void clientCode(const AbstractClass *clazz)
{
clazz->templateMethod();
}
}
void run()
{
std::cout << "\t[ConcreteClass1] Executed templateMethod\n";
AbstractClass *clazz1 = new ConcreteClass1();
Client::clientCode(clazz1);
std::cout << "\t[ConcreteClass1] Executed templateMethod\n";
AbstractClass *clazz2 = new ConcreteClass2();
Client::clientCode(clazz2);
delete clazz1;
delete clazz2;
}
}
}
struct TemplateMethodAutoRunner
{
TemplateMethodAutoRunner()
{
std::cout << "\n--- TemplateMethod Pattern Example ---\n";
TemplateMethod::run();
}
};
static TemplateMethodAutoRunner instance;