본문 바로가기
Tech/Java

Abstract Factory Design Pattern

by Augustine™ 2019. 3. 20.
반응형

Abstract Factory Design Pattern


Abstract Factory Design pattern은 생산적인 패턴 중에 하나다. Abstract Factory Design Pattern은 Factory Pattern과 매우 유사하지만, 좀 더 생산적으로 factory를 만들어낼 수 있는 차이점이 있다.

Factory Design Pattern에서는 factory 클래스에서 if-else나 switch 구문과 같은 분기문을 사용해서 서브 클래스를 생성했다. Abstract Factory Design Pattern에서는 위와 같은 분기문 대신에, 서버클래스마다 factory 클래스를 만들고, Abstract Factory 메소드를 통해서 오브젝트를 생성한다.

앞서 Factory Design Pattern의 Computer, PC, Server 클래스를 그대로 사용하고, 여기에  ComputerAbstractFactory 인터페이스를 만들고, PcFactory, ServerFactory 클래스를 만들 것이다.

UML로 표현하면 아래와 같다.



먼저 Computer, PC, Server 클래스를 만들자.


package com.augustine.afactory;

public abstract class Computer {

	public abstract String getRam();
	public abstract String getHdd();
	public abstract String getCpu();
	
	@Override
	public String toString() {
		return "Computer [getRam()=" + getRam() + ", getHdd()=" + getHdd() + ", getCpu()=" + getCpu() + "]";
	}
	
}



package com.augustine.afactory;

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;
	}
}



package com.augustine.afactory;

public class Server extends Computer {

	private String ram;
	private String hdd;
	private String cpu;
	
	public Server(String ram, String hdd, String cpu) {
		super();
		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;
	}
}

 

이제, Abstract Factory 의 인터페이스를 만들자.


package com.augustine.afactory;

public interface ComputerAbstractFactory {

	public Computer createComputer();
}



createComputer() 메소드는 Computer 클래스의 인스턴스를 반환한다. 여기서 구현할 factory 클래스들은 이 인터페이스를 구현하고, Computer 의 서브 클래스를 각각 반환할 것이다.


package com.augustine.afactory;

public class PCFactory implements ComputerAbstractFactory {
	
	private String ram;
	private String hdd;
	private String cpu;
	public PCFactory(String ram, String hdd, String cpu) {
		super();
		this.ram = ram;
		this.hdd = hdd;
		this.cpu = cpu;
	}
	
	@Override
	public Computer createComputer() {
		// TODO Auto-generated method stub
		return new PC(ram, hdd, cpu);
	}	

}



package com.augustine.afactory;

public class ServerFactory implements ComputerAbstractFactory {

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


앞서 만든 ComputerAbstractFactory 인터페이스를 인자로 전달하는 메소드를 가지는 ComputerFactory 클래스를 만들어서, 서브클래스의 오브젝트를 생성할 수 있도록 하자.



package com.augustine.afactory;

public class ComputerFactory {

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


테스트 코드와 테스트 결과


package com.augustine.afactory;

public class TestAbstractFactory {

	public static void main(String[] args) {
		testAbstractFactory();
	}
	
	public static void testAbstractFactory() {
		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("pc spec :" + pc);
		System.out.println("server spec " + server);
	}
}


pc spec :Computer [getRam()=2 gb, getHdd()=500 gb, getCpu()=2.4 ghz]

server spec Computer [getRam()=16 gb, getHdd()=1 tb, getCpu()=2.9 ghz]


반응형

'Tech > Java' 카테고리의 다른 글

Overriding와 Overloading  (0) 2019.08.25
Java command line의 Arguments에 대해 알아보기  (2) 2019.08.13
Factory Design Pattern  (0) 2019.03.18
Singleton Design Pattern  (0) 2019.03.14
Observer Design Pattern  (0) 2019.03.14

댓글