samedi 24 octobre 2020

A OOD design question, how to elegantly prevent building duplicate stuff

I have a problem, I enum with several types of certain object

class Type(Enum):
    TYPE_A
    TYPE_B
    TYPE_C
    TYPE_D
     ....
class Something():
    def __init__(self):
        self.TYPE_A_list = []
        self.TYPE_B_list = []
        self.TYPE_C_list = []
        self.TYPE_D_list = []
        ...


    def add_data(self, type: Type, value: int):
        if type == TYPE_A:
            self.TYPE_A_list.append(value)
        elif type == TYPE_B:
            self.TYPE_A_list.append(value)
        elif type == TYPE_C:
            self.TYPE_A_list.append(value)
        elif type == TYPE_D:
            self.TYPE_A_list.append(value)
        ....

I am wondering is there any way to prevent this type of duplicate initialization? Different types can have a parent class, but how to deal with the list I need to create? Any suggestion would be helpful. Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire