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? – Marcel Popescu Jul 29 '13 at 09:12IF, some are just an array lookup. – James Snell Jul 29 '13 at 09:17void DoStuff(IPayment payment)and call it with
DoStuff(ChoosePayment(...))where the
ifis only in one place (theChoosePaymentmethod).Would that work? Is there anything I missed?
– Marcel Popescu Jul 29 '13 at 09:19if, 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. – Neil Jul 29 '13 at 09:34