A comprehensive collection of 23 Gang of Four (GoF) Design Patterns implemented in Swift 5+. This repository serves as a practical learning resource and reference guide for understanding and applying design patterns in Swift development.
Completed: Design Patterns in Swift - Udemy Course
Certificate: UC-67912da2-9f6d-4994-b111-71a3448cfdfe
Design patterns are reusable solutions to common programming problems. They represent best practices evolved from collective programming experience and help create more maintainable, scalable, and robust code.
The Gang of Four (GoF) identified 23 classic patterns organised into three categories:
- Creational Patterns - Object creation mechanisms
- Structural Patterns - Object composition and relationships
- Behavioural Patterns - Communication between objects and responsibility distribution
Patterns focused on object creation while hiding the creation logic.
| Pattern | Purpose | Use Case |
|---|---|---|
| Singleton | Restrict instantiation to a single object | Database connections, Logger |
| Factory | Create objects without specifying exact classes | UI component creation |
| Abstract Factory | Create families of related objects | Theme systems (light/dark mode) |
| Builder | Construct complex objects step by step | Configuration objects |
| Prototype | Create objects by copying existing object | Clone functionality |
Location: ./singleton/, ./factory/, ./builder/, ./prototype/
Patterns that compose objects into larger structures while maintaining flexibility and efficiency.
| Pattern | Purpose | Use Case |
|---|---|---|
| Adapter | Make incompatible interfaces work together | Legacy code integration |
| Bridge | Decouple abstraction from implementation | Platform-specific implementations |
| Composite | Compose objects into tree structures | File system hierarchies |
| Decorator | Add behavior to objects dynamically | Feature extensions |
| Facade | Provide simplified interface to complex subsystems | Wrapper APIs |
| Flyweight | Share common data between objects efficiently | Memory optimization |
| Proxy | Control access to another object | Lazy loading, access control |
Location: ./adapter/, ./bridge/, ./composite/, ./decorator/, ./facade/, ./flyweight/
Patterns that define communication between objects and distribute responsibility.
| Pattern | Purpose | Use Case |
|---|---|---|
| Chain of Responsibility | Pass request along a chain of handlers | Event handling, logging levels |
| Command | Encapsulate request as an object | Undo/Redo, queuing operations |
| Iterator | Access elements sequentially without exposing structure | Collection traversal |
| Mediator | Define object interaction through mediator | Event bus, chat rooms |
| Memento | Capture and restore object state | Save/Load functionality |
| Observer | Notify multiple objects about state changes | Event systems, KVO |
| State | Allow object to alter behavior based on state | State machines |
| Strategy | Encapsulate algorithms as interchangeable objects | Sorting algorithms, payment methods |
| Template Method | Define algorithm skeleton in base class | Common workflows |
| Visitor | Define operations on objects without changing them | Compiler operations, traversals |
| Null Object | Provide object with neutral behavior | Default implementations |
| Interpreter | Define grammar and interpret sentences | Expression evaluation |
Location: ./chain of responsibility/, ./command/, ./iterator/, ./mediator/, ./memento/, ./observer/, ./state/, ./strategy/, ./template/, ./visitor/, ./nullobject/, ./interpreter/
Foundation principles for writing maintainable and scalable code.
| Principle | Description | Benefit |
|---|---|---|
| S - Single Responsibility | One class, one reason to change | Clear focus, easier maintenance |
| O - Open/Closed | Open for extension, closed for modification | Flexible, reduces breaking changes |
| L - Liskov Substitution | Derived classes must be substitutable | Reliable inheritance |
| I - Interface Segregation | Clients shouldn't depend on unused methods | Decoupled, focused interfaces |
| D - Dependency Inversion | Depend on abstractions, not concrete classes | Flexible, testable code |
Location: ./solid/
swift-design-patterns/
βββ Creational/
β βββ singleton/
β βββ factory/
β βββ abstract-factory/
β βββ builder/
β βββ prototype/
β
βββ Structural/
β βββ adapter/
β βββ bridge/
β βββ composite/
β βββ decorator/
β βββ facade/
β βββ flyweight/
β βββ proxy/
β
βββ Behavioural/
β βββ chain-of-responsibility/
β βββ command/
β βββ interpreter/
β βββ iterator/
β βββ mediator/
β βββ memento/
β βββ observer/
β βββ state/
β βββ strategy/
β βββ template-method/
β βββ visitor/
β βββ null-object/
β
βββ SOLID/
β βββ single-responsibility/
β βββ open-closed/
β βββ liskov-substitution/
β βββ interface-segregation/
β βββ dependency-inversion/
β
βββ README.md
βββ LICENSE
- Xcode 13.0 or higher
- Swift 5.0+
- macOS 10.15 or later
-
Clone the repository
git clone https://github.com/codersanjeev/swift-design-patterns.git cd swift-design-patterns -
Open in Xcode (Optional)
open swift-design-patterns.xcodeproj
-
Explore pattern implementations
- Navigate to each pattern directory
- Review the implementation files
- Study the pattern structure and usage
-
Run examples
// Each pattern includes practical examples // Uncomment and run in a Swift playground or app let example = SingletonExample() example.demonstratePattern()
By studying this repository, you'll learn:
β
Pattern Recognition - Identify when to use each pattern
β
Swift Implementation - How patterns translate to Swift code
β
Best Practices - Industry-standard approaches
β
Code Quality - Write maintainable and scalable code
β
SOLID Principles - Foundation for clean architecture
β
Architecture Decisions - When and why to use patterns
β
Refactoring Techniques - Improve existing code structure
Need to restrict object creation? β Singleton, Factory
Working with complex object creation? β Builder
Need to decouple abstraction from implementation? β Bridge, Adapter
Want to add behaviour dynamically? β Decorator
Implementing state-dependent behaviour? β State, Strategy
Need to notify multiple objects? β Observer
Working with hierarchical structures? β Composite
Need undo/redo functionality? β Memento, Command
- Refactoring.Guru - Design Patterns
- Design Patterns: Elements of Reusable Object-Oriented Software (Gang of Four)
- SOLID Principles
Each pattern folder contains:
- Implementation files - Clean, well-documented Swift code
- Example usage - Practical demonstrations
- Comments - Explanations of key concepts
- Advantages & Disadvantages - Trade-offs for each pattern
- Real-world use cases - When to apply in actual projects
- Follows Swift naming conventions and API guidelines
- Includes comprehensive comments and documentation
- Uses modern Swift 5+ features
- Demonstrates best practices
Course Completion: Design Patterns in Swift - Udemy
Certificate: View Certificate
This comprehensive study demonstrates:
- Deep understanding of software architecture
- Ability to identify and implement design patterns
- Knowledge of SOLID principles
- Clean code practices
- Professional development standards
Mastering design patterns enables you to:
- Write Better Code - Use proven solutions instead of reinventing
- Improve Communication - Use common vocabulary with other developers
- Solve Complex Problems - Apply appropriate patterns to challenges
- Build Scalable Systems - Create maintainable and extensible code
- Speed Up Development - Leverage established best practices
- Enhance Collaboration - Easier code reviews and onboarding
Contributions are welcome! If you'd like to:
- Improve existing implementations
- Add more examples
- Fix bugs or improve documentation
- Add alternative implementations
Feel free to fork this repository and submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
- GitHub: @codersanjeev
- LinkedIn: Connect on LinkedIn