GitHub

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
Daily
-3
Weekly
-14
Monthly
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