Skip to main content
Commonmark migration
Source Link

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:

  1. 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

  2. Create a new class called WidgetFactory

    Create a new class called WidgetFactory

  3. 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

  4. 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.

  5. 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.

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:

  1. Remove all code references to the Theme from all of your Widget classes
  2. Create a new class called WidgetFactory
  3. Pass a Theme in to the constructor of the WidgetFactory to decouple the construction of the factory from the theme
  4. 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. 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.

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:

  1. Remove all code references to the Theme from all of your Widget classes

  2. Create a new class called WidgetFactory

  3. Pass a Theme in to the constructor of the WidgetFactory to decouple the construction of the factory from the theme

  4. 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.

  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.

Source Link
Greg Burghardt
  • 46.1k
  • 8
  • 87
  • 150

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:

  1. Remove all code references to the Theme from all of your Widget classes
  2. Create a new class called WidgetFactory
  3. Pass a Theme in to the constructor of the WidgetFactory to decouple the construction of the factory from the theme
  4. 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. 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.