ochococo /Design-Patterns-In-Swift
📖 Design Patterns implemented in Swift 5.0
★ 15,232⑂ 1,728👁 646⑃ 88● 41▤ 0
-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
#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