Flaw: Constructor does Real Work

前端 未结 6 1618
伪装坚强ぢ
伪装坚强ぢ 2020-12-17 04:15

I have a class which represents a set of numbers. The constructor takes three arguments: startValue, endValue and stepSize. The class

6条回答
  •  眼角桃花
    2020-12-17 04:56

    It's true, a constructur should do minimum of work oriented to a single aim - successful creaation of the valid object. Whatever it takes is ok. But not more.

    In your example, creating this collection in the constructor is perfectly valid, as object of your class represent a set of numbers (your words). If an object is set of numbers, you should clearly create it in the constructor! On the contrary - the constructur does not perform what it is made for - a fresh, valid object construction.

    These info strings call my attention. What is their purpose? What exactly do you do? This sounds like something periferic, something that can be left for later and exposed through a method, like

    String getInfo()
    

    or similar.

提交回复
热议问题