factory pattern은 유명한 디자인 패턴 중 하나이다. 이것 역시 JDK 및 우리가 잘 알고 있는 Spring, Struts 프레임워크에 많이 사용되고 있다.

super class와 여러개의 sub class가 있는 상황에서 input이 발생하면 하나의 sub class를 반환해야 할때 factory pattern이 사용된다. factory class는 client class로 부터 인스턴스를 생성하는 책임을 가져온다.

슈퍼 클래스

factory pattern에서 super class는 interface, abstract class 또는 일반적인 java class가 될 수 있다. 예제에서 super class 로 abstract class를 사용하는데, 테스팅을 위해 toString() method를 오버라이드 하기로 한다.

public abstract class Product {
    public abstract String getName();
    public abstract int getPrice();

    @Override
    public String toString() {
        return "product name : " + getName() + ", price :" + getPrice();
    }
}

서브 클래스

Product class를 상속받은 Computer와 Ticket class를 구현한다. 아래의 클래스들은 super class 하위의 sub class를 정의 한 것이다.

public class Computer extends Product {
    private String name;
    private int price;

    public Computer (String name, int price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String getName() {
        return this.name;
    }
    @Override
    public int getPrice() {
        return this.price;
    }

    public void toStrig () {
        System.out.println("항목 :: " + this.name + ", 가격 :: "+ this.price);
    }
}
public class Ticket extends Product {
    private String name;
    private int price;

    public Ticket (String name, int price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String getName() {
        return this.name;
    }
    @Override
    public int getPrice () {
        return this.price;
    }

    public void toStrig () {
        System.out.println("항목 :: " + this.name + ", 가격 :: "+ this.price);
    }

}

팩토리 클래스

이제 우리는 super class와 sub class를 사용할 준비가 되었다. factory class를 작성해보자.

public class ProductFactory {
    public static Product getProduct(String type, String name, int price) {
        if ( "ticket".equals(type) )
            return new Ticket(name, price);
        else if ( "computer".equals(type) )
            return new Computer(name, price);
        return null;
    }
}
  • factory class를 static으로 선언함으로써 singleton을 유지할 수가 있다.
  • input 파라메터에 의해 sub class 가 생성되어 리턴된다.

아래는 factory pattern을 활용한 테스트 코드이다.

public class main {
    public static void main(String[] args) {

        Product t1 = ProductFactory.getProduct("ticket", "한국여행", 300000);
        Product c1 = ProductFactory.getProduct("computer", "pc", 1500000);

        System.out.println(t1.toString());
        System.out.println(c1.toString());

    }
}
  • Factory pattern은 구현체 보다는 인터페이스 코드 접근에 좀더 용의하게 해준다.
  • Factory pattern은 클라이언트 클래스로부터 인스턴스를 구현하는 코드를 떼어내서, 코드를 더욱 탄탄하게 하고 결합도를 낮춘다.
  • Factory pattern은 구현과 클라이언트 클래스들의 상속을 통해 추상적인 개념을 제공한다.
Tags : java pattern

java proxy pattern (프록시 패턴)

프록시는 실제로 액션을 취하는 객체를 대신해서 대리자 역할을 해준다. 프록시 패턴을 사용하게 되면 프록시 단계에서 권한을 부여할 수 있는 이점이 생기고 필요에 따라 객체를 생성시키거나 사용하기 때문에 메모리를 절약할 수 있는 이점도 생긴다. 프록시 패턴이 하는 일은 한마디로 자신이 보호하고...

더보기

java composite pattern (컴포지트 패턴)

컴포지트 패턴이란 클래스의 구조적 디자인 패턴으로 단일 객체와 복합 객체를 동일하게 컨트롤 할 수 있게끔 도와주는 패턴이다. 컴포지트 패턴은 아래와 같이 3가지의 요소에 의해 이루어진다. base component base component는 composition(구성자)을 위한 인터페이스로 구성된다. 클라이언트 클래스에서는 base component의 인터페이스를 사용하여 작업하게...

더보기

java adapter pattern (어뎁터 패턴)

adapter pattern은 관계가 없는 인터페이스들이 같이 일할 수 있도록 도와주는 디자인 패턴이다. 이 두 인터페이스를 이어주는 인터페이스를 adapter라 부른다. 아래 예제를 보면서 이해해보도록하자. volt class public class Volt { private int volts; public Volt(int v) { this.volts = v; }...

더보기

java builder pattern (빌더 패턴)

bulider pattern은 창조적 디자인 패턴이며 이것은 factory pattern 또는 abstract factory pattern과 매우 비슷하다. 이 패턴에 들어가기 전에 factory pattern과 abstract factory pattern들의 문제점(수 많은 attributes을 사용해야 패턴을 사용할 수 있는 점)에 대해 먼저 알아보자. factory pattern과 abstract factory pattern에는...

더보기