mardi 23 novembre 2021

Is this wrong way to use factory pattern

Is this wrong way to use factory pattern ?

class SettlementAPIFactory @Inject constructor(
      val instantPayFeatureFlags: InstantPayFeatureFlags,
      val banklinSettlementAPI: BanklinSettlementAPI,
      val moneyMoverCaller: MoneyMoverCaller
    ) {
    
      fun getSettlementAPI(customerToken: CustomerToken): SettlementAPI {
        if (instantPayFeatureFlags.useBanklin(customerToken)) {
          return banklinSettlementAPI
        }
        return moneyMoverCaller
      }
    }

Basically I am defining a factory which is returns banklinSettlementAPI or moneyMoverCaller for a customer based on some feature flag condition.

They way I am planning to use this factory is to get correct settlementAPI object for each customer and invoke a method on it, something like this

val factory SettlementAPIFactory  = SettlementAPIFactory()
factory.getSettlementAPI(customerToken).callSettlement();  

I am wondering if this is wrong way to define factory since I am actually not creating new objects. What could have been better implementation or pattern to use ?

Aucun commentaire:

Enregistrer un commentaire