Python工厂模式

2017-03-18  本文已影响45人  虾想家

工厂模式,一个工厂实例化一个指定类。

class NeedToInstanceClass:
    def __init__(self):
        pass


class Factory(object):
    def __init__(self):
        super().__init__()
        self.cls = NeedToInstanceClass

    def get_instance(self):
        return self.cls()


if __name__ == '__main__':
    factory = Factory()
    print("class name of factory product instance:", factory.get_instance().__class__.__name__)
上一篇 下一篇

猜你喜欢

热点阅读