The problem with option 1 is that you are coupling the "theme" to the widget. Application themes can get very complicated and messy as new features are added to the system, so it might be useful to decouple the "theme" from the widget. For this you'll want to utilize the Factory Pattern:
- Remove all code references to the Theme from all of your Widget classes
Remove all code references to the Theme from all of your Widget classes
- Create a new class called WidgetFactory
Create a new class called WidgetFactory
- Pass a Theme in to the constructor of the WidgetFactory to decouple the construction of the factory from the theme
Pass a Theme in to the constructor of the WidgetFactory to decouple the construction of the factory from the theme
- Expose 1 public method per widget that simply creates a new widget of that type
Expose 1 public method per widget that simply creates a new widget of that type
Internally, these public methods know how to construct a new widget and pass the proper theme settings in to configure the widget.
Whatever "owns" the widget factory should also "own" the theme so memory doesn't leak.
Internally, these public methods know how to construct a new widget and pass the proper theme settings in to configure the widget. 5. Whatever "owns" the widget factory should also "own" the theme so memory doesn't leak.
I'm a little light on C++ experience, and this next bit should be verified by someone more versed than me, but whatever is calling the widget factory to generate a UI element should then take ownership or responsibility for managing that memory, not the widget factory.
The responsibility of the widget factory should be the creation of widgets and marrying them with settings in the theme without the widgets knowing about the theme. This gives you flexibility to refactor the theme to match changing business needs without refactoring an entire class hierarchy of UI widgets in the process — and it allows you to refactor the widget classes without also touching the theme.