Created
January 13, 2016 16:03
-
-
Save lsem/4e4b39e327d68d8f5faa to your computer and use it in GitHub Desktop.
Cross Platform Widgets Toolkit -- Better Approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ToolkitCode { | |
class IButton { | |
virtual void setTitle(const string &title) = 0; | |
virtual void doOnClick(std::function<void()> handler) = 0; | |
}; | |
class Win32APIButton : public IButton { | |
virtual void setTitle(const string &title) = 0; | |
virtual void doOnClick(std::function<void()> handler) = 0; | |
}; | |
class WinFormsButton : public IButton { | |
virtual void setTitle(const string &title) = 0; | |
virtual void doOnClick(std::function<void()> handler) = 0; | |
}; | |
class GTKButton : public IButton { | |
virtual void setTitle(const string &title) override; | |
virtual void doOnClick(std::function<void()> handler) override; | |
}; | |
class QtButton : public IButton { | |
virtual void setTitle(const string &title) = 0; | |
virtual void doOnClick(std::function<void()> handler) override; | |
}; | |
} | |
interface IWidgetsFactory { | |
public: | |
IButton *createButton(); | |
}; | |
class Win32APIWidgetsFactory : public IWidgetsFactory { | |
public: | |
IButton *createButton() { return Win32APIButton(); } | |
IComboBox *createComboBox() { return Win32ComboBox(); } | |
}; | |
class GTKWidgetsFactory : public IWidgetsFactory { | |
public: | |
IButton *createButton() { return GTKAPIButton(); } | |
IComboBox *createComboBox() { return GTKComboBox(); } | |
}; | |
class QtWidgetsFactory : public IWidgetsFactory { | |
public: | |
IButton *createButton() { return QtAPIButton(); } | |
IComboBox *createComboBox() { return QtComboBox(); } | |
}; | |
namespace ClientCode { | |
enum class UIFlavor { | |
GTK, | |
WinForms, | |
Win32, | |
QT | |
}; | |
class ProgramOptions { | |
uiFlavor: UIFlavor; | |
}; | |
void createUI() { | |
IButton *createDocumentButton, | |
*saveDocumentButton, | |
*loadDocumentButton; | |
IWidgetsFactory *widgetsFactory; | |
ProgramOptions options = getActualOptions(); | |
if (options.uiFlavor == GTK) { | |
widgetsFactory = new GTKWidgetsFactory(); | |
} else if (options.uiFlavor == WinForms) { | |
widgetsFactory = WinFormsWidgetsFactory(); | |
} else if (options.uiFlavor == Win32) { | |
widgetsFactory = Win32WidgetsFactory(); | |
} else if (options.uiFlavor == Qt) { | |
widgetsFactory = QtWidgetsFactory(); | |
} | |
createDocumentButton = widgetsFactory->createButton(); | |
saveDocumentButton = widgetsFactory->createButton(); | |
loadDocumentButton = widgetsFactory->createButton(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment