상위문서: GoF 디자인 패턴
다양한 구성 요소 별로 '객체의 집합'을 생성해야 할 때 유용하다. 이 패턴을 사용하여 상황에 알맞은 객체를 생성할 수 있다.
import enum
from abc import *
{...}
class WinFactory(GUIFactory):
def CreateButton(self):
return WinButton()
def CreateMousePointer(self):
return WinMousePointer()
class OSXFactory(GUIFactory):
def CreateButton(self):
return OSXButton()
def CreateMousePointer(self):
return OSXMousePointer()
{...}
class Settings:
@staticmethod
def Default():
return Appearance.WIN
class Appearance(enum.Enum):
WIN = 0
OSX = 1
def main():
apperance = Settings.Default()
if apperance == Appearance.WIN:
factory = WinFactory()
elif apperance == Appearance.OSX:
factory = OSXFactory()
button = factory.CreateButton()
mousePointer = factory.CreateMousePointer()
button.Paint()
mousePointer.Paint()
if __name__ == '__main__':
main()