原廠模式:
應用特性:其實原廠模式就是建立對象模式,你想要什麼就給你建立什麼對象。通常接收一個要求,返回一個要求的對象。
結構特性:一個獨立的各類對象的產生器。
UML:
#!/usr/bin/env python#encoding: utf-8class baseclass: def __init__(self): pass def dosomething(self): pass class newclass1(baseclass): def __init__(self): pass def dosomething(self): print 'dosomething1' class newclass2(baseclass): def __init__(self): pass def dosomething(self): print 'dosomething2' class newclass3(baseclass): def __init__(self): pass def dosomething(self): print 'dosomething3' class createclass: def __init__(self): pass @staticmethod def get_new_class(something): if 'newclass1' == something: return newclass1() elif 'newclass2' == something: return newclass2() elif 'newclass3' == something: return newclass3() else: return None if '__main__' == __name__: class1 = createclass.get_new_class('newclass1') class2 = createclass.get_new_class('newclass2') class3 = createclass.get_new_class('newclass3') print class1.__class__ print class2.__class__ print class3.__class__
結果:
__main__.newclass1__main__.newclass2__main__.newclass3