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
| // 동작 객체의 인터페이스
class IEvent
{
public:
virtual std::string name() = 0;
};
// 동작 객체 A
class AEvent :
public IEvent
{
public:
std::string name() override
{
return "A";
}
};
// 동작 객체 B
class BEvent :
public IEvent
{
public:
std::string name() override
{
return "B";
}
};
// 핸들러 인터페이스 클래스
class IHandler
{
public:
IHandler(std::shared_ptr<IHandler> handler) :
m_handler(handler)
{
}
virtual void event(std::shared_ptr<IEvent> user)
{
if (m_handler != nullptr)
{
m_handler->event(user);
}
}
private:
std::shared_ptr<IHandler> m_handler;
};
// 핸들러 템플릿
template<typename T>
class HandlerTemplate :
public IHandler
{
public:
HandlerTemplate(std::shared_ptr<IHandler> handler) :
IHandler(handler)
{
}
std::string event(std::shared_ptr<IEvent> user) override
{
if (dynamic_cast<std::shared_ptr<T>>(user) != nullptr)
{
std::cout << user->name << " Event" << std::endl;
}
else
{
IHandler::event(user);
}
}
};
|