I need to add a new payment type to an existing code base. That means that I'm going to have a few methods looking like this:
if (old payment type) process old type of payment else process new type of payment Now, if this could have been determined beforehand, I would have this method point to an interface implementing a common Pay method and then that interface would be implemented by one of two classes. Unfortunately, I only know which method the customer chooses at runtime, which means I need a way to determine which branch to use. Is there another way except for just having ifs spread through the code?
ifto determine which one to use... wouldn't I?IF, some are just an array lookup.void DoStuff(IPayment payment)and call it withDoStuff(ChoosePayment(...))where theifis only in one place (theChoosePaymentmethod). Would that work? Is there anything I missed?if, however if done right, you will require exactly oneifwhich determines which implementation of IPayment to use. If you find yourself with many implementations, consider using a Factory pattern.