An educational iOS application demonstrating all 23 Gang of Four (GoF) design patterns with practical Swift and SwiftUI implementations.
This project serves as a comprehensive reference and learning tool for software design patterns. Each pattern includes working code examples, visual demonstrations, and detailed explanations to help developers understand when and how to apply these patterns in real-world iOS development.
- Complete Pattern Coverage: All 23 GoF design patterns implemented in Swift
- Interactive Examples: Each pattern includes a working SwiftUI demonstration
- UML Diagrams: PlantUML-generated class diagrams for visual understanding
- Code Samples: Comprehensive Swift implementations with explanations
- Categorized Organization: Patterns grouped by type (Creational, Structural, Behavioral)
- Clean Architecture: MVVM + Repository pattern with custom routing
- Detailed Documentation: Comprehensive markdown guides for each pattern
- Type-Safe Code Generation: SwiftGen integration for JSON data access
Object creation mechanisms that increase flexibility and reuse of existing code.
- Abstract Factory: Create families of related objects without specifying concrete classes
- Builder: Construct complex objects step by step
- Factory Method: Provide an interface for creating objects in a superclass
- Prototype: Clone existing objects without coupling to their classes
- Singleton: Ensure a class has only one instance with global access
How to assemble objects and classes into larger structures while keeping them flexible and efficient.
- Adapter: Make incompatible interfaces work together
- Bridge: Split large classes into separate hierarchies (abstraction and implementation)
- Composite: Compose objects into tree structures for part-whole hierarchies
- Decorator: Attach new behaviors to objects by wrapping them
- Facade: Provide a simplified interface to complex subsystems
- Flyweight: Share common state between multiple objects efficiently
- Proxy: Provide a substitute or placeholder for another object
Algorithms and the assignment of responsibilities between objects.
- Chain of Responsibility: Pass requests along a chain of handlers
- Command: Turn requests into stand-alone objects
- Interpreter: Define a language grammar and interpret statements
- Iterator: Traverse elements without exposing underlying representation
- Mediator: Reduce chaotic dependencies between objects
- Memento: Save and restore object state without violating encapsulation
- Observer: Define a subscription mechanism for event notifications
- State: Alter object behavior when internal state changes
- Strategy: Define a family of interchangeable algorithms
- Template Method: Define algorithm skeleton, let subclasses override steps
- Visitor: Separate algorithms from objects they operate on
- iOS 17.0+
- Xcode 15.0+
- Swift 5.9+
- macOS 14.0+ (for development)
- Clone the repository:
git clone https://github.com/yourusername/DesignPattern.git
cd DesignPattern- Open the project in Xcode:
open DesignPatterns.xcodeproj-
Wait for Swift Package Manager to resolve dependencies
-
Build and run (⌘R)
DesignPatterns/
├── DataLayer/
│ ├── Models/ # Domain models
│ ├── Repositories/ # Data access layer
│ └── Responses/ # JSON response DTOs
├── PresentationLayer/
│ ├── App/ # App entry point
│ ├── General/ # Shared utilities, routing, extensions
│ └── Stories/
│ ├── PatternsList/ # Main patterns list screen
│ ├── PatternDetails/ # Pattern detail view
│ └── Samples/ # Pattern implementations
│ ├── Creational/
│ ├── Structural/
│ └── Behavioral/
├── SupportingFiles/
│ ├── Assets/ # Images and color assets
│ ├── CodeSamples/
│ │ └── Markdown/ # Pattern code samples (23 .md files)
│ ├── Data/ # JSON data files
│ ├── Diagrams/
│ │ ├── PlantUML/ # UML diagram sources (23 .puml files)
│ │ └── Images/ # Generated UML diagrams (23 .png files)
│ ├── Generated/ # SwiftGen outputs
│ └── Localization/ # String catalogs
└── MDFiles/ # Pattern documentation
- SwiftUI: Modern declarative UI framework
- Combine: Reactive programming framework
- NerdzInject: Lightweight dependency injection
- NerdzUtils: Utility extensions and helpers
- SwiftMessages: Toast message presentation
- SFSafeSymbols: Type-safe SF Symbols access
- KeychainAccess: Secure storage wrapper
- SwiftGen: Code generation for type-safe resource access
- SwiftLint: Enforce Swift style and conventions
The app follows a layered architecture with clear separation of concerns:
- ViewModels are protocol-based for testability
- Mock implementations provided for SwiftUI previews
- Display models separate UI concerns from domain logic
Type-safe navigation using generic Router with support for:
- Push navigation
- Sheet presentation
- Full screen covers
Data access abstraction layer separating business logic from data sources.
NerdzInject manages dependencies with compile-time safety.
The project uses SwiftGen for type-safe code generation:
# Generate type-safe accessors
swiftgen config runConfiguration is defined in swiftgen.yml.
Strict linting rules enforce consistent code style:
# Run linting
swiftlint
# Auto-fix violations
swiftlint --fixKey conventions:
- 4-space indentation
- Explicit
selfrequired - Force unwrapping forbidden
- Custom rules for naming conventions
- Each pattern includes detailed markdown documentation
- UML class diagrams generated from PlantUML sources
- Full Swift code samples with explanations
- Practical use cases specific to iOS/mobile development
- Real-world iOS framework examples (UIKit, SwiftUI, Combine)
- Pros and cons for each pattern
Pattern documentation references:
- Refactoring Guru
- Digital Ocean GoF Guide
- Original "Design Patterns: Elements of Reusable Object-Oriented Software" by Gang of Four
- Launch the app
- Browse patterns by category (Creational, Structural, Behavioral)
- Tap any pattern to view:
- Overview: Pattern purpose, applicability, and when to use
- Structure: Participants, collaborations, and structural details
- Diagram: UML class diagram with relationships
- Code: Full Swift implementation with explanations
- Explore the code in
PresentationLayer/Stories/Samples/to see real Swift examples
When adding new patterns or examples:
- Follow the existing pattern structure in
Samples/ - Create PlantUML diagram in
SupportingFiles/Diagrams/PlantUML/ - Generate PNG image and add to
SupportingFiles/Diagrams/Images/ - Create markdown code sample in
SupportingFiles/CodeSamples/Markdown/ - Update
GOFPatterns.jsonwith pattern metadata - Run SwiftGen to regenerate type-safe accessors
- Ensure SwiftLint compliance
- Add corresponding documentation in
MDFiles/
This project is available for educational purposes.
- Gang of Four for the original design patterns book
- Refactoring Guru for excellent pattern explanations
- Swift and iOS developer community for modern Swift implementations
Note: This is an educational project intended to demonstrate design patterns. Real-world applications should apply patterns judiciously based on specific requirements rather than using them everywhere.