Source

dictionary_switches / code / TableDriven / factory.py

Full commit
class Widget(object):

    def __str__(self):
        return self.__class__.__name__

class FooWidget(Widget): pass
class BarWidget(Widget): pass
class BazWidget(Widget): pass
class DefaultWidget(Widget): pass

class WidgetFactory(object):

    widgets = {
            "foo": FooWidget,
            "bar": BarWidget,
            "baz": BazWidget
            }

    @classmethod
    def getWidget(cls, name):
        try:
            return cls.widgets[name]()
        except KeyError as e:
            return DefaultWidget()


if __name__ == '__main__':
    widgetA = WidgetFactory.getWidget('foo')
    widgetB = WidgetFactory.getWidget('unknown')
    print( widgetA )
    print( widgetB )