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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#include <iostream>
#include <memory>
// 抽象接口类
class IDrawable
{
public:
virtual ~IDrawable() = default; // 默认虚析构函数
virtual void Draw() const = 0;
};
class ISerializable
{
public:
virtual ~ISerializable() = default;
virtual std::string Serialize() const = 0;
virtual void Deserialize(const std::string& data) = 0;
};
// 具体实现类
class Button : public IDrawable, public ISerializable
{
private:
std::string m_Text;
int m_Width, m_Height;
public:
Button(const std::string& text, int width, int height)
: m_Text(text), m_Width(width), m_Height(height)
{
std::cout << "Button created: " << m_Text << std::endl;
}
~Button()
{
std::cout << "Button destroyed: " << m_Text << std::endl;
}
void Draw() const override
{
std::cout << "Drawing button: " << m_Text
<< " (" << m_Width << "x" << m_Height << ")" << std::endl;
}
std::string Serialize() const override
{
return "Button{text:" + m_Text + ",width:" + std::to_string(m_Width)
+ ",height:" + std::to_string(m_Height) + "}";
}
void Deserialize(const std::string& data) override
{
std::cout << "Deserializing button from: " << data << std::endl;
// 简化的反序列化逻辑
}
};
class Label : public IDrawable, public ISerializable
{
private:
std::string m_Text;
std::string m_Font;
public:
Label(const std::string& text, const std::string& font)
: m_Text(text), m_Font(font)
{
std::cout << "Label created: " << m_Text << std::endl;
}
~Label()
{
std::cout << "Label destroyed: " << m_Text << std::endl;
}
void Draw() const override
{
std::cout << "Drawing label: " << m_Text << " (font: " << m_Font << ")" << std::endl;
}
std::string Serialize() const override
{
return "Label{text:" + m_Text + ",font:" + m_Font + "}";
}
void Deserialize(const std::string& data) override
{
std::cout << "Deserializing label from: " << data << std::endl;
}
};
// 工厂函数
std::unique_ptr<IDrawable> CreateWidget(const std::string& type)
{
if (type == "button")
return std::make_unique<Button>("Click Me", 100, 30);
else if (type == "label")
return std::make_unique<Label>("Hello World", "Arial");
else
return nullptr;
}
void InterfaceDemo()
{
std::cout << "=== Interface Demo ===" << std::endl;
auto button = CreateWidget("button");
auto label = CreateWidget("label");
if (button)
{
button->Draw();
// 尝试转换为ISerializable
if (auto serializable = dynamic_cast<ISerializable*>(button.get()))
{
std::cout << "Serialized: " << serializable->Serialize() << std::endl;
}
}
if (label)
{
label->Draw();
if (auto serializable = dynamic_cast<ISerializable*>(label.get()))
{
std::cout << "Serialized: " << serializable->Serialize() << std::endl;
}
}
std::cout << "Widgets will be automatically destroyed..." << std::endl;
}
int main()
{
InterfaceDemo();
return 0;
}
|