팩토리 패턴

팩토리라는 클래스에 객체 생성을 위임(캡슐화)하여 팩토리 클래스가 객체를 생성하도록 하는 방식.

어떤 클래스의 인스턴스를 생성할지 서브클래스에서 결정하도록 한다는게 팩토리 패턴의 핵심.

분류에 딱히 큰 의미는 없는 듯 하나, 팩토리 메소드 패턴과 추상 팩토리 메소드 패턴으로 나누고 있다.

"구상클래스에 의존하지 않게, 추상화 된 것에 의존하도록 개발" 을 따르는 패턴

 

 

1. 팩토리 메소드 패턴

: 객체 생성을 담당하는 팩토리 메소드 작성하여 객체 생성을 캡슐화

: 객체를 생성하기 위한 인터페이스를 정의하는데, 어떤 클래스의 인스턴스를 만들지는 서브클래스에서 결정한다.

 

[Mouse interface]

1
2
public interface Mouse {
}
cs

[LGMouse.java]

Mouse interface 구현

1
2
3
4
5
public class LGMouse implements Mouse {
    public LGMouse() {
        System.out.println("LG 마우스 생성");
    }
}
cs

[SamsungMouse.java]

Mouse interface 구현

1
2
3
4
5
public class SamsungMouse implements Mouse {
    public SamsungMouse() {
        System.out.println("Samsung 마우스 생성");
    }
}
cs

 

[Factory.java]

객체 생성을 맡아서 처리 하는 팩토리클래스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Factory {
    public static Mouse createMouse(String type) {
        Mouse mouse = null;
        switch(type) {
            case "LG":
                mouse = new LGMouse();
                break;
            case "SAMSUNG":
                mouse = new SamsungMouse();
                break;
        }
        return mouse;
    }
}
cs

 

[Client]

1
2
3
4
5
public class Client {
    public static void main(String[] args) {
        Factory.createMouse("LG");
    }
}
cs

[결과]

LG 마우스 생성

 

 

2. 추상 팩토리 패턴

: 연관된 서브 클래스를 그룹화할 수 있고 그룹을 자유롭게 교체할 수 있는 패턴

: 인터페이스를 이용하여 서로 연관된, 또는 의존하는 객체를 구상 클래스를 지정하지 않고 생성할 수 있다

: 팩토리 메소드 패턴이 좀 더 캡슐화 되어있는 형태

 

[Mouse interface]

1
2
public interface Mouse {
}
cs

[LGMouse.java]

Mouse interface 구현

1
2
3
4
5
public class LGMouse implements Mouse {
    public LGMouse() {
        System.out.println("LG 마우스 생성");
    }
}
cs

[SamsungMouse.java]

Mouse interface 구현

1
2
3
4
5
public class SamsungMouse implements Mouse {
    public SamsungMouse() {
        System.out.println("SAMSUNG 마우스 생성");
    }
}
cs

 

[Keyboard interface]

1
2
public interface Keyboard {
}
cs

[LGKeyboard.java]

Keyboard interface 구현

1
2
3
4
5
public class LGKeyboard implements Keyboard {
    public LGKeyboard() {
        System.out.println("LG 키보드 생성");
    }
}
cs

[SamsungKeyboard.java]

Keyboard interface 구현

1
2
3
4
5
public class SamsungKeyboard implements Keyboard {
    public SamsungKeyboard() {
        System.out.println("SAMSUNG 키보드 생성");
    }
}
cs

 

[Computer interface]

1
2
3
4
public interface Computer {
    public Keyboard createKeyboard();
    public Mouse createMouse();
}
cs

[ComputerFactory.java]

Computer interface 구현

1
2
3
4
5
6
public class ComputerFactory {
     public void createComputer(Computer computer){
        computer.createKeyboard();
        computer.createMouse();
    }
}
cs

 

[Client.java]

1
2
3
4
5
6
7
public class Client {
    public static void main(String[] args) {
        ComputerFactory cf = new ComputerFactory();
        cf.createComputer(new SamsungComputer());
        cf.createComputer(new LGComputer());
    }
}
cs

[결과]

SAMSUNG 컴퓨터 생성
SAMSUNG 키보드 생성
SAMSUNG 마우스 생성
LG 컴퓨터 생성
LG 키보드 생성
LG 마우스 생성

 

참고 :

아래의 포스팅을 많이 참고했습니다. 훌륭한 예제를 보시려면 아래를 참고해주세요..

https://victorydntmd.tistory.com/300

 

 

 

반응형

+ Recent posts