728x90
반응형
Abstract Factory
서로 관련있는 여러 객체를 만들어 주는 인터페이스
구체적으로 어떤 클래스의 인스턴스를 (contrate product)를 사용하는지 감출 수 있다.
클라이언트 코드에서 구체적인 클래스의 의존성을 제거한다.
Ship
public class Ship {
private String name;
private String color;
private String logo;
private Wheel wheel;
private Anchor anchor;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
@Override
public String toString() {
return "Ship{" +
"name='" + name + '\'' +
", color='" + color + '\'' +
", logo='" + logo + '\'' +
'}';
}
public Wheel getWheel() {
return wheel;
}
public void setWheel(Wheel wheel) {
this.wheel = wheel;
}
public Anchor getAnchor() {
return anchor;
}
public void setAnchor(Anchor anchor) {
this.anchor = anchor;
}
}
public class Whiteship extends Ship {
public Whiteship() {
setName("whiteship");
setLogo("\uD83D\uDEE5️");
setColor("white");
}
}
Anchor & Wheel
public interface Anchor {
}
public class WhiteAnchor implements Anchor {
}
public interface Wheel {
}
public class WhiteWheel implements Wheel {
}
ShipFactory
- WhiteShipFactory에서 shipPartsFactory를 받아서, 어떤 factory를 사용할 것인지 구분
public interface ShipFactory {
default Ship orderShip(String name, String email) {
validate(name, email);
prepareFor(name);
Ship ship = createShip();
sendEmailTo(email, ship);
return ship;
}
void sendEmailTo(String email, Ship ship);
Ship createShip();
private void validate(String name, String email) {
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("배 이름을 지어주세요.");
}
if (email == null || email.isBlank()) {
throw new IllegalArgumentException("연락처를 남겨주세요.");
}
}
private void prepareFor(String name) {
System.out.println(name + " 만들 준비 중");
}
}
public abstract class DefaultShipFactory implements ShipFactory {
@Override
public void sendEmailTo(String email, Ship ship) {
System.out.println(ship.getName() + " 다 만들었습니다.");
}
}
// ★★★★★ shipPartsFactory를 받아준다
public class WhiteshipFactory extends DefaultShipFactory {
private ShipPartsFactory shipPartsFactory;
public WhiteshipFactory(ShipPartsFactory shipPartsFactory) {
this.shipPartsFactory = shipPartsFactory;
}
@Override
public Ship createShip() {
Ship ship = new Whiteship();
ship.setAnchor(shipPartsFactory.createAnchor());
ship.setWheel(shipPartsFactory.createWheel());
return ship;
}
}
ShipPartsFactory
- whiteShipPartsFactory는 whiteShip관련 anchor와 wheel을 생성하고,
whiteShipPartsProFacotry는 whiteShipPro관련 anchor와 wheel을 생성한다.
public interface ShipPartsFactory {
Anchor createAnchor();
Wheel createWheel();
}
public class WhiteshipPartsFactory implements ShipPartsFactory {
@Override
public Anchor createAnchor() {
return new WhiteAnchor();
}
@Override
public Wheel createWheel() {
return new WhiteWheel();
}
}
public class WhitePartsProFactory implements ShipPartsFactory {
@Override
public Anchor createAnchor() {
return new WhiteAnchorPro();
}
@Override
public Wheel createWheel() {
return new WhiteWheelPro();
}
}
public class ShipInventory {
public static void main(String[] args) {
ShipFactory shipFactory = new WhiteshipFactory(new WhiteshipPartsFactory());
Ship ship = shipFactory.createShip();
System.out.println("ship.getAnchor().getClass() = " + ship.getAnchor().getClass());
System.out.println("ship.getWheel().getClass() = " + ship.getWheel().getClass());
}
}
> Task :ShipInventory.main()
ship.getAnchor().getClass() = class com.me.whiteship.designpatterns._01_creational_patterns._03_abstract_factory._01_before.WhiteAnchor
ship.getWheel().getClass() = class com.me.whiteship.designpatterns._01_creational_patterns._03_abstract_factory._01_before.WhiteWheel
[ 패턴복습 ]
- 팩토리 메소드와 공통점
: 둘 다 구체적인 객체 생성 과정을 추상화한 인터페이스를 제공한다.
- 팩토리 메소드와 차이점
팩토리 메소드 | 추상 팩토리 | |
관점 | 팩토리를 구현하는 방법에 초점 | 팩토리를 사용하는 방법에 초점 |
목적 | 구체적인 객체 생성 과정을 하위 또는 구체적인 클래스로 옮기는 것 | 관련있는 여러 객체를 구체적인 클래스에 의존하지 않고 만들 수 있게 해주는 것 |
728x90
반응형
'Design Patterns > 생성(Creational)' 카테고리의 다른 글
[Prototype]프로토타입 패턴 (0) | 2022.05.31 |
---|---|
[Builder]빌더패턴 (0) | 2022.05.26 |
[FactoryMethod]장점과 단점 (0) | 2022.05.20 |
[Singleton]4. 자바와 스프링에서 찾아보는 패턴 (0) | 2022.04.08 |
[Singleton]3. 안전하고 단순하게 구현하는 방법 (0) | 2022.04.07 |