Command

godo·2022년 8월 18일
0

Swift - Design Patterns

목록 보기
13/24
post-thumbnail

되돌릴 수 있는 property 가 없음

Command

class BankAccount : CustomStringConvertible
{
    private var balance = 0
    private let overdraftLimit = -500
    
    func deposit(_ amount: Int)
    {
        balance += amount
        print("Disposited \(amount), balance = \(balance)")
    }
    
    func withdraw(_ amount: Int) -> Bool
    {
        if (balance - amount >= overdraftLimit)
        {
            balance -= amount
            print("Withdraw \(amount), balance is now \(balance)")
            return true
        }
        return false
    }
    
    var description: String
    {
        return "Balance = \(balance)"
    }
}

protocol command
{
    func call()
}

class BankAccountCommand : command
{
    private var account: BankAccount
    
    enum Action
    {
        case deposit
        case withdraw
    }
    
    private var action : Action
    private var amount : Int
    private var succeeded = false
    
    init (_ account: BankAccount, _ action: Action, _ amount: Int)
    {
        self.account = account
        self.action = action
        self.amount = amount
    }
    
    func call()
    {
        switch action {
        case .deposit:
            account.deposit(amount)
            succeeded = true
        case .withdraw:
            succeeded = account.withdraw(amount)
        }
    }
    
}

func main()
{
    let ba = BankAccount()
    
    let commands = [
        BankAccountCommand(ba, .deposit, 100),
        BankAccountCommand(ba, .withdraw, 25)
    ]
    
    print(ba)
    
    commands.forEach{$0.call()}
    
    print(ba)
}

undo operations

    func undo()
    {
        // 성공했을 때만 가능하게
        if !succeeded { return }
        switch action
        {
            case .deposit:
                account.withdraw(amount)
            case .withdraw:
                account.deposit(amount)
        }
    }

profile
☀️☀️☀️

0개의 댓글