TIL

09 유연한 설계

01 개방-폐쇄 원칙

컴파일타임 의존성을 고정시키고 런타임 의존성을 변경하라

추상화가 핵심이다

02 생성 사용 분리

public class Client {
    public Movney getAvatarFee() {
        Movie avatar = new Movie("아바타", 
                                Duration.ofMinutes(120),
                                Money.wons(10000),
                                new AmountDiscountPolicy(...));
        return avatar.getFee();
    }
}

FACTORY 추가하기

public class Factory {
    public Movie createAvatarMovie() {
        return new Movie("아바타", 
                        Duration.ofMinutes(120),
                        Money.wons(10000),
                        new AmountDiscountPolicy(...));
    }
}

public class Client {
    private Factory factory;

    public Cleint(Factory factory) {
        this.factory = factory;
    }

    public Movney getAvatarFee() {
        Movie avatar = factory.createAvatarMvoie();
        return avatar.getFee();
    }
}

순수한 가공물에게 책임 할당하기

03 의존성 주입

숨겨진 의존성은 나쁘다.

public class Movie {
// ...
    private DiscountPolicy discountPolicy;

    public Movie(String title, Duration runningTime, Movie fee) {
        this.title = title;
        this.runningTime = runningTime;
        this.fee = fee;
        this.discountPolicy = ServiceLocator.discountPolicy();
    }
}

04 의존성 역전 원칙

추상화와 의존성 역전

의존성 역전 원칙과 패키지

05 유연성에 대한 조언

유연한 설계는 유연성이 필요할 때만 옳다

협력과 책임이 중요하다