🐝 Chain Of Responsibility
Tags: swift, programming, computerscience, architecture
Published: 2022-12-13 16:01:59


![Chain Of Responsibility](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jd3232v967acbk0bexch.png)

The chain of responsibility pattern is used to process varied requests, each of which may be dealt with by a different handler.

Github https://github.com/sergeyleschev/design-patterns


**Example:**

```swift
protocol Withdrawing {
    func withdraw(amount: Int) -> Bool
}

final class MoneyPile: Withdrawing {
    let value: Int
    var quantity: Int
    var next: Withdrawing?

    init(value: Int, quantity: Int, next: Withdrawing?) {
        self.value = value
        self.quantity = quantity
        self.next = next
    }

    func withdraw(amount: Int) -> Bool {
        var amount = amount

        func canTakeSomeBill(want: Int) -> Bool {
            return (want / self.value) > 0
        }

        var quantity = self.quantity

        while canTakeSomeBill(want: amount) {

            if quantity == 0 {
                break
            }

            amount -= self.value
            quantity -= 1
        }

        guard amount > 0 else {
            return true
        }

        if let next = self.next {
            return next.withdraw(amount: amount)
        }

        return false
    }
}

final class ATM: Withdrawing {

    private var hundred: Withdrawing
    private var fifty: Withdrawing
    private var twenty: Withdrawing
    private var ten: Withdrawing

    private var startPile: Withdrawing {
        return self.hundred
    }

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

        self.hundred = hundred
        self.fifty = fifty
        self.twenty = twenty
        self.ten = ten
    }

    func withdraw(amount: Int) -> Bool {
        return startPile.withdraw(amount: amount)
    }
}
```

**Usage**

```swift
// Create piles of money and link them together 10 < 20 < 50 < 100.**
let ten = MoneyPile(value: 10, quantity: 6, next: nil)
let twenty = MoneyPile(value: 20, quantity: 2, next: ten)
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)
atm.withdraw(amount: 310) // Cannot because ATM has only 300
atm.withdraw(amount: 100) // Can withdraw - 1x100
```

Sources: [Github](https://github.com/sergeyleschev/design-patterns/tree/main/Chain%20Of%20Responsibility)

> **_Behavioral_**
In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.
Source: [wikipedia.org](https://en.wikipedia.org/wiki/Behavioral_pattern)

---

🐝 [Chain Of Responsibility](https://dev.to/sergeyleschev/swift-design-patterns-chain-of-responsibility-4bbd)
👫 [Command Pattern](https://dev.to/sergeyleschev/swift-design-patterns-command-pattern-3044)
🎶 [Interpreter Pattern](https://dev.to/sergeyleschev/swift-design-patterns-interpreter-pattern-390a)
🍫 [Iterator Pattern](https://dev.to/sergeyleschev/swift-design-patterns-iterator-pattern-405o)
💐 [Mediator Pattern](https://dev.to/sergeyleschev/swift-design-patterns-mediator-pattern-4299)
💾 [Memento Pattern](https://dev.to/sergeyleschev/swift-design-patterns-memento-pattern-chp)
👓 [Observer Pattern](https://dev.to/sergeyleschev/swift-design-patterns-observer-pattern-44fc)
🐉 [State Pattern](https://dev.to/sergeyleschev/swift-design-patterns-state-pattern-5ej4)
💡 [Strategy Pattern](https://dev.to/sergeyleschev/swift-design-patterns-strategy-pattern-143c)
📝 [Template Method](https://dev.to/sergeyleschev/swift-design-patterns-template-method-1c2o)
🏃 [Visitor Pattern](https://dev.to/sergeyleschev/swift-design-patterns-visitor-pattern-4hl5)
🌰 [Abstract Factory](https://dev.to/sergeyleschev/swift-design-patterns-abstract-factory-3ppa)
👷 [Builder Pattern](https://dev.to/sergeyleschev/swift-design-patterns-builder-pattern-3609)
🏭 [Factory Method](https://dev.to/sergeyleschev/swift-design-patterns-factory-method-5c6e)
🔂 [Monostate Pattern](https://dev.to/sergeyleschev/swift-design-patterns-monostate-pattern-574i)
🃏 [Prototype Pattern](https://dev.to/sergeyleschev/swift-design-patterns-prototype-pattern-n25)
💍 [Singleton](https://dev.to/sergeyleschev/swift-design-patterns-singleton-3c8b)
🔌 [Adapter Pattern](https://dev.to/sergeyleschev/swift-design-patterns-adapter-pattern-1040)
🌉 [Bridge Pattern](https://dev.to/sergeyleschev/swift-design-patterns-bridge-pattern-1ee2)
🌿 [Composite Pattern](https://dev.to/sergeyleschev/swift-design-patterns-composite-pattern-1koj)
🍧 [Decorator Pattern](https://dev.to/sergeyleschev/swift-design-patterns-decorator-pattern-2c6p)
🎁 [Facade Pattern](https://dev.to/sergeyleschev/swift-design-patterns-facade-pattern-130a)
🍃 [Flyweight Pattern](https://dev.to/sergeyleschev/swift-design-patterns-flyweight-pattern-6ko)
☔ [Protection Proxy](https://dev.to/sergeyleschev/swift-design-patterns-protection-proxy-3kpc)
🍬 [Virtual Proxy](https://dev.to/sergeyleschev/swift-design-patterns-virtual-proxy-2bbg)

---

**Contacts**
I have a clear focus on time-to-market and don't prioritize technical debt. And I took part in the Pre-Sale/RFX activity as a System Architect, assessment efforts for Mobile (iOS-Swift, Android-Kotlin), Frontend (React-TypeScript) and Backend (NodeJS-.NET-PHP-Kafka-SQL-NoSQL). And I also formed the work of Pre-Sale as a CTO from Opportunity to Proposal via knowledge transfer to Successful Delivery.

🛩️ #startups #management #cto #swift #typescript #database
📧 Email: sergey.leschev@gmail.com
👋 LinkedIn: https://linkedin.com/in/sergeyleschev/
👋 LeetCode: https://leetcode.com/sergeyleschev/
👋 Twitter: https://twitter.com/sergeyleschev
👋 Github: https://github.com/sergeyleschev
🌎 Website: https://sergeyleschev.github.io
🌎 Reddit: https://reddit.com/user/sergeyleschev
🌎 Quora: https://quora.com/sergey-leschev
🌎 Medium: https://medium.com/@sergeyleschev
🖨️ PDF Design Patterns: [Download](https://sergeyleschev.github.io/sergeyleschev-design-patterns.pdf)