Suppose I have a widget class:
struct Widget { public Color Color { get; set; } public int Frobbles { get; set; } }
Now I need to create a factory to create these widgets, so I create a WidgetFactory:
abstract class WidgetFactory { public virtual Widget GetWidget(); }
As it turned out, you can make widgets from several different materials, but the resulting widgets are almost the same. So, I have several implementations of WidgetFactory:
class GoldWidgetFactory : WidgetFactory { public GoldWidgetFactory(GoldMine goldmine) { //... } public Widget GetWidget() { Gold g = goldmine.getGold(); //... } } class XMLWidgetFactory : WidgetFactory { public XMLWidgetFactory(XmlDocument xmlsource) { //... } public Widget GetWidget() { XmlNode node = //whatever //... } } class MagicWidgetFactory : WidgetFactory { public Widget GetWidget() { //creates widget from nothing } }
My question is this: should a WidgetFactory be an abstract class or interface? I see arguments in both directions:
Base class:
- Implementations of ARE WidgetFactories
- They may be able to use functionality (for example, the
List<Widget> WidgetFactory.GetAllWidgets() method)
Interface:
- Implementations do not inherit any data or functionality from the parent
- Their inner workings are completely different.
- Only one method defined
For those who answer, this is not (at present) parallel to any real problem, but if / when I need to implement this template, it would be nice to know. Also, “it doesn't matter” is the correct answer.
Edit: I must indicate why this is happening in the first place. A hypothetical use of this class hierarchy would be something like this:
So, having the GetGoldWidget() method in a WidgetFactory is not a good idea. Plus, maybe adventures in Widget technology allow us to add different and more exotic types of widgets in the future? It’s easier and cleaner to add a new class for processing than the shoehorn method in an existing class.
source share