Last active
February 9, 2020 09:26
-
-
Save myssun0325/982df5de4850101933ea012cd5f52548 to your computer and use it in GitHub Desktop.
말랑말랑한 코드로 바뀌어가는 과정 기록
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/* | |
0. 문제있는 코드 | |
1. TicketSeller의 자율성 높이기 | |
2. Audience의 자율성 높이기 | |
3. Bag의 자율성 | |
4. TicketOffice의 자율성 | |
*/ | |
class Invitation { | |
private let localDateTime: Date | |
init(localDateTime: Date) { | |
self.localDateTime = localDateTime | |
} | |
} | |
class Ticket { | |
private let fee: Int | |
init(fee: Int) { | |
self.fee = fee | |
} | |
func getFee() -> Int { | |
return fee | |
} | |
} | |
class Bag { | |
private var amount: Int | |
private var invitation: Invitation? | |
private var ticket: Ticket? | |
init(amount: Int, invitation: Invitation?) { | |
self.amount = amount | |
self.invitation = invitation | |
} | |
private func hasInvitation() -> Bool { | |
return invitation != nil | |
} | |
private func setTicket(ticket: Ticket) { | |
self.ticket = ticket | |
} | |
private func minusAmount(amount: Int) { | |
self.amount -= amount | |
} | |
private func plusAmount(amount: Int) { | |
self.amount += amount | |
} | |
func hold(ticket: Ticket) -> Int { | |
if hasInvitation() { | |
setTicket(ticket: ticket) | |
return 0 | |
} else { | |
setTicket(ticket: ticket) | |
minusAmount(amount: ticket.getFee()) | |
return ticket.getFee() | |
} | |
} | |
} | |
class Audience { | |
private let bag: Bag | |
init(bag: Bag) { | |
self.bag = bag | |
} | |
func getBag() -> Bag { | |
return bag | |
} | |
func buy(ticket: Ticket) -> Int { | |
return bag.hold(ticket: ticket) | |
} | |
} | |
class TicketOffice { | |
private var amount: Int | |
private var tickets: [Ticket] = [] | |
init(amount: Int, tickets: Ticket...) { | |
self.amount = amount | |
self.tickets = tickets | |
} | |
private func getTicket() -> Ticket { | |
return tickets.removeFirst() | |
} | |
func miniusAmount(amount: Int) { | |
self.amount -= amount | |
} | |
private func plusAmount(amount: Int) { | |
self.amount += amount | |
} | |
func sellTicketTo(audience: Audience) { | |
plusAmount(amount: audience.buy(ticket: getTicket())) | |
} | |
} | |
class TicketSeller { | |
private let ticketOffice: TicketOffice | |
init(ticketOffice: TicketOffice) { | |
self.ticketOffice = ticketOffice | |
} | |
func getTicketOffice() -> TicketOffice { | |
return ticketOffice | |
} | |
func sellTo(audience: Audience) { | |
ticketOffice.sellTicketTo(audience: audience) | |
} | |
} | |
class Theater { | |
private let ticketSeller: TicketSeller | |
init(ticketSeller: TicketSeller) { | |
self.ticketSeller = ticketSeller | |
} | |
func enter(audience: Audience) { | |
ticketSeller.sellTo(audience: audience) | |
} | |
} | |
/* | |
class Screening { | |
private let movie: Movie | |
private let sequence: Int | |
private let whenScreened: Date | |
init(movie: Movie, sequence: Int, whenScreened: Date) { | |
self.movie = movie | |
self.sequence = sequence | |
self.whenScreened = whenScreened | |
} | |
func getStartTime() -> Date { | |
return whenScreened | |
} | |
func isSequence(sequence: Int) -> Bool { | |
return self.sequence == sequence | |
} | |
func getMovieFee() -> Money { | |
return movie.getFee() | |
} | |
private calculateFee(audienceCount: Int) -> Money { | |
return movie.calculateMovieFee(self).times(audienceCount) | |
} | |
func reserve(customer: Customer, audienceCount: Int) -> Reservation { | |
return Reservation(customer, self, calculateFee(audienceCount), audienceCount) | |
} | |
} | |
class Movie { | |
func getFee() -> Money { | |
return Money() | |
} | |
} | |
class Money { | |
static final zero = Money.wons(amount: 0) | |
private final amount: Int | |
static wons(amount: Int) -> Money { | |
return Money( | |
} | |
} | |
class Reservation { | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment