[JAVA] - 빌더 패턴

김주형·2022년 12월 1일
0

자라기

목록 보기
21/22

개선 전


	private final List<Integer> player;
    private final List<Integer> computer;
    private int ball;
    private int strike;

    public Hint() {
        this.player = PLAYER.number();
        this.computer = COMPUTER.number();
        this.ball = 0;
        this.strike = 0;
    }
    

개선 후 (테스트는 red)

public abstract class Builder {
    protected final List<Integer> player;
    protected final List<Integer> computer;
    protected int ball;
    protected int strike;

    public Builder() {
        this.player = PLAYER.number();
        this.computer = COMPUTER.number();
        this.ball = 0;
        this.strike = 0;
    }
    
}
  • 빌더 사용
public class Hint extends Builder {

    public Hint() {
        super();
    }

    public List<Integer> buildHint() {

        computer.forEach( computerNumber -> comparing(computerNumber) );
        List<Integer> hint = new ArrayList<>();
        // .. 
        return hint;

    }

    private void comparing(int computerNumber) {

        ListIterator<Integer> playerList = player.listIterator();
        int playerNumber = playerList.next();

        if (computer.contains(playerNumber) && computerNumber != playerNumber) {
            ball++;
        }
        if (computerNumber == playerNumber) {
            strike++;
        }

    }

}

고민

  • 사용자 입력 위치는 테스트 용이성을 고려해야 함
  • 적절한 인스턴스 생성 위치는 어디인가?
  • 객체 참조(메서드 체이닝)의 단위는 어떻게 끊어야 할까?

profile
왔을때보다좋은곳으로

0개의 댓글