Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions source-cn/behavioral/chain_of_responsibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,14 @@ final class MoneyPile: Withdrawing {

final class ATM: Withdrawing {

private var hundred: Withdrawing
private var fifty: Withdrawing
private var twenty: Withdrawing
private var ten: Withdrawing
private var moneyPiles: [Withdrawing]

private var startPile: Withdrawing {
return self.hundred
return self.moneyPiles.first!
}

init(hundred: Withdrawing,
fifty: Withdrawing,
twenty: Withdrawing,
ten: Withdrawing) {

self.hundred = hundred
self.fifty = fifty
self.twenty = twenty
self.ten = ten
init(moneyPiles: [Withdrawing]) {
self.moneyPiles = moneyPiles
}

func withdraw(amount: Int) -> Bool {
Expand All @@ -91,6 +81,6 @@ let fifty = MoneyPile(value: 50, quantity: 2, next: twenty)
let hundred = MoneyPile(value: 100, quantity: 1, next: fifty)

// 创建 ATM 实例
var atm = ATM(hundred: hundred, fifty: fifty, twenty: twenty, ten: ten)
var atm = ATM(moneyPiles: [hundred, fifty, twenty, ten])
atm.withdraw(amount: 310) // Cannot because ATM has only 300
atm.withdraw(amount: 100) // Can withdraw - 1x100
20 changes: 5 additions & 15 deletions source/behavioral/chain_of_responsibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,14 @@ final class MoneyPile: Withdrawing {

final class ATM: Withdrawing {

private var hundred: Withdrawing
private var fifty: Withdrawing
private var twenty: Withdrawing
private var ten: Withdrawing
private var moneyPiles: [Withdrawing]

private var startPile: Withdrawing {
return self.hundred
return self.moneyPiles.first!
}

init(hundred: Withdrawing,
fifty: Withdrawing,
twenty: Withdrawing,
ten: Withdrawing) {

self.hundred = hundred
self.fifty = fifty
self.twenty = twenty
self.ten = ten
init(moneyPiles: [Withdrawing]) {
self.moneyPiles = moneyPiles
}

func withdraw(amount: Int) -> Bool {
Expand All @@ -91,6 +81,6 @@ let fifty = MoneyPile(value: 50, quantity: 2, next: twenty)
let hundred = MoneyPile(value: 100, quantity: 1, next: fifty)

// Build ATM.
var atm = ATM(hundred: hundred, fifty: fifty, twenty: twenty, ten: ten)
var atm = ATM(moneyPiles: [hundred, fifty, twenty, ten])
atm.withdraw(amount: 310) // Cannot because ATM has only 300
atm.withdraw(amount: 100) // Can withdraw - 1x100