728x90

팩토리 패턴(Factory Pattern)

  • 객체를 사용하는 코드에서 객체 생성 부분을 떼어내 추상화한 패턴
  • 상속 관계에 있는 두 클래스에서 상위 클래스가 중요한 뼈대를 결정하고, 하위 클래스에서 객체 생성에 관한 구체적인 내용을 결정하는 패턴
  • 상위 클래스하위 클래스가 분리되기 때문에 느슨한 결합을 가지며, 상위 클래스에서는 인스턴스 생성 방식에 대해 전혀 알 필요가 없기 때문에 더 많은 유연성을 갖게 된다.
  • 객체 생성 로직이 따로 떼어져 있기 때문에, 코드를 리팩토링하더라도 한 곳만 고칠 수 있게 되니 유지 보수성이 증가된다.
  • 예를 들어, 라떼 레시피아메리카노 레시피, 우유 레시피라는 구체적인 내용이 들어 있는 하위 클래스컨베이어 벨트를 통해 전달되고, 상위 클래스바리스타 공장에서 이 레시피들을 토대로 우유 등을 생산하는 생산 공정을 생각하면 된다.

ⓒ https://refactoring.guru/design-patterns/factory-method

 

Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

 

자바스크립트의 팩토리 패턴

  • 자바스크립트에서 간단하게 @new Object()@로 팩토리 패턴을 구현할 수 있다.
const num = new Object(42);
const str = new Object('abc');

num.constructor.name;    // Number
str.constructor.name;    // String

 

  • 숫자를 전달하거나 문자열을 전달함에 따라 다른 타입의 객체를 생성하는 것을 볼 수 있다.
  • 즉, 전달받은 값에 따라 다른 객체를 생성하며 인스턴스의 타입 등을 정한다.

 

커피 팩토리를 기반으로 라뗴 등을 생산하는 코드의 예
class Latte {
  constructor() {
    this.name = "latte"
  }
}

class Espresso {
  constructor() {
    this.name = "Espresso"
  }
}

class LatteFactory {
  static createCoffee() {
    return new Latte()
  }
}

class EspressoFactory {
  static createCoffee() {
    return new Esplresso()
  }
}

const factoryList = { LatteFactory, EspressoFactory }

class coffeeFactory {
  static createCoffee(type) {
    const factory = factoryList[type]
    return factory.createCoffee()
  }
}

const main = () => {
  // 라떼 커피를 주문한다.
  const coffee = CoffeeFactory.createCoffee("LatteFactory")
  // 커피 이름을 부른다.
  console.log(coffee.name) // latte
}

main()

 

  • @CoffeeFactory@라는 상위 클래스가 중요한 뼈대를 결정하고, 하위 클래스인 @LatteFactory@가 구체적인 내용을 결정하고 있다.
  • @CoffeeFactory@에서 @LatteFactory@의 인스턴스를 생성하는 것이 아닌, @LatteFactory@에서 생성한 인스턴스를 @CoffeeFactory@에서 주입하고 있기 때문에 이것은 의존성 주입이라고도 볼 수 있다.
  • 또한, @CoffeeFactory@를 보면 @static@으로 @createCoffee()@ 정적 메서드를 정의한 것을 알 수 있는데, 정적 메서드를 쓰면 클래스의 인스턴스 없이 호출이 가능하여 메모리를 절약할 수 있고, 개별 인스턴스에 묶이지 않으며 클래스 내의 함수를 정의할 수 있는 장점이 있다.

 

자바의 팩토리 패턴

abstract class Coffee { 
    public abstract int getPrice(); 
    
    @Override
    public String toString(){
        return "Hi this coffee is "+ this.getPrice();
    }
}

class CoffeeFactory { 
    public static Coffee getCoffee(String type, int price){
        if("Latte".equalsIgnoreCase(type)) return new Latte(price);
        else if("Americano".equalsIgnoreCase(type)) return new Americano(price);
        else{
            return new DefaultCoffee();
        } 
    }
}
class DefaultCoffee extends Coffee {
    private int price;

    public DefaultCoffee() {
        this.price = -1;
    }

    @Override
    public int getPrice() {
        return this.price;
    }
}
class Latte extends Coffee { 
    private int price; 
    
    public Latte(int price){
        this.price=price; 
    }
    @Override
    public int getPrice() {
        return this.price;
    } 
}
class Americano extends Coffee { 
    private int price; 
    
    public Americano(int price){
        this.price=price; 
    }
    @Override
    public int getPrice() {
        return this.price;
    } 
} 
public class HelloWorld{ 
     public static void main(String []args){ 
        Coffee latte = CoffeeFactory.getCoffee("Latte", 4000);
        Coffee ame = CoffeeFactory.getCoffee("Americano",3000); 
        System.out.println("Factory latte ::"+latte);
        System.out.println("Factory ame ::"+ame); 
     }
} 
/*
Factory latte ::Hi this coffee is 4000
Factory ame ::Hi this coffee is 3000
*/

 

  • @if ("Latte".eqaulsIgnoreCase(type))@을 통해 문자열 비교 기반으로 로직이 구성됨을 볼 수 있는데, 이것은 @Enum@ 또는 @Map@을 이용하여 if 문을 쓰지 않고 매핑해서 할 수도 있다.

 

Enum은 상수의 집합을 정의할 때 사용되는 타입이다. 상수나 메서드 등을 집어 넣어서 관리하며, 코드를 리팩토링할 때 강점이 생기게 된다. 왜냐하면, 해당 집합에 관한 로직 수정 시 이 부분만 수정하면 되기 때문이다.

 

참고 사이트

 

Factory Method

/ Design Patterns / Creational Patterns Factory Method Also known as: Virtual Constructor Intent Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objec

refactoring.guru

 

팩토리 메서드 패턴 - 위키백과, 우리 모두의 백과사전

위키백과, 우리 모두의 백과사전. -->

ko.wikipedia.org

 

728x90