Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*build
*private*
*.vscode
*.vscode
*Identifier
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ set(APP_SOURCES
"src/core/datatypes/class/CConstructors.cpp"
"src/core/datatypes/class/CDestructors.cpp"
"src/patterns/structural/Adapter.cpp"
"src/patterns/structural/Bridge.cpp"
)

# Test files
Expand Down
4 changes: 4 additions & 0 deletions docs/uml/patterns_structural_bridge.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
207 changes: 207 additions & 0 deletions src/patterns/structural/Bridge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
#include <iostream>

namespace Problem
{
class Widget
{
public:
virtual ~Widget() = default;
virtual std::string clickOn() const = 0;
};

/* Concrete variations for Button */
class Button : public Widget
{
public:
virtual ~Button() = default;

public:
std::string clickOn() const override
{
return "Click on: Button\n";
}
};

class ButtonWindows : public Button
{
public:
std::string clickOn() const override
{
return "[Linux]" + Button::clickOn();
}
};

class ButtonLinux : public Button
{
public:
std::string clickOn() const override
{
return "[Windows]" + Button::clickOn();
}
};

/* Concrete variations for Label */
class Label : public Widget
{
public:
virtual ~Label() = default;

public:
std::string clickOn() const override
{
return "Click on: Label\n";
}
};

class LabelWindows : public Label
{
public:
std::string clickOn() const override
{
return "[Windows]" + Label::clickOn();
}
};

class LabelLinux : public Label
{
public:
std::string clickOn() const override
{
return "[Linux]" + Label::clickOn();
}
};

/* Concrete variations for others widgets like Text,CCombo or new platform macOS etc*/
// [Problem 1] We have to write the Text/TextLinux ...

namespace Client
{
void clientCode(const Widget *widget)
{
if (widget != nullptr)
std::cout << widget->clickOn();
}
}

void run()
{
// [Problem 2] : Use the Bridge if you need to be able to switch implementations at runtime. how to exmaple for this
// still don't know
Widget *button = new ButtonWindows();
Client::clientCode(button);
delete button;
}
}

namespace BridgePattern
{
/**
* The Implementation defines the interface for all implementation classes. It
* doesn't have to match the Abstraction's interface. In fact, the two
* interfaces can be entirely different. Typically the Implementation interface
* provides only primitive Widgets, while the Abstraction defines higher-
* level Widgets based on those primitives.
*/
class OsImplemetation
{
public:
virtual std::string clickOnImplement() const = 0;
virtual ~OsImplemetation() = default;
};

class WindowsImplemetation : public OsImplemetation
{
public:
std::string clickOnImplement() const override
{
return "[Windows]";
}
};

class LinuxImplemetation : public OsImplemetation
{
public:
std::string clickOnImplement() const override
{
return "[Linux]";
}
};

/**
* The Abstraction defines the interface for the "control" part of the two class
* hierarchies. It maintains a reference to an object of the Implementation
* hierarchy and delegates all of the real work to this object.
*/
class WidgetAbstraction
{
protected:
OsImplemetation *_implementation;

public:
explicit WidgetAbstraction(OsImplemetation *implemetation) : _implementation{implemetation}
{
}
virtual ~WidgetAbstraction() = default;

virtual std::string clickOn() const = 0;
};

/**
* We can extend the Abstraction without changing the Implementation classes.
*/
class ButtonAbstraction : public WidgetAbstraction
{
public:
explicit ButtonAbstraction(OsImplemetation *implemetation) : WidgetAbstraction{implemetation} {}
std::string clickOn() const override
{
return this->_implementation->clickOnImplement() + "Click on: Button\n";
}
};

class LabelAbstraction : public WidgetAbstraction
{
public:
explicit LabelAbstraction(OsImplemetation *implemetation) : WidgetAbstraction{implemetation} {}
std::string clickOn() const override
{
return this->_implementation->clickOnImplement() + "Click on: Label\n";
}
};

namespace Client
{
void clientCode(const WidgetAbstraction *widget)
{
if (widget != nullptr)
std::cout << widget->clickOn();
}
}

void run()
{
// TODO: check memory leak here
OsImplemetation *os = new WindowsImplemetation();
WidgetAbstraction *widget = new ButtonAbstraction(os);
Client::clientCode(widget);

os = new LinuxImplemetation();
widget = new LabelAbstraction(os);
Client::clientCode(widget);

delete os;
delete widget;
}
}

struct BridgeAutoruner
{
BridgeAutoruner()
{
std::cout << "\n--- Bridge Pattern Example ---\n";
Problem::run();
BridgePattern::run();
}
};

static BridgeAutoruner instance;