생성자 패턴: 팩토리 메서드 패턴

xellos·2022년 3월 28일
0

디자인 패턴

목록 보기
1/20

소개

  • 구체적으로 어쩐 인스턴스를 만들지는 서브 클래스가 정한다.
  • 다양한 구현체가 있고, 그중에서 특정한 구현체를 만들 수 있는 다양한 팩토리를 제공할 수 있다.

팩토리 메서드 적용 전

1) 사용할 클래스

@Getter
@Setter
@ToString
public class Ship {
	private String name;
    private String color;
    private String logo;
}

2) 팩토리(Creator)

만들어야할 Product의 종류가 늘어날수록 Factory 클래스는 커진다.

public class ShipFactory {
	public static Ship orderShip(String name, String email) {
    	argumentValidationCheck(name, email);
        
        System.out.println(name + "만들 준비중");
        
        Ship ship = new Ship();
        ship.setName(name);
        
        writeLogo(name, ship);
        coloringShip(name, ship);
        
        System.out.println(ship.getName() + " 다 만들었습니다.");
        return ship;
    }
    
    private static void argumentValidationCheck(String name, String email) {
    	if(isNotValid(name)) {
        	throw new IllegalArgumentException("배 이름이 존재하지 않습니다.");
        }
        
        if(isNotValid(email)) {
     		throw new IllegalArgumentException("연락받을 이메일 정보가 존재하지 않습니다.");   
        }
    }
    
    private static void writeLogo(String name, Ship ship) {
    	if(isWhiteship(name)) {
        	ship.setLogo("\uD83D\uDEE5");
        } else if(isBlackship(name)) {
            ship.setLogo("😃");
        }
    }
    
    private static void coloringShip(String name, Ship ship) {
    	if(isWhitehhip(name)) {
        	ship.setColor("whiteship");
        } else if(isBlackship(name)) {
        	ship.setLogo("black");
        }
    }
    
    private static boolean isWhiteship(String name) {
    	return name.equalsIgnoreCase("whiteship");
    }
    
    private static boolean isBlackship(String name) {
    	return name.equalsIgnoreCase("blackship");
    }
    
    private static boolean isNotValid(String name) {
    	return name == null || name.isBlack();
    }
}



팩토리 메서드 적용후

1) 인터페이스 생성

public interface ShipFactory {
	Ship createShip();
    
    default Ship orderShip(String name, String email) {
    	validate(name, email);
        prepareFor(name);
        
        Ship ship = new createShip();
        sendEmailTo(Ship);
        return ship;
    }
    
    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 + "만들 준비중");
    }
    
    private void sendEmailTo(Ship ship) {
    	System.out.println(ship.getName()) + "다 만들었습니다.");
    }
}

2) 인터페이스를 구현하는 팩토리 생성

WhiteShip Factory )

public class WhiteShipFactory implements ShipFactory {

	@Override
    public ship createShip() {
    	return new WhiteShip();
    }
}

BlackShip Factory )

public class BlackShipFactory implements ShipFactory {

	@Override
    public Ship createShip() {
    	return new BlackShip();
    }
}

3) 클래스 생성

부모 클래스 생성: Ship )

@Getter
@Setter
@ToString
public class Ship {
	private String name;
    private String color;
    private String logo;
}

자식 클래스: WhiteShip )

public class WhiteShip extends Ship {

	public WhiteShip() {
    	setName("whiteship");
        setLogo("\uD83D\uDEE5");
        setColor("white");
    }
}

자식 클래스: BlackShip )

public class BlackShip extends Ship {

	public BlackShip() {
    	setName("blackship");
        setColor("black");
        setLogo("😃");
    }
}

0개의 댓글