{{위키데이터 속성 추적}}
'''추상 팩토리 패턴'''(Abstract factory pattern)은 다양한 구성 요소 별로 '[[객체 (컴퓨터 과학)|객체]]의 집합'을 생성해야 할 때 유용하다. 이 패턴을 사용하여 상황에 알맞은 객체를 생성할 수 있다.

대표적인 예로, [[자바 프로그래밍 언어]]의 [[그래픽 사용자 인터페이스|GUI]] 구성 요소인 AWT/Swing 라이브러리의 [[룩 앤드 필]] 변경 기능을 들 수 있다. [[메탈]], [[마이크로소프트 윈도우]], [[맥 OS]] 등의 모양으로 변경할 수 있다. 이에 따라 추상 팩토리에 있는 기본 팩토리 객체가 변경된다. 그러면 이후로 생성되는 GUI 객체들은 전부 해당 룩앤필에 따른다.

== 구조 ==
=== UML 클래스 및 시퀀스 다이어그램 ===
[[파일:w3sDesign Abstract Factory Design Pattern UML.jpg|frame|none|추상 팩토리 패턴을 위한 샘플 UML 클래스 및 시퀀스 다이어그램.<ref>{{웹 인용|title=The Abstract Factory design pattern - Structure and Collaboration|url=http://w3sdesign.com/?gr=c01&ugr=struct|website=w3sDesign.com|accessdate=2017-08-12}}</ref>]]
=== 클래스 다이어그램 ===
[[파일:Abstract factory.svg|left]]
{{-}}
=== UML 클래스 다이어그램 ===
[[파일:Abstract factory UML.svg|left]]
{{-}}
=== Lepus3 차트 ([https://web.archive.org/web/20180314162121/http://www.lepus.org.uk/ref/legend/legend.xml 범례]) ===
[[파일:Abstract Factory in LePUS3 vector.svg|451px|left]]
{{-}}

== C # 예제 ==
<syntaxhighlight lang="csharp">
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 a button in a Windows style
    }
}

class OSXButton : IButton
{
    public void Paint()
    {
        //Render a button in a Mac OS X style
    }
}

class Program
{
    static void Main()
    {
        var appearance = Settings.Appearance;

        IGUIFactory factory;
        switch (appearance)
        {
            case Appearance.Win:
                factory = new WinFactory();
                break;
            case Appearance.OSX:
                factory = new OSXFactory();
                break;
            default:
                throw new System.NotImplementedException();
        }

        var button = factory.CreateButton();
        button.Paint();
    }
}
</syntaxhighlight>

== Golang 예제 ==
<syntaxhighlight lang="go">
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 new(LinuxButton)
}

type WinButton struct{}

func (b *WinButton) Paint() {
	fmt.Println("WinButton")
}

type OSXButton struct{}

func (b *OSXButton) Paint() {
	fmt.Println("OSXButton")
}

type LinuxButton struct{}

func (b *LinuxButton) Paint() {
	fmt.Println("LinuxButton")
}

func BuildFactory(appearance string) GUIFactory {
	switch appearance {
	case "Window":
		return new(WinFactory)
	case "OSX":
		return new(OSXFactory)
	case "Linux":
		return new(LinuxFactory)
	}
	return nil
}

func main() {
	if factory := BuildFactory("Window"); factory != nil {
		winButton := factory.CreateButton()
		winButton.Paint()
	}

	if factory := BuildFactory("OSX"); factory != nil {
		osxButton := factory.CreateButton()
		osxButton.Paint()
	}

	if factory := BuildFactory("Linux"); factory != nil {
		linuxButton := factory.CreateButton()
		linuxButton.Paint()
	}
}

</syntaxhighlight>

== Python 예제 ==
<syntaxhighlight lang="python">
import enum
from abc import *

class Button:
    @abstractmethod
    def Paint(self):
        pass

class MousePointer:
    @abstractmethod
    def Paint(self):
        pass

class GUIFactory:
    @abstractmethod
    def CreateButton(self):
        return Button

    @abstractmethod
    def CreateMousePointer(self):
        return MousePointer

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 WinMousePointer(MousePointer):
    def Paint(self):
        print("Render a mousepointer in a Windows style")

class OSXMousePointer(MousePointer):
    def Paint(self):
        print ("Render a mousepointer in a OSX style")

class WinButton(Button):
    def Paint(self):
        print ("Render a button in a Windows style")

class OSXButton(Button):
    def Paint(self):
        print ("Render a button in a Mac OSX style")

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()

</syntaxhighlight>

== PHP 예제 ==
<syntaxhighlight lang="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 = $factory->createButton();
    $button->paint();
}
</syntaxhighlight>

== 같이 보기 ==
* [[팩토리 메서드 패턴]]
* [[소프트웨어 디자인 패턴]]
== 각주 ==
{{각주}}

{{디자인 패턴(소프트웨어)}}
{{토막글|컴퓨터 과학}}

[[분류:소프트웨어 디자인 패턴]]