Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ATM extensibility by replacing fixed properties with a dynamic list #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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