A comprehensive collection of classic Design Patterns implemented in Swift. This project demonstrates 22 fundamental patterns organized into three categories: Creational, Structural, and Behavioral patterns.
- About
- Pattern Categories
- Project Structure
- Requirements
- Usage
- Patterns Overview
- Contributing
- License
This repository serves as a practical reference and learning resource for software developers who want to understand and implement design patterns in Swift. Each pattern is implemented with clear, well-documented code that follows best practices and Swift idioms.
Design patterns are reusable solutions to common problems in software design. They represent best practices evolved over time by experienced software developers.
Patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.
Patterns that ease the design by identifying a simple way to realize relationships between entities.
Patterns that identify common communication patterns between objects and realize these patterns.
PatternsCatalog/
├── Creational/
│ ├── AbstractFactory.swift
│ ├── Builder.swift
│ ├── FactoryMethod.swift
│ ├── Prototype.swift
│ └── Singleton.swift
├── Structural/
│ ├── Adapter.swift
│ ├── Bridge.swift
│ ├── Composite.swift
│ ├── Decorator.swift
│ ├── Facade.swift
│ ├── Flyweight.swift
│ └── Proxy.swift
├── Behavioral/
│ ├── ChainOfResponsibility.swift
│ ├── Command.swift
│ ├── Iterator.swift
│ ├── Mediator.swift
│ ├── Memento.swift
│ ├── Observer.swift
│ ├── State.swift
│ ├── Strategy.swift
│ ├── TemplateMethod.swift
│ └── Visitor.swift
├── TestCreationalBundle/
├── TestStructuralBundle/
├── TestBehavioralBundle/
└── main.swift
- Xcode: 12.0 or later
- Swift: 5.0 or later
- macOS: 10.15 or later
-
Clone the repository:
git clone https://github.com/yourusername/PatternsCatalog.git cd PatternsCatalog -
Open the project:
open PatternsCatalog.xcodeproj
-
Build and run:
- Select the target scheme in Xcode
- Press
Cmd + Rto build and run - Or run tests with
Cmd + U
-
Explore individual patterns: Each pattern file contains:
- Detailed comments explaining the pattern
- Complete implementation
- Client code demonstrating usage
| Pattern | Purpose |
|---|---|
| Abstract Factory | Creates families of related objects without specifying their concrete classes |
| Builder | Constructs complex objects step by step |
| Factory Method | Defines an interface for creating objects, but lets subclasses decide which class to instantiate |
| Prototype | Creates new objects by copying existing ones |
| Singleton | Ensures a class has only one instance and provides a global access point |
| Pattern | Purpose |
|---|---|
| Adapter | Allows incompatible interfaces to work together |
| Bridge | Separates abstraction from implementation |
| Composite | Composes objects into tree structures to represent part-whole hierarchies |
| Decorator | Adds new functionality to objects dynamically |
| Facade | Provides a simplified interface to a complex subsystem |
| Flyweight | Reduces memory usage by sharing common data between objects |
| Proxy | Provides a surrogate or placeholder for another object |
| Pattern | Purpose |
|---|---|
| Chain of Responsibility | Passes requests along a chain of handlers |
| Command | Encapsulates a request as an object |
| Iterator | Provides a way to access elements of a collection sequentially |
| Mediator | Reduces coupling between components by making them communicate through a mediator |
| Memento | Captures and restores an object's internal state |
| Observer | Defines a subscription mechanism to notify multiple objects about events |
| State | Allows an object to alter its behavior when its internal state changes |
| Strategy | Defines a family of algorithms and makes them interchangeable |
| Template Method | Defines the skeleton of an algorithm in a base class |
| Visitor | Separates algorithms from the objects on which they operate |
Contributions are welcome! If you'd like to add more patterns, improve existing implementations, or fix bugs:
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingPattern) - Commit your changes (
git commit -m 'Add some AmazingPattern') - Push to the branch (
git push origin feature/AmazingPattern) - Open a Pull Request
This project is available for educational and reference purposes. Feel free to use and modify the code for your learning and projects.
- Design Patterns: Elements of Reusable Object-Oriented Software (Gang of Four)
- Refactoring.Guru - Design Patterns
- Swift.org Documentation