How many constructors should a class have?

前端 未结 16 1955
一向
一向 2021-02-04 12:34

I\'m currently modifying a class that has 9 different constructors. Now overall I believe this class is very poorly designed... so I\'m wondering if it is poor design for a clas

16条回答
  •  暖寄归人
    2021-02-04 12:45

    A class should do one thing and one thing only. If it has so many constructors it seems to be a tell tale sign that it's doing too many things.

    Using multiple constructors to force the correct creation of instances of the object in a variety of circumstances but 9 seems like a lot. I would suspect there is an interface in there and a couple of implementations of the interface that could be dragged out. Each of those would likely have from one to a few constructors each relevant to their specialism.

提交回复
热议问题