ochococo /Design-Patterns-In-Swift

📖 Design Patterns implemented in Swift 5.0

★ 15,232⑂ 1,728👁 646⑃ 88● 41▤ 0
SwiftGPL-3.0Created 2014-07-13
-1
日增长
-3
周增长
-14
月增长
15,232
stars
1,728
forks
646
watchers
88
prs
41
issues
0
releases

README

Design Patterns implemented in Swift 5.0
========================================

A short cheat-sheet with Xcode 10.2 Playground (Design-Patterns.playground.zip).

 🇨🇳中文版

👷 Project started by: @nsmeme (Oktawian Chojnacki)

👷 中文版由 @binglogo (棒棒彬) 整理翻译。

🚀 How to generate README, Playground and zip from source: CONTRIBUTING.md


swift
print("Welcome!")



 Table of Contents

| Behavioral                              | Creational                | Structural                |
| ------------------------------------------------------ | ---------------------------------------- | ---------------------------------------- |
| 🐝 Chain Of Responsibility | 🌰 Abstract Factory | 🔌 Adapter                   |
| 👫 Command                                 | 👷 Builder                   | 🌉 Bridge                     |
| 🎶 Interpreter                         | 🏭 Factory Method     | 🌿 Composite               |
| 🍫 Iterator                               | 🔂 Monostate               | 🍧 Decorator               |
| 💐 Mediator                               | 🃏 Prototype               | 🎁 Façade                     |
| 💾 Memento                                 | 💍 Singleton               | 🍃 Flyweight               |
| 👓 Observer                               |                                          | ☔ Protection Proxy |
| 🐉 State                                     |                                          | 🍬 Virtual Proxy       |
| 💡 Strategy                               |                                          |                                          |
| 📝 Template Method                 |                                          |                                          |
| 🏃 Visitor                                 |                                          |                                          |

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




🐝 Chain Of Responsibility
--------------------------

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

 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
  

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON