-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassicalVisitor.hpp
111 lines (100 loc) · 2.46 KB
/
ClassicalVisitor.hpp
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
#pragma once
#include <string>
#include <iostream>
namespace Jason {
namespace JClassicalVisitor {
class Triangle;
class Polygon;
class Cat;
class Dog;
class IVisitor
{
public:
virtual ~IVisitor() = default;
virtual void Visit(Triangle* triangle) = 0;
virtual void Visit(Polygon* polygon) = 0;
virtual void Visit(Cat* cat) = 0;
virtual void Visit(Dog* dog) = 0;
};
class IElement
{
public:
virtual ~IElement() = default;
virtual void Accept(IVisitor* visitor) = 0;
};
class Figure : public IElement {};
class Triangle : public Figure
{
public:
std::string name{ "Figure" };
void Accept(IVisitor* visitor) override
{
visitor->Visit(this);
}
};
class Polygon : public Figure
{
public:
std::string name{ "Polygon" };
void Accept(IVisitor* visitor) override
{
visitor->Visit(this);
}
};
class Animal : public IElement {};
class Cat : public Animal
{
public:
std::string name{ "Cat" };
void Accept(IVisitor* visitor) override
{
visitor->Visit(this);
}
};
class Dog : public Animal
{
public:
std::string name{ "Dog" };
void Accept(IVisitor* visitor) override
{
visitor->Visit(this);
}
};
class Draw : public IVisitor
{
public:
~Draw() {};
void Visit(Triangle* triangle) override
{
std::cout << "Draw : " << triangle->name << std::endl;
}
void Visit(Polygon* polygon) override
{
std::cout << "Draw : " << polygon->name << std::endl;
}
void Visit(Cat* cat) override {}
void Visit(Dog* dog) override {}
};
class Serialize : public IVisitor
{
public:
~Serialize() {};
void Visit(Triangle* triangle) override
{
std::cout << "Serialize : " << triangle->name << std::endl;
}
void Visit(Polygon* polygon) override
{
std::cout << "Serialize : " << polygon->name << std::endl;
}
void Visit(Cat* cat) override
{
std::cout << "Serialize : " << cat->name << std::endl;
}
void Visit(Dog* dog) override
{
std::cout << "Serialize : " << dog->name << std::endl;
}
};
} // JClassicalVisitor
} // Jason