44// UML: docs/uml/patterns_structural_bridge.drawio.svg
55
66#include < iostream>
7-
8- namespace Problem
7+ namespace
98{
10- class Widget
9+ namespace Problem
1110 {
12- public:
13- virtual ~Widget () = default ;
14- virtual std::string clickOn () const = 0;
15- };
16-
17- /* Concrete variations for Button */
18- class Button : public Widget
19- {
20- public:
21- virtual ~Button () = default ;
22-
23- public:
24- std::string clickOn () const override
11+ class Widget
2512 {
26- return " Click on: Button\n " ;
27- }
28- };
13+ public:
14+ virtual ~Widget () = default ;
15+ virtual std::string clickOn () const = 0;
16+ };
2917
30- class ButtonWindows : public Button
31- {
32- public:
33- std::string clickOn () const override
18+ /* Concrete variations for Button */
19+ class Button : public Widget
3420 {
35- return " [Linux]" + Button::clickOn ();
36- }
37- };
38-
39- class ButtonLinux : public Button
40- {
41- public:
42- std::string clickOn () const override
43- {
44- return " [Windows]" + Button::clickOn ();
45- }
46- };
21+ public:
22+ virtual ~Button () = default ;
4723
48- /* Concrete variations for Label */
49- class Label : public Widget
50- {
51- public:
52- virtual ~Label () = default ;
24+ public:
25+ std::string clickOn () const override
26+ {
27+ return " Click on: Button\n " ;
28+ }
29+ };
5330
54- public:
55- std::string clickOn () const override
31+ class ButtonWindows : public Button
5632 {
57- return " Click on: Label \n " ;
58- }
59- };
60-
61- class LabelWindows : public Label
62- {
63- public:
64- std::string clickOn () const override
33+ public:
34+ std::string clickOn () const override
35+ {
36+ return " [Linux] " + Button::clickOn ();
37+ }
38+ };
39+
40+ class ButtonLinux : public Button
6541 {
66- return " [Windows]" + Label::clickOn ();
67- }
68- };
69-
70- class LabelLinux : public Label
71- {
72- public:
73- std::string clickOn () const override
42+ public:
43+ std::string clickOn () const override
44+ {
45+ return " [Windows]" + Button::clickOn ();
46+ }
47+ };
48+
49+ /* Concrete variations for Label */
50+ class Label : public Widget
7451 {
75- return " [Linux]" + Label::clickOn ();
76- }
77- };
52+ public:
53+ virtual ~Label () = default ;
7854
79- /* Concrete variations for others widgets like Text,CCombo or new platform macOS etc*/
80- // [Problem 1] We have to write the Text/TextLinux ...
55+ public:
56+ std::string clickOn () const override
57+ {
58+ return " Click on: Label\n " ;
59+ }
60+ };
8161
82- namespace Client
83- {
84- void clientCode (const Widget *widget)
62+ class LabelWindows : public Label
8563 {
86- if (widget != nullptr )
87- std::cout << widget->clickOn ();
88- }
89- }
90-
91- void run ()
92- {
93- // [Problem 2] : Use the Bridge if you need to be able to switch implementations at runtime. how to exmaple for this
94- // still don't know
95- Widget *button = new ButtonWindows ();
96- Client::clientCode (button);
97- delete button;
98- }
99- }
64+ public:
65+ std::string clickOn () const override
66+ {
67+ return " [Windows]" + Label::clickOn ();
68+ }
69+ };
70+
71+ class LabelLinux : public Label
72+ {
73+ public:
74+ std::string clickOn () const override
75+ {
76+ return " [Linux]" + Label::clickOn ();
77+ }
78+ };
10079
101- namespace BridgePattern
102- {
103- /* *
104- * The Implementation defines the interface for all implementation classes. It
105- * doesn't have to match the Abstraction's interface. In fact, the two
106- * interfaces can be entirely different. Typically the Implementation interface
107- * provides only primitive Widgets, while the Abstraction defines higher-
108- * level Widgets based on those primitives.
109- */
110- class OsImplemetation
111- {
112- public:
113- virtual std::string clickOnImplement () const = 0;
114- virtual ~OsImplemetation () = default ;
115- };
80+ /* Concrete variations for others widgets like Text,CCombo or new platform macOS etc*/
81+ // [Problem 1] We have to write the Text/TextLinux ...
11682
117- class WindowsImplemetation : public OsImplemetation
118- {
119- public:
120- std::string clickOnImplement () const override
83+ namespace Client
12184 {
122- return " [Windows]" ;
85+ void clientCode (const Widget *widget)
86+ {
87+ if (widget != nullptr )
88+ std::cout << widget->clickOn ();
89+ }
12390 }
124- };
12591
126- class LinuxImplemetation : public OsImplemetation
127- {
128- public:
129- std::string clickOnImplement () const override
92+ void run ()
13093 {
131- return " [Linux]" ;
94+ // [Problem 2] : Use the Bridge if you need to be able to switch implementations at runtime. how to exmaple for this
95+ // still don't know
96+ Widget *button = new ButtonWindows ();
97+ Client::clientCode (button);
98+ delete button;
13299 }
133- };
100+ }
134101
135- /* *
136- * The Abstraction defines the interface for the "control" part of the two class
137- * hierarchies. It maintains a reference to an object of the Implementation
138- * hierarchy and delegates all of the real work to this object.
139- */
140- class WidgetAbstraction
102+ namespace BridgePattern
141103 {
142- protected:
143- OsImplemetation *_implementation;
144-
145- public:
146- explicit WidgetAbstraction (OsImplemetation *implemetation) : _implementation{implemetation}
104+ /* *
105+ * The Implementation defines the interface for all implementation classes. It
106+ * doesn't have to match the Abstraction's interface. In fact, the two
107+ * interfaces can be entirely different. Typically the Implementation interface
108+ * provides only primitive Widgets, while the Abstraction defines higher-
109+ * level Widgets based on those primitives.
110+ */
111+ class OsImplemetation
147112 {
148- }
149- virtual ~WidgetAbstraction () = default ;
150-
151- virtual std::string clickOn () const = 0;
152- };
113+ public:
114+ virtual std::string clickOnImplement () const = 0;
115+ virtual ~OsImplemetation () = default ;
116+ };
153117
154- /* *
155- * We can extend the Abstraction without changing the Implementation classes.
156- */
157- class ButtonAbstraction : public WidgetAbstraction
158- {
159- public:
160- explicit ButtonAbstraction (OsImplemetation *implemetation) : WidgetAbstraction{implemetation} {}
161- std::string clickOn () const override
118+ class WindowsImplemetation : public OsImplemetation
162119 {
163- return this ->_implementation ->clickOnImplement () + " Click on: Button\n " ;
164- }
165- };
166-
167- class LabelAbstraction : public WidgetAbstraction
168- {
169- public:
170- explicit LabelAbstraction (OsImplemetation *implemetation) : WidgetAbstraction{implemetation} {}
171- std::string clickOn () const override
120+ public:
121+ std::string clickOnImplement () const override
122+ {
123+ return " [Windows]" ;
124+ }
125+ };
126+
127+ class LinuxImplemetation : public OsImplemetation
172128 {
173- return this ->_implementation ->clickOnImplement () + " Click on: Label\n " ;
174- }
175- };
176-
177- namespace Client
178- {
179- void clientCode (const WidgetAbstraction *widget)
129+ public:
130+ std::string clickOnImplement () const override
131+ {
132+ return " [Linux]" ;
133+ }
134+ };
135+
136+ /* *
137+ * The Abstraction defines the interface for the "control" part of the two class
138+ * hierarchies. It maintains a reference to an object of the Implementation
139+ * hierarchy and delegates all of the real work to this object.
140+ */
141+ class WidgetAbstraction
180142 {
181- if (widget != nullptr )
182- std::cout << widget->clickOn ();
143+ protected:
144+ OsImplemetation *_implementation;
145+
146+ public:
147+ explicit WidgetAbstraction (OsImplemetation *implemetation) : _implementation{implemetation}
148+ {
149+ }
150+ virtual ~WidgetAbstraction () = default ;
151+
152+ virtual std::string clickOn () const = 0;
153+ };
154+
155+ /* *
156+ * We can extend the Abstraction without changing the Implementation classes.
157+ */
158+ class ButtonAbstraction : public WidgetAbstraction
159+ {
160+ public:
161+ explicit ButtonAbstraction (OsImplemetation *implemetation) : WidgetAbstraction{implemetation} {}
162+ std::string clickOn () const override
163+ {
164+ return this ->_implementation ->clickOnImplement () + " Click on: Button\n " ;
165+ }
166+ };
167+
168+ class LabelAbstraction : public WidgetAbstraction
169+ {
170+ public:
171+ explicit LabelAbstraction (OsImplemetation *implemetation) : WidgetAbstraction{implemetation} {}
172+ std::string clickOn () const override
173+ {
174+ return this ->_implementation ->clickOnImplement () + " Click on: Label\n " ;
175+ }
176+ };
177+
178+ namespace Client
179+ {
180+ void clientCode (const WidgetAbstraction *widget)
181+ {
182+ if (widget != nullptr )
183+ std::cout << widget->clickOn ();
184+ }
183185 }
184- }
185186
186- void run ()
187- {
188- // TODO: check memory leak here
189- OsImplemetation *os = new WindowsImplemetation ();
190- WidgetAbstraction *widget = new ButtonAbstraction (os);
191- Client::clientCode (widget);
187+ void run ()
188+ {
189+ // TODO: check memory leak here
190+ OsImplemetation *os = new WindowsImplemetation ();
191+ WidgetAbstraction *widget = new ButtonAbstraction (os);
192+ Client::clientCode (widget);
192193
193- os = new LinuxImplemetation ();
194- widget = new LabelAbstraction (os);
195- Client::clientCode (widget);
194+ os = new LinuxImplemetation ();
195+ widget = new LabelAbstraction (os);
196+ Client::clientCode (widget);
196197
197- delete os;
198- delete widget;
198+ delete os;
199+ delete widget;
200+ }
199201 }
200- }
201202
202- struct BridgeAutoruner
203- {
204- BridgeAutoruner ()
203+ struct BridgeAutoruner
205204 {
206- std::cout << " \n --- Bridge Pattern Example ---\n " ;
207- Problem::run ();
208- BridgePattern::run ();
209- }
210- };
205+ BridgeAutoruner ()
206+ {
207+ std::cout << " \n --- Bridge Pattern Example ---\n " ;
208+ Problem::run ();
209+ BridgePattern::run ();
210+ }
211+ };
211212
212- static BridgeAutoruner instance;
213+ static BridgeAutoruner instance;
214+ }
0 commit comments