How theniks/strategypattern works
Project type and purpose
• This is an iOS demo app (UIKit + Storyboards) that illustrates the Strategy Pattern in Swift/POP (Protocol-Oriented Programming).
• Its purpose: allow the user to pick a payment method at runtime (Credit Card, Apple Pay, PayPal), then process the payment via a common interface without conditional checks.
a. UI Layer
• Main.storyboard & LaunchScreen
• PaymentViewController / PaymentViewControllerOptimize
• SceneDelegate / AppDelegate
b. Domain Layer (Strategy Pattern)
• PaymentMethodProtocol (the “Strategy” interface)
• Concrete strategy implementations (e.g. CreditCardPayment, ApplePayPayment, PayPalPayment) in PaymentMethod.swift
• PaymentContext (or the controller that holds a reference to PaymentMethodProtocol)
c. System Frameworks
• UIKit
d. External Service
• Payment Gateway API (e.g. calls out to a backend or Apple Pay framework)
Relationships and data-flow
• User taps “Pay” in the UI → PaymentViewController invokes PaymentContext.processPayment()
• PaymentContext holds a PaymentMethodProtocol reference → calls concreteStrategy.processPayment(amount)
• ConcreteStrategy calls out to external payment API or Apple Pay framework → returns success/failure → back to PaymentContext → notifies UI layer
• SceneDelegate/AppDelegate handle app lifecycle but do not contain business logic
Architectural patterns and design principles
• Strategy Pattern: define a family of interchangeable algorithms (Payment methods) that conform to the same protocol.
• Protocol-Oriented Programming: no inheritance, only protocols and struct/class implementations.
• Single Responsibility: each PaymentMethod class handles exactly one payment type.
• Dependency Injection: ViewController or Context is injected with a concrete strategy at runtime.