Skip to content

Instantly share code, notes, and snippets.

@Nirajpaul2
Created December 1, 2024 09:33
Show Gist options
  • Select an option

  • Save Nirajpaul2/0e8c60ee8d53a377d76b6e611aef2008 to your computer and use it in GitHub Desktop.

Select an option

Save Nirajpaul2/0e8c60ee8d53a377d76b6e611aef2008 to your computer and use it in GitHub Desktop.
PaymentService
// Define a protocol that all payment methods must conform to
// It ensures a uniform interface for calculating the processing fee
protocol PaymentMethodProtocol {
func calculateProcessingFee(for amount: Double) -> Double
}
// Concrete implementation of the PaymentMethodProtocol for Credit Card payments
class CreditCardPayment: PaymentMethodProtocol {
func calculateProcessingFee(for amount: Double) -> Double {
return amount * 0.02 // 2% fee for credit card payments
}
}
// Concrete implementation of the PaymentMethodProtocol for Debit Card payments
class DebitCardPayment: PaymentMethodProtocol {
func calculateProcessingFee(for amount: Double) -> Double {
return amount * 0.01 // 1% fee for debit card payments
}
}
// Concrete implementation of the PaymentMethodProtocol for Net Banking payments
class NetBankingPayment: PaymentMethodProtocol {
func calculateProcessingFee(for amount: Double) -> Double {
return amount * 0.005 // 0.5% fee for net banking payments
}
}
// The PaymentGateway class acts as the Context in the Strategy pattern
// It delegates the fee calculation logic to the current payment method
class PaymentGateway {
private var paymentDelegate: PaymentMethodProtocol? // Holds the currently selected payment method
// Method to set the current payment method dynamically
func setPaymentMethod(_ method: PaymentMethodProtocol) {
self.paymentDelegate = method
}
// Processes the payment by calculating the total amount (amount + fee)
func processPayment(amount: Double) -> Double {
guard let paymentDelegate = paymentDelegate else {
fatalError("Payment method is not set") // Ensure a payment method is set before processing
}
return amount + paymentDelegate.calculateProcessingFee(for: amount)
}
}
// The PaymentService class simplifies client interaction
// It manages available payment methods and delegates processing to PaymentGateway
class PaymentService {
private let paymentGateway: PaymentGateway // Instance of PaymentGateway to handle payments
private let creditCardPayment: CreditCardPayment // Instance of CreditCardPayment
private let debitCardPayment: DebitCardPayment // Instance of DebitCardPayment
private let netBankingPayment: NetBankingPayment // Instance of NetBankingPayment
// Initializes the service with the necessary payment methods
init() {
self.paymentGateway = PaymentGateway()
self.creditCardPayment = CreditCardPayment()
self.debitCardPayment = DebitCardPayment()
self.netBankingPayment = NetBankingPayment()
}
// Processes the payment by selecting the appropriate payment method
func processPayment(amount: Double, paymentMethod: PaymentMethodType) {
// Switch statement to select the correct payment method
switch paymentMethod {
case .CreditCard:
paymentGateway.setPaymentMethod(creditCardPayment) // Use CreditCardPayment
case .DebitCard:
paymentGateway.setPaymentMethod(debitCardPayment) // Use DebitCardPayment
case .NetBanking:
paymentGateway.setPaymentMethod(netBankingPayment) // Use NetBankingPayment
}
// Calculate the processed amount and print the result
let processedAmount = paymentGateway.processPayment(amount: amount)
print("Processed amount: \(processedAmount)")
}
}
// Enum to represent the available payment method types
enum PaymentMethodType {
case CreditCard
case DebitCard
case NetBanking
}
// --- Client Code ---
// Creates an instance of PaymentService to handle payments
let paymentService = PaymentService()
// Processes payments using different methods dynamically
paymentService.processPayment(amount: 1000, paymentMethod: .CreditCard) // Uses CreditCardPayment
paymentService.processPayment(amount: 1000, paymentMethod: .DebitCard) // Uses DebitCardPayment
paymentService.processPayment(amount: 1000, paymentMethod: .NetBanking) // Uses NetBankingPayment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment