> For the complete documentation index, see [llms.txt](https://nelmm.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nelmm.gitbook.io/til/common/designpattern/create/abstractfactoy.md).

# 추상 팩토리 패턴

관련있는 여러개의 인스턴스를 만들어주는 인터페이스로 분리하여 생성하는 패턴

## 팩토리메서드 패턴과 비교해서 봐보자!

### 팩토리 메서드

![factory Method](https://325898919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fm27e2hdi88tcctgFwT8o%2Fuploads%2Fgit-blob-5ae523b3593ec73f3b48bef001e36326e8ed0333%2FfactoryMethod.PNG?alt=media) 팩토리메서드는 인자 타입에 따라 여러타입의 객체를 반환

팩토리메서드는 구체적인 클래스에서 객체를 생성

### 추상 팩토리

![abstract Factory](https://325898919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fm27e2hdi88tcctgFwT8o%2Fuploads%2Fgit-blob-f99ca8f9e8da66dd5589cfc3b4e62be521e09585%2FabstractFactory.PNG?alt=media)

추상 팩토리 패턴은 이 추상 팩토리의 구현체에 존재하는 각각의 메서드들은 한가지 종류의 객체만 반환을 한다.

추상팩토리는 인터페이스를 통해 생성

<br>

한마디로, 팩토리메서드는 팩토리를 구현하는 방법으로 추상팩토리 패턴은 팩토리를 사용하는 방법

### 목적

`팩토리 메소드 패턴`은 구체적인 객체 생성과정을 하위/구체적인 클래스로 옮기는 것이 목적이며, `추상 팩토리 패턴`은 관련있는 여러객체를 구체적인 클래스에 의존하지 않고 만들 수 있게 해주는 것이 목적

한 종류의 제품군을 생성할 때 유리하게 사용할 수 있는 패턴이다.

## 장점

1. client와 객체간의 결합을 피할 수 있다.
2. SPR
3. OCP

## 단점

1. 추상 팩토리가 생성해내는 객체들도 인터페이스를 기반으로 구현을 하다보니 많은 인터페이스와 객체로 인해 구조가 복잡해 질 수 있다.

## 구조예시

![struture](https://325898919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fm27e2hdi88tcctgFwT8o%2Fuploads%2Fgit-blob-8059b0ac68a03fa0add9825387745bd2d9a20605%2Fstructure.PNG?alt=media)
