lundi 11 avril 2016

To many constructor arguments

I encounter this problem where I have to pass a lot of arguments to the constructor in order to instantiate the class. I have no clue how to tackle this problem.

user = SpecialStudent(John, Doe, johndoe@example.com, 0612345678, 9988778, hello)

class User(Base):
    def __init__(self, name, surname, email):
        self.name = name
        self.surname = surname
        self.email = email

class Student(User):
    def __init__(self, name, surname, email, phone, student_number):
        super(Student, self).__init__(name, surname, email)
        self.phone = phone
        self.student_number = student_number

class SpecialStudent(Student):
    def __init__(self, name, surname, email, phone, student_number, random_param):
        super(SpecialStudent, self).__init__(name, surname, email, phone, student_number)
        self.random_param = random_param

I have thought about the builder pattern or factory, but i don't know which pattern will tackle this problem or if there is a other way to do this.

Aucun commentaire:

Enregistrer un commentaire