Skip to content

Latest commit

 

History

History
257 lines (200 loc) · 10.2 KB

File metadata and controls

257 lines (200 loc) · 10.2 KB

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