samedi 16 juin 2018

c# multiple classes same methods

I have 4 classes

class A {
    private List<InputSelector> _lstA = new List<InputSelector();
    private List<InputSelector> _lstB = new List<InputSelector();
    private List<InputSelector> _lstC = new List<InputSelector();

    public A(){
        _lstA.Add(new InputSelector{ description = "ItemA"});
        _lstB.Add(new InputSelector{ description = "ItemB"});
        _lstC.Add(new InputSelector{ description = "ItemC"});
    }

    public List<InputSelector> getInputSelector(InputSelectorEnum input) {
        switch(type){
             case(InputSelectorEnum.A):
                 return _lstA;
             case(InputSelectorEnum.B):
                 return _lstB;
             case(InputSelectorEnum.C):
                 return _lstC;
        }
    }
}
[...]
class D {
    private List<InputSelector> _lstA = new List<InputSelector();
    private List<InputSelector> _lstB = new List<InputSelector();
    private List<InputSelector> _lstC = new List<InputSelector();

    public D(){
        _lstA.Add(new InputSelector{ description = "ItemA"});
        _lstB.Add(new InputSelector{ description = "ItemB"});
        _lstC.Add(new InputSelector{ description = "ItemC"});
    }

    public List<InputSelector> getInputSelector(InputSelectorEnum input) {
        switch(type){
             case(InputSelectorEnum.A):
                 return _lstA;
             case(InputSelectorEnum.B):
                 return _lstB;
             case(InputSelectorEnum.C):
                 return _lstC;
        }
    }
}

This is obviously not the best DRY pattern but I can't get my head around a better pattern.

Do you have any suggestion on how to simplify this/make it easier to modify/extend and I don't have to repeat it for all 4 classes?

Many thanks!

Aucun commentaire:

Enregistrer un commentaire