본문 바로가기
Programming/디자인 패턴

팩토리 메서드 패턴 (Factory Method), 추상 팩토리 패턴 (Abstract Factory)

by Teshub 2021. 2. 3.

팩토리 메서드 패턴 (Factory Methos)

 

- 정의 -

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

(구글 이미지를 참고하였습니다)

객체 생성을 담당하는 부분은 factoryMethod이 부분입니다

서브클래스에서 팩토리 메서드를 정의하여 팩토리 메서드 호출로 객체를 반환합니다.

 

Allen Holub은 "실용주의 디자인 패턴"에서

Factory Method 패턴은 기반 클래스에 알려지지 않은 구체 클래스를 생성하는 Template Method라 할 수 있다. Factory Method의 반환 타입은 생성되어 반환되는 객체가 구현하고 있는 인터페이스이다. Factory Method는 또한 기반 클래스 코드에 구체 클래스의 이름을 감추는 방법이기도 하다(Factory Method는 부적절한 이름이다. 사람들은 객체를 생성하는 모든 메서드를 자연스레 팩토리 메서드라 부르는 경향이 있는데, 이러한 생성 메서드가 모두 Factory Method 패턴을 사용하는 것은 아니다

 

추상 팩토리 패턴 (Abstract Factory)

 

- 정의 -

인터페이스를 이용하여 서로 연관, 의존 객체를 구현 클래스로 지정하지 않고 생성할 수 있습니다.

객체가 생성되거나 구성, 표현되는 방식과 무관하게 시스템을 독립적으로 만들때 사용됩니다

 

public abstract class Computer {
     
    public abstract String getRAM();
    public abstract String getHDD();
    public abstract String getCPU();
     
    @Override
    public String toString(){
        return "RAM= "+this.getRAM()+", HDD="+this.getHDD()+", CPU="+this.getCPU();
    }
}


public class PC extends Computer {
 
    private String ram;
    private String hdd;
    private String cpu;
     
    public PC(String ram, String hdd, String cpu){
        this.ram=ram;
        this.hdd=hdd;
        this.cpu=cpu;
    }
    @Override
    public String getRAM() {
        return this.ram;
    }
 
    @Override
    public String getHDD() {
        return this.hdd;
    }
 
    @Override
    public String getCPU() {
        return this.cpu;
    }
 
}


public class Server extends Computer {
 
    private String ram;
    private String hdd;
    private String cpu;
     
    public Server(String ram, String hdd, String cpu){
        this.ram=ram;
        this.hdd=hdd;
        this.cpu=cpu;
    }
    @Override
    public String getRAM() {
        return this.ram;
    }
 
    @Override
    public String getHDD() {
        return this.hdd;
    }
 
    @Override
    public String getCPU() {
        return this.cpu;
    }
 
}


public interface ComputerAbstractFactory {
 
	public Computer createComputer();
 
}


public class PCFactory implements ComputerAbstractFactory {
 
	private String ram;
	private String hdd;
	private String cpu;
	
	public PCFactory(String ram, String hdd, String cpu){
		this.ram=ram;
		this.hdd=hdd;
		this.cpu=cpu;
	}
	@Override
	public Computer createComputer() {
		return new PC(ram,hdd,cpu);
	}
 
}


public class ServerFactory implements ComputerAbstractFactory {
 
	private String ram;
	private String hdd;
	private String cpu;
	
	public ServerFactory(String ram, String hdd, String cpu){
		this.ram=ram;
		this.hdd=hdd;
		this.cpu=cpu;
	}
	
	@Override
	public Computer createComputer() {
		return new Server(ram,hdd,cpu);
	}
 
}


public class ComputerFactory {
 
	public static Computer getComputer(ComputerAbstractFactory factory){
		return factory.createComputer();
	}
}


 

테스트코드입니다

public class AbstractFactoryTest {
 
	public static void main(String[] args) {
		Computer pc = ComputerFactory.getComputer(new PCFactory("2 GB","500 GB","2.4 GHz"));
		Computer server = ComputerFactory.getComputer(new ServerFactory("16 GB","1 TB","2.9 GHz"));
		System.out.println("AbstractFactory PC Config::"+pc);
		System.out.println("AbstractFactory Server Config::"+server);
	}
    
}

실행결과입니다

보시면 추상 팩토리(인터페이스)에서 사용할 객체를 정합니다

 

이상입니다

 

 

추상 팩토리 예제는 

readystory.tistory.com/119

글을 참고하였습니다

728x90

'Programming > 디자인 패턴' 카테고리의 다른 글

커맨드 패턴 (Command)  (0) 2021.02.09
싱글턴 패턴 (Singleton)  (0) 2021.02.04
데코레이터 패턴 (Decorator)  (0) 2021.02.02
옵저버 패턴 (Observer)  (0) 2021.01.29
스트래티지 패턴 (Strategy)  (0) 2021.01.28

댓글