[자바 디자인패턴 소스코드] Abstract Factory Pattern
- Java/Java
- 2022. 6. 16. 00:09
추상 팩토리 패턴
/**
* 추상 팩토리 패턴은 관련성 있는 여러 종류의 객체를 일관성 있는 방식으로 생성할 때 유용하다.
*/
public class AbstractFactoryPattern {
public static void main(String[] args) {
AbFactory abFactory = AbFactoryFactory.getFactory(VEHICLE.BUS);
Door door = abFactory.createDoor();
V8Engine v8Engine = abFactory.createEngine();
door.open();
v8Engine.turnOn();
AbFactory abFactory2 = AbFactoryFactory.getFactory(VEHICLE.TAXI);
Door door2 = abFactory2.createDoor();
V8Engine v8Engine2 = abFactory2.createEngine();
door2.open();
v8Engine2.turnOn();
}
}
class AbFactoryFactory {
public static AbFactory getFactory(VEHICLE vehicle) {
AbFactory abFactory = null;
switch (vehicle) {
case BUS:
abFactory = BusAbFactory.getInstance();
break;
case TAXI:
abFactory = TaxiAbFactory.getInstance();
break;
default:
}
return abFactory;
}
}
enum VEHICLE {TAXI, BUS}
enum DoorStatus {CLOSED, OPENED}
abstract class AbFactory {
public abstract V8Engine createEngine();
public abstract Door createDoor();
}
class BusAbFactory extends AbFactory {
private static AbFactory abFactory;
private BusAbFactory(){}
public static AbFactory getInstance() {
if(abFactory == null)
abFactory = new BusAbFactory();
return abFactory;
}
public V8Engine createEngine() {
return new BusV8Engine();
}
public Door createDoor() {
return new BusDoor();
}
}
class TaxiAbFactory extends AbFactory {
private static AbFactory abFactory;
private TaxiAbFactory(){}
public static AbFactory getInstance() {
if(abFactory == null)
abFactory = new TaxiAbFactory();
return abFactory;
}
public V8Engine createEngine() {
return new TaxiV8Engine();
}
public Door createDoor() {
return new TaxiDoor();
}
}
abstract class Door {
private DoorStatus doorStatus;
public Door() {
doorStatus = DoorStatus.CLOSED;
}
public DoorStatus getDoorStatus() {
return this.doorStatus;
}
public void close() {
if(doorStatus == DoorStatus.CLOSED) return;
doorClose();
doorStatus = DoorStatus.CLOSED;
}
protected abstract void doorClose();
public void open() {
if(doorStatus == DoorStatus.OPENED) return;
doorOpen();
doorStatus = DoorStatus.OPENED;
}
protected abstract void doorOpen();
}
class BusDoor extends Door {
@Override
protected void doorClose() {
System.out.println("close Bus Door.");
}
@Override
protected void doorOpen() {
System.out.println("open Bus Door.");
}
}
class TaxiDoor extends Door {
@Override
protected void doorClose() {
System.out.println("close Taxi Door.");
}
@Override
protected void doorOpen() {
System.out.println("open Taxi Door.");
}
}
abstract class V8Engine {
abstract protected void turnOn();
}
class BusV8Engine extends V8Engine {
protected void turnOn() {
System.out.println("turn on the BusV8Engine.");
}
}
class TaxiV8Engine extends V8Engine {
protected void turnOn() {
System.out.println("turn on the TaxiV8Engine.");
}
}
'Java > Java' 카테고리의 다른 글
SOLID 원칙 (0) | 2022.06.23 |
---|---|
[자바 디자인패턴 소스코드] Composite Pattern (0) | 2022.06.20 |
[자바 디자인패턴 소스코드] Factory Pattern (0) | 2022.06.15 |
[자바 디자인패턴 소스코드] Template Pattern (0) | 2022.06.08 |
[자바 디자인패턴 소스코드] Decorator Pattern (0) | 2022.06.02 |
이 글을 공유하기