Skip to content

codersanjeev/swift-design-patterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Swift Design Patterns 🎨

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

πŸ“š What Are Design Patterns?

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

πŸ—οΈ Creational Patterns

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/

πŸ”— Structural Patterns

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/

πŸ”„ Behavioral Patterns

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/

🎯 SOLID Principles

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/

πŸ“ Repository Structure

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

πŸš€ Getting Started

Prerequisites

  • Xcode 13.0 or higher
  • Swift 5.0+
  • macOS 10.15 or later

Exploring Patterns

  1. Clone the repository

    git clone https://github.com/codersanjeev/swift-design-patterns.git
    cd swift-design-patterns
  2. Open in Xcode (Optional)

    open swift-design-patterns.xcodeproj
  3. Explore pattern implementations

    • Navigate to each pattern directory
    • Review the implementation files
    • Study the pattern structure and usage
  4. Run examples

    // Each pattern includes practical examples
    // Uncomment and run in a Swift playground or app
    let example = SingletonExample()
    example.demonstratePattern()

πŸ’‘ Learning Outcomes

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

πŸ“– Pattern Quick Reference

When to Use Each Pattern

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

πŸ”— Resources

Official Documentation

Design Patterns References

Related Courses & Learning

πŸ“ Pattern Implementation Notes

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

Code Style

  • Follows Swift naming conventions and API guidelines
  • Includes comprehensive comments and documentation
  • Uses modern Swift 5+ features
  • Demonstrates best practices

πŸŽ“ Experience & Certification

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

πŸ’Ό How This Helps Your Development

Mastering design patterns enables you to:

  1. Write Better Code - Use proven solutions instead of reinventing
  2. Improve Communication - Use common vocabulary with other developers
  3. Solve Complex Problems - Apply appropriate patterns to challenges
  4. Build Scalable Systems - Create maintainable and extensible code
  5. Speed Up Development - Leverage established best practices
  6. Enhance Collaboration - Easier code reviews and onboarding

🀝 Contributing

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.

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ“§ Connect

Releases

No releases published

Packages

 
 
 

Contributors

Languages