jeudi 22 juillet 2021

How will you design classes with many parameters and different clients

For example, if we have a class with 8 parameters:

class TestClass(
    private val paramA: ParamA, 
    private val paramB: ParamB,
    private val paramC: ParamC,
    private val paramD: ParamD,
    private val paramE: ParamE,
    private val paramF: ParamF,
    private val paramG: ParamG,
    private val clientA: ClientA,
    private val clientB: ClientB
)

Most of the time, when this class is used, first 7 parameters will always be passed. The last 2 parameters will depend on the use case. If we need use clientA to call other functions, it will pass clientA, and clientB will be null. If we need use clientB to call other functions, it will pass clientB, and clientA will be null. Inside the class, either clientA or clientB will be used to call other functions, depending on which one is not null.

Can we use different approach here? I don't like using if...else... to check clientA or clientB is null then do other things. Can we replace the two parameters with

private val clientGeneric: ClientGeneric

then clientA and clientB can inherit from ClientGeneric and be passed to TestClass. But I don't see a good way to make clientA and clientB inherit from a ClientGeneric class.

Aucun commentaire:

Enregistrer un commentaire