'book > DDD start' 카테고리의 다른 글
7장 도메인 서비스 (0) | 2021.06.22 |
---|---|
6장 응용서비스와 표현 영역 (0) | 2021.06.18 |
5장 리포지토리 조회기능 ( JPA 중심 ) (0) | 2021.06.05 |
4장 리포지터리와 모델구현 ( JPA 중심 ) (0) | 2021.06.04 |
D.D.D start 1,2장 (0) | 2021.05.19 |
7장 도메인 서비스 (0) | 2021.06.22 |
---|---|
6장 응용서비스와 표현 영역 (0) | 2021.06.18 |
5장 리포지토리 조회기능 ( JPA 중심 ) (0) | 2021.06.05 |
4장 리포지터리와 모델구현 ( JPA 중심 ) (0) | 2021.06.04 |
D.D.D start 1,2장 (0) | 2021.05.19 |
7장 도메인 서비스 (0) | 2021.06.22 |
---|---|
6장 응용서비스와 표현 영역 (0) | 2021.06.18 |
5장 리포지토리 조회기능 ( JPA 중심 ) (0) | 2021.06.05 |
4장 리포지터리와 모델구현 ( JPA 중심 ) (0) | 2021.06.04 |
DDD start 3장 - 애그리것 (0) | 2021.05.22 |
www.notion.so/4-abaa24321532499abe2ccf1ac0821531
public class ReservationAgency { public Reservation reserve(Screening screening, Customer customer, int audienceCount) { Movie movie = screening.getMovie(); boolean discountable = false; for(DiscountCondition condition : movie.getDiscountConditions()) { if (condition.getType() == DiscountConditionType.PERIOD) { discountable = screening.getWhenScreened().getDayOfWeek().equals(condition.getDayOfWeek()) && condition.getStartTime().compareTo(screening.getWhenScreened().toLocalTime()) <= 0 && condition.getEndTime().compareTo(screening.getWhenScreened().toLocalTime()) >= 0; } else { discountable = condition.getSequence() == screening.getSequence(); } if (discountable) { break; } } Money fee; if (discountable) { Money discountAmount = Money.ZERO; switch(movie.getMovieType()) { case AMOUNT_DISCOUNT: discountAmount = movie.getDiscountAmount(); break; case PERCENT_DISCOUNT: discountAmount = movie.getFee().times(movie.getDiscountPercent()); break; case NONE_DISCOUNT: discountAmount = Money.ZERO; break; } fee = movie.getFee().minus(discountAmount).times(audienceCount); } else { fee = movie.getFee().times(audienceCount); } return new Reservation(customer, screening, fee, audienceCount); } }
public class ReservationAgency { public Reservation reserve(Screening screening, Customer customer, int audienceCount) { boolean discountable = checkDiscountable(screening); Money fee = calculateFee(screening, discountable, audienceCount); return createReservation(screening, customer, audienceCount, fee); } private boolean checkDiscountable(Screening screening) { return screening.getMovie().getDiscountConditions().stream() .anyMatch(condition -> condition.isDiscountable(screening)); } private Money calculateFee(Screening screening, boolean discountable, int audienceCount) { if (discountable) { return screening.getMovie().getFee() .minus(calculateDiscountedFee(screening.getMovie())) .times(audienceCount); } return screening.getMovie().getFee(); } private Money calculateDiscountedFee(Movie movie) { switch(movie.getMovieType()) { case AMOUNT_DISCOUNT: return calculateAmountDiscountedFee(movie); case PERCENT_DISCOUNT: return calculatePercentDiscountedFee(movie); case NONE_DISCOUNT: return calculateNoneDiscountedFee(movie); } throw new IllegalArgumentException(); } private Money calculateAmountDiscountedFee(Movie movie) { return movie.getDiscountAmount(); } private Money calculatePercentDiscountedFee(Movie movie) { return movie.getFee().times(movie.getDiscountPercent()); } private Money calculateNoneDiscountedFee(Movie movie) { return movie.getFee(); } private Reservation createReservation(Screening screening, Customer customer, int audienceCount, Money fee) { return new Reservation(customer, screening, fee, audienceCount); } }
5장 - 3. 구현을 통한 검증 (0) | 2021.05.10 |
---|---|
5장 - 2. 책임 할당을 위한 GRASP 패턴 (0) | 2021.05.09 |
5장 - 1 책임 할당하기 (0) | 2021.05.08 |
4장 설계 품질과 트레이드오프 (0) | 2021.05.05 |
오브젝트 3장 - 역할, 책임, 협력 (0) | 2021.04.30 |
public class Screening { private Movie movie; private int sequence; private LocalDateTime whenScreened; public Reservation reserve(Customer customer, int audienceCount) { return new Reservation(customer, this, calculateFee(audienceCount), audienceCount); } private Money calculateFee(int audienceCount) { return movie.calculateMovieFee(this).times(audienceCount); } public LocalDateTime getWhenScreened() { return whenScreened; } public int getSequence() { return sequence; } }
public class PeriodCondition { private DayOfWeek dayOfWeek; private LocalTime startTime; private LocalTime endTime; public PeriodCondition(DayOfWeek dayOfWeek, LocalTime startTime, LocalTime endTime) { this.dayOfWeek = dayOfWeek; this.startTime = startTime; this.endTime = endTime; } public boolean isSatisfiedBy(Screening screening) { return dayOfWeek.equals(screening.getWhenScreened().getDayOfWeek()) && startTime.compareTo(screening.getWhenScreened().toLocalTime()) <= 0 && endTime.compareTo(screening.getWhenScreened().toLocalTime()) >= 0; } } public class SequenceCondition { private int sequence; public SequenceCondition(int sequence) { this.sequence = sequence; } public boolean isSatisfiedBy(Screening screening) { return sequence == screening.getSequence(); } }
public interface DiscountCondition { boolean isSatisfiedBy(Screening screening); } public class PeriodCondition implements DiscountCondition { private DayOfWeek dayOfWeek; private LocalTime startTime; private LocalTime endTime; public PeriodCondition(DayOfWeek dayOfWeek, LocalTime startTime, LocalTime endTime) { this.dayOfWeek = dayOfWeek; this.startTime = startTime; this.endTime = endTime; } public boolean isSatisfiedBy(Screening screening) { return dayOfWeek.equals(screening.getWhenScreened().getDayOfWeek()) && startTime.compareTo(screening.getWhenScreened().toLocalTime()) <= 0&& endTime.compareTo(screening.getWhenScreened().toLocalTime()) >= 0; } } public class SequenceCondition implements DiscountCondition { private int sequence; public SequenceCondition(int sequence) { this.sequence = sequence; } public boolean isSatisfiedBy(Screening screening) { return sequence == screening.getSequence(); } } public class Movie { private String title; private Duration runningTime; private Money fee; private List<DiscountCondition> discountConditions; private MovieType movieType; private Money discountAmount; private double discountPercent; public Money calculateMovieFee(Screening screening) { if (isDiscountable(screening)) { return fee.minus(calculateDiscountAmount()); } return fee; } private boolean isDiscountable(Screening screening) { return discountConditions.stream() .anyMatch(condition -> condition.isSatisfiedBy(screening)); } }
그림출처 https://songii00.github.io/2020/01/27/2020-01-27-OBJECTS Item 02/
public abstract class Movie { private String title; private Duration runningTime; private Money fee; private List<DiscountCondition> discountConditions; public Movie(String title, Duration runningTime, Money fee, DiscountCondition... discountConditions) { this.title = title; this.runningTime = runningTime; this.fee = fee; this.discountConditions = Arrays.asList(discountConditions); } public Money calculateMovieFee(Screening screening) { if (isDiscountable(screening)) { return fee.minus(calculateDiscountAmount()); } return fee; } private boolean isDiscountable(Screening screening) { return discountConditions.stream() .anyMatch(condition -> condition.isSatisfiedBy(screening)); } protected Money getFee() { return fee; } abstract protected Money calculateDiscountAmount(); } public class AmountDiscountMovie extends Movie { private Money discountAmount; public AmountDiscountMovie(String title, Duration runningTime, Money fee, Money discountAmount, DiscountCondition... discountConditions) { super(title, runningTime, fee, discountConditions); this.discountAmount = discountAmount; } @Override protected Money calculateDiscountAmount() { return discountAmount; } } public class PercentDiscountMovie extends Movie { private double percent; public PercentDiscountMovie(String title, Duration runningTime, Money fee, double percent, DiscountCondition... discountConditions) { super(title, runningTime, fee, discountConditions); this.percent = percent; } @Override protected Money calculateDiscountAmount() { return getFee().times(percent); } }
5장 - 4. 책임주도 설계의 대안 (0) | 2021.05.11 |
---|---|
5장 - 2. 책임 할당을 위한 GRASP 패턴 (0) | 2021.05.09 |
5장 - 1 책임 할당하기 (0) | 2021.05.08 |
4장 설계 품질과 트레이드오프 (0) | 2021.05.05 |
오브젝트 3장 - 역할, 책임, 협력 (0) | 2021.04.30 |
메시지를 전송할 객체는 무엇을 원하는가 ?
메시지를 수신할 적합한 객체는 누구인가 ?
예매에 필요한 것은 영화의 가격이다 Screening은 스스로 처리할수 없고 이 정보는 Movie가 들고 있다.
Movie는 스스로 할인여부를 판단할수 없다. 메시지를 전송해 외부에 요청하자.
5장 - 4. 책임주도 설계의 대안 (0) | 2021.05.11 |
---|---|
5장 - 3. 구현을 통한 검증 (0) | 2021.05.10 |
5장 - 1 책임 할당하기 (0) | 2021.05.08 |
4장 설계 품질과 트레이드오프 (0) | 2021.05.05 |
오브젝트 3장 - 역할, 책임, 협력 (0) | 2021.04.30 |
이 클래스가 필요한 것은 알겠는데 클래스는 무엇을 해야하지 ? 가 아닌
메시지를 전송해야하는지 누구에게 전송해야하지 ? 라고 질문해야 한다.
5장 - 3. 구현을 통한 검증 (0) | 2021.05.10 |
---|---|
5장 - 2. 책임 할당을 위한 GRASP 패턴 (0) | 2021.05.09 |
4장 설계 품질과 트레이드오프 (0) | 2021.05.05 |
오브젝트 3장 - 역할, 책임, 협력 (0) | 2021.04.30 |
오브젝트 2장 (0) | 2021.04.29 |
5장 - 2. 책임 할당을 위한 GRASP 패턴 (0) | 2021.05.09 |
---|---|
5장 - 1 책임 할당하기 (0) | 2021.05.08 |
오브젝트 3장 - 역할, 책임, 협력 (0) | 2021.04.30 |
오브젝트 2장 (0) | 2021.04.29 |
오브젝트 1장 (0) | 2021.04.28 |