Swift设计模式——Chain Of Responsibility

简单介绍一下程序的功能:模拟ATM机取款的过程!

首先,初始化ATM机中有多少张100的,多少张50的,多少张20的,多少张10的

var atm = ATM(hundred: hundred, fifty: fifty, twenty: twenty, ten: ten)

其次,提供取款接口:

func canWithdraw(value: Int) -> String {
        return "Can withdraw: \(self.startPile.canWithdraw(value))"
    }

在MoneyPile 中添加变量 nextPile,此变量告诉你当它干不了这件事情的时候,你该去找谁!(责任链的体现处

以取款310为例,ATM机中有100*1, 50*2, 20*2, 10*6

第一步:有100的吗?--->有!--->取出100--->还有100的吗?--->没有了!---> 那么你去取50的吧!(把责任推给50的)

第二步:有50的吗?--->有!--->取出50--->还有50的吗?--->有!---> 再取50(把责任推给20的)

第三步:。。。。。。。。。。。。(把责任推给10的)

第四步:。。。。。。。。。。。。 (没有推责的下家啦)

最后,根据第四步的结果,告诉取款人,能不能取出钱!

注意:以上只是大致思路,很多细节并没有涉及(比如,你只是取40,那么你是不必问ATM机是否有100的!)

主要是体验这种思想。。。。

//: Playground - noun: a place where people can play

import UIKit

class MoneyPile {
    let value: Int
    var quantity: Int
    var nextPile: MoneyPile?
    
    init(value: Int, quantity: Int, nextPile: MoneyPile?) {
        self.value = value
        self.quantity = quantity
        self.nextPile = nextPile
    }
    
    func canWithdraw(var v: Int) -> Bool { 
        
        func canTakeSomeBill(want: Int) -> Bool {
            return (want / self.value) > 0 
        }
        
        var q = self.quantity
        
        while canTakeSomeBill(v) {
            
            if q == 0 {
                break
            }
            
            v -= self.value
            q -= 1
        }
        
        if v == 0 {
            return true
        } else if let next = self.nextPile {
            return next.canWithdraw(v)
        }
        
        return false
    }
}

class ATM {
    private var hundred: MoneyPile //=====================100,1,fifty
    private var fifty: MoneyPile
    private var twenty: MoneyPile
    private var ten: MoneyPile
    
    private var startPile: MoneyPile {
        return self.hundred
    }
    
    init(hundred: MoneyPile,
        fifty: MoneyPile,
        twenty: MoneyPile,
        ten: MoneyPile) {
            
            self.hundred = hundred
            self.fifty = fifty
            self.twenty = twenty
            self.ten = ten
    }
    
    func canWithdraw(value: Int) -> String {
        return "Can withdraw: \(self.startPile.canWithdraw(value))"
    }
}


// Create piles of money and link them together 10 < 20 < 50 < 100.**
let ten = MoneyPile(value: 10, quantity: 6, nextPile: nil)
let twenty = MoneyPile(value: 20, quantity: 2, nextPile: ten)
let fifty = MoneyPile(value: 50, quantity: 2, nextPile: twenty)
let hundred = MoneyPile(value: 100, quantity: 1, nextPile: fifty)

// Build ATM.
var atm = ATM(hundred: hundred, fifty: fifty, twenty: twenty, ten: ten)
atm.canWithdraw(310) // Cannot because ATM has only 300
atm.canWithdraw(100) // Can withdraw - 1x100
atm.canWithdraw(165) // Cannot withdraw because ATM doesn't has bill with value of 5
atm.canWithdraw(30)  // Can withdraw - 1x20, 2x10


转载:https://github.com/ochococo/Design-Patterns-In-Swift.git
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值