mercredi 25 janvier 2017

How to design a nested polymorphism in OOP?

Let's assume we have a simple payment feature on an online shop. We want to manage different transactions with different processors of transactions:

  • A transaction can be a payment or a refund.
  • A processor of transactions can be Paypal or Payplug.

So we have the following classes:

class PaymentTransaction implements Transaction {

}

class RefundTransaction implements Transaction {

}

class PaypalProcessor implements Processor {

}

class PayplugProcessor implements Processor {

}

What should be a good understanding of OOP?

  1. Processor.process(transaction); or
  2. Transaction.process(processor);

For example, if we take the example 1, how to avoid the following switch statement?

class PaypalProcessor {

    function process(Transaction transaction) {
        switch(transaction.getType()) {
            case payment:
            //..
                break;
            case refund:
            //..
  }
}

In all cases, how to manage a "nested" polymorphism, a strategy or something else to be able to manage the different transactions with the different processors?

PS: if the title is not appropriate, tell me I will edit it.

Aucun commentaire:

Enregistrer un commentaire