バッドプラクティスから学ぶ Strategy による柔軟なシステム設計
PaymentProcessor
class PaymentProcessor def process(payment_type, amount) if payment_type == 'credit_card' process_credit_card(amount) elsif payment_type == 'paypal' process_paypal(amount) else raise "Unsupported payment type" end end def process_credit_card(amount) puts "Processing credit card payment of #{amount}" end def process_paypal(amount) puts "Processing PayPal payment of #{amount}" end end processor = PaymentProcessor.new processor.process('credit_card', 100) processor.process('paypal', 200)
class PaymentProcessor def initialize(strategy) @strategy = strategy end def process(amount) @strategy.process(amount) end end class CreditCardPayment def process(amount) puts "Processing credit card payment of #{amount}" end end class PayPalPayment def process(amount) puts "Processing PayPal payment of #{amount}" end end # Strategyの選択と実行 credit_card_processor = PaymentProcessor.new(CreditCardPayment.new) credit_card_processor.process(100) paypal_processor = PaymentProcessor.new(PayPalPayment.new) paypal_processor.process(200)