🍃 Flyweight Pattern
Tags: swift, programming, computerscience, architecture
Published: 2022-12-15 10:36:55


![Flyweight Pattern](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azggezqy0inkyn0728xc.png)

---

The flyweight pattern is used to minimize memory usage or computational expenses by sharing as much as possible with other similar objects.

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

**Example**
```swift
// Instances of SpecialityCoffee will be the Flyweights
struct SpecialityCoffee {
    let origin: String
}

protocol CoffeeSearching {
    func search(origin: String) -> SpecialityCoffee?
}

// Menu acts as a factory and cache for SpecialityCoffee flyweight objects
final class Menu: CoffeeSearching {

    private var coffeeAvailable: [String: SpecialityCoffee] = [:]

    func search(origin: String) -> SpecialityCoffee? {
        if coffeeAvailable.index(forKey: origin) == nil {
            coffeeAvailable[origin] = SpecialityCoffee(origin: origin)
        }

        return coffeeAvailable[origin]
    }
}

final class CoffeeShop {

    private var orders: [Int: SpecialityCoffee] = [:]
    private let menu: CoffeeSearching

    init(menu: CoffeeSearching) {
        self.menu = menu
    }

    func takeOrder(origin: String, table: Int) {
        orders[table] = menu.search(origin: origin)
    }

    func serve() {
        for (table, origin) in orders {
            print("Serving \(origin) to table \(table)")
        }
    }
}
```
**Usage**
```swift
let coffeeShop = CoffeeShop(menu: Menu())

coffeeShop.takeOrder(origin: "Yirgacheffe, Ethiopia", table: 1)
coffeeShop.takeOrder(origin: "Buziraguhindwa, Burundi", table: 3)

coffeeShop.serve()
```
Sources: [Github](https://github.com/sergeyleschev/design-patterns/tree/main/Flyweight)
> **Structural**
In software engineering, structural design patterns are design patterns that ease the design by identifying a simple way to realize relationships between entities.
Source: [wikipedia.org](https://en.wikipedia.org/wiki/Structural_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)