ochococo /Design-Patterns-In-Swift
๐ Design Patterns implemented in Swift 5.0
โ
15,232โ 1,728๐ 646โ 88โ 41โค 0
-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
#132Remove unnecessary return keyword from single-line computed properties and functions1 comments ยท 2025-10-15#110Lose call flow in mediator example1 comments ยท 2021-12-02#116Need to fix the camel case naming in Adapter0 comments ยท 2022-11-24#111Facade direct link in README.md file is broken0 comments ยท 2022-05-07