31 / 문서의제목
절차적인 예매 로직
@Transactional
public Reservation reserveShowing(int customerId, int showingId, int audienceCount) {
① 데이터베이스로부터 Movie, Showing, Rule 조회
② Showing에 적용할 수 있는 Rule 이 존재하는지 판단
③ if (Rule 이 존재하면) {
Discount를 읽어 요금 할인된 요금 계산
} else {
Movie의 정가를 이용해 요금 계산
}
④ Reservation 생성 후 데이터베이스 저장
}
71.
32 / 문서의제목
@Override
public Reservation reserveShowing(int customerId, int showingId, int audienceCount) {
Showing showing = showingDAO.selectShowing(showingId);
Movie movie = movieDAO.selectMovie(showing.getMovieId());
ListRule rules = ruleDAO.selectRules(movie.getId());
② Showing에 적용할 수 있는 Rule 이 존재하는지 판단
③ if (Rule 이 존재하면) {
Discount를 읽어 요금 할인된 요금 계산
} else {
Movie의 정가를 이용해 요금 계산
}
④ Reservation 생성 후 데이터베이스 저장
}
① 데이터베이스로부터 Movie, Showing, Rule 조회
절차적인 예매 로직
72.
33 / 문서의제목
@Transactional
public Reservation reserveShowing(int customerId, int showingId, int audienceCount) {
Showing showing = showingDAO.selectShowing(showingId);
Movie movie = movieDAO.selectMovie(showing.getMovieId());
ListRule rules = ruleDAO.selectRules(movie.getId());
Rule rule = findRule(showing, rules);
③ if (Rule 이 존재하면) {
Discount를 읽어 요금 할인된 요금 계산
} else {
Movie의 정가를 이용해 요금 계산
}
④ Reservation 생성 후 데이터베이스 저장
}
절차적인 예매 로직
② Showing에 적용할 수 있는 Rule 이 존재하는지 판단
private Rule findRule(Showing showing, ListRule rules) {
for(Rule rule : rules) {
if (rule.isTimeOfDayRule()) {
if (showing.isDayOfWeek(rule.getDayOfWeek())
showing.isDurationBetween(rule.getStartTime(), rule.getEndTime())) {
return rule;
}
} else {
if (rule.getSequence() == showing.getSequence()) {
return rule;
}
}
}
return null;
}
73.
34 / 문서의제목
@Transactional
public Reservation reserveShowing(int customerId, int showingId, int audienceCount) {
Showing showing = showingDAO.selectShowing(showingId);
Movie movie = movieDAO.selectMovie(showing.getMovieId());
ListRule rules = ruleDAO.selectRules(movie.getId());
Rule rule = findRule(showing, rules);
Money fee = movie.getFee();
if (rule != null) {
fee = calculateFee(movie);
}
④ Reservation 생성 후 데이터베이스 저장
}
절차적인 예매 로직
③ if (Rule 이 존재하면) {
Discount를 읽어 요금 할인된 요금 계산
} else {
Movie의 정가를 이용해 요금 계산
}
private Money calculateFee(Movie movie) {
Discount discount = discountDAO.selectDiscount(movie.getId());
Money discountFee = Money.ZERO;
if (discount != null) {
if (discount.isAmountType()) {
discountFee = Money.wons(discount.getFee());
} else if (discount.isPercentType()) {
discountFee = movie.getFee().times(discount.getPercent());
}
}
return movie.getFee().minus(discountFee);
}
74.
35 / 문서의제목
@Transactional
public Reservation reserveShowing(int customerId, int showingId, int audienceCount) {
Showing showing = showingDAO.selectShowing(showingId);
Movie movie = movieDAO.selectMovie(showing.getMovieId());
ListRule rules = ruleDAO.selectRules(movie.getId());
Rule rule = findRule(showing, rules);
Money fee = movie.getFee();
if (rule != null) {
fee = calculateFee(movie);
}
Reservation result = makeReservation(customerId, showingId, audienceCount, fee);
reservationDAO.insert(result);
return result;
}
절차적인 예매 로직
private Reservation makeReservation(int customerId, int showingId,
int audienceCount, Money payment) {
Reservation result = new Reservation();
result.setCustomerId(customerId);
result.setShowingId(showingId);
result.setAudienceCount(audienceCount);
result.setFee(payment);
return result;
}
④ Reservation 생성 후 데이터베이스 저장
75.
36 / 문서의제목
중앙 집중식Centralized 제어 스타일
reserveShowing()
showing = selectShowing()
rules = selectRules()
isTimeOfDayRule()*
discount = selectDiscount()
isAmountType()
getFee()
new
rules
:Rule
:Showing
DAO
showing
:Showing
:Reservation
Script
:Rule
DAO
:Discount
DAO
discount
:Discount
:Reservation
할인율 계산 책임할당
할인율을 적용할 책임을 가진 객체 추가
Movie
영화 정보를 알고 있다
가격을 계산한다
Showing
상영 정보를 알고 있다
예매 정보를 생성한다
Movie
Customer
Discount Strategy
Discount Strategy
할인 정책을 알고 있다
할인된 가격을 계산한다
객체 추가
Movie
영화 정보를알고 있다
가격을 계산한다
Showing
상영 정보를 알고 있다
예매 정보를 생성한다
Movie
Customer
Discount Strategy
할인 정책을 알고 있다
할인된 가격을 계산한다
Discount Strategy
Rule
할인 규칙을 알고 있다
할인 여부를 판단한다
Rule
94.
public class Showing{
public Reservation reserve(Customer customer, int audienceCount) {
return new Reservation(customer, this, audienceCount);
}
}
public class Reservation {
public Reservation(Customer customer, Showing showing, int audienceCount) {
this.customer = customer;
this.showing = showing;
this.fee = showing.calculateFee().times(audienceCount);
this.audienceCount = audienceCount;
}
}
public class Showing {
public Money calculateFee() {
return movie.calculateFee(this);
}
}
public class Movie {
public Money calculateFee(Showing showing) {
return fee.minus(discountStrategy.calculateDiscountFee(showing));
}
}
객체지향 구현
95.
public abstract classDiscountStrategy {
public Money calculateDiscountFee(Showing showing) {
for(Rule each : rules) {
if (each.isStatisfiedBy(showing)) {
return getDiscountFee(showing);
}
}
return Money.ZERO;
}
abstract protected Money getDiscountFee(Showing showing);
public interface Rule {
boolean isStatisfiedBy(Showing showing);
}
public class SequenceRule implements Rule {
public boolean isStatisfiedBy(Showing showing) {
return showing.isSequence(sequence);
}
}
public class TimeOfDayRule implements Rule {
public boolean isStatisfiedBy(Showing showing) {
return showing.isPlayingOn(dayOfWeek)
Interval.closed(startTime, endTime)
.includes(showing.getPlayngInterval());
}
}
객체지향 구현
96.
객체지향 구현
public abstractclass DiscountStrategy {
public Money calculateDiscountFee(Showing showing) {
for(Rule each : rules) {
if (each.isStatisfiedBy(showing)) {
return getDiscountFee(showing);
}
}
return Money.ZERO;
}
abstract protected Money getDiscountFee(Showing showing);
public class AmountDiscountStrategy extends DiscountStrategy {
protected Money getDiscountFee(Showing showing) {
return discountAmount;
}
}
public class NonDiscountStrategy extends DiscountStrategy {
protected Money getDiscountFee(Showing showing) {
return Money.ZERO;
}
}
public class PercentDiscountStrategy extends DiscountStrategy {
protected Money getDiscountFee(Showing showing) {
return showing.getFixedFee().times(percent);
}
}