'''추상 팩토리 패턴'''(Abstract factory pattern)은 다양한 구성 요소 별로 '객체의 집합'을 생성해야 유용하다. 패턴을 사용하여 상황에 알맞은 객체를 생성할 있다. 대표적인 예로, 자바 프로그래밍 언어의 GUI 구성 요소인 AWT/Swing 라이브러리의 앤드 변경 기능을 있다. 메탈, 마이크로소프트 윈도우, OS 등의 모양으로 변경할 있다. 이에 따라 추상 팩토리에 있는 기본 팩토리 객체가 변경된다. 그러면 이후로 생성되는 GUI 객체들은 전부 해당 룩앤필에 따른다. == 구조 == === UML 클래스 시퀀스 다이어그램 === 추상 팩토리 패턴을 위한 샘플 UML 클래스 시퀀스 다이어그램. === 클래스 다이어그램 === left === UML 클래스 다이어그램 === left === Lepus3 차트 범례) === left == 예제 == interface IButton void Paint(); interface IGUIFactory IButton CreateButton(); class WinFactory IGUIFactory public IButton CreateButton() return new WinButton(); class OSXFactory IGUIFactory public IButton CreateButton() return new OSXButton(); class WinButton IButton public void Paint() //Render button in Windows style class OSXButton IButton public void Paint() //Render button in Mac OS style class Program static void Main() var appearance IGUIFactory factory; switch (appearance) case Appearance.Win: factory new WinFactory(); break; case Appearance.OSX: factory new OSXFactory(); break; default: throw new var button button.Paint(); == Golang 예제 == package main import "fmt" type Button interface Paint() type GUIFactory interface CreateButton() Button type WinFactory struct{} func (f *WinFactory) CreateButton() Button return new(WinButton) type OSXFactory struct{} func (f *OSXFactory) CreateButton() Button return new(OSXButton) type LinuxFactory struct{} func (f *LinuxFactory) CreateButton() Button return type WinButton struct{} func (b *WinButton) Paint() type OSXButton struct{} func (b *OSXButton) Paint() type LinuxButton struct{} func (b *LinuxButton) Paint() func string) GUIFactory switch appearance case "Window": return new(WinFactory) case "OSX": return new(OSXFactory) case "Linux": return return nil func main() if factory := factory != nil winButton := if factory := factory != nil osxButton := if factory := factory != nil linuxButton := == Python 예제 == import enum from abc import class Button: @abstractmethod def Paint(self): pass class MousePointer: @abstractmethod def Paint(self): pass class GUIFactory: @abstractmethod def return Button @abstractmethod def return MousePointer class def return WinButton() def return class def return OSXButton() def return class def Paint(self): print("Render mousepointer in Windows style") class def Paint(self): print ("Render mousepointer in OSX style") class def Paint(self): print ("Render button in Windows style") class def Paint(self): print ("Render button in Mac OSX style") class Settings: @staticmethod def Default(): return Appearance.WIN class WIN OSX def main(): apperance if apperance == Appearance.WIN: factory WinFactory() elif apperance == Appearance.OSX: factory OSXFactory() button mousePointer button.Paint() if __name__ == '__main__': main() == PHP 예제 == interface Button public function paint(); interface GUIFactory public function createButton(): Button; class WinFactory implements GUIFactory public function createButton(): Button return new WinButton(); class OSXFactory implements GUIFactory public function createButton(): Button return new OSXButton(); class WinButton implements Button public function paint() echo "Windows Button"; class OSXButton implements Button public function paint() echo "OSX Button"; $appearance "osx"; $factory NULL; switch ($appearance) case "win": $factory new WinFactory(); break; case "osx": $factory new OSXFactory(); break; default: break; if ($factory instanceof GUIFactory) $button == 각주 == 분류:소프트웨어 디자인 패턴