Making Methods All Static in Class

前端 未结 10 1368
离开以前
离开以前 2021-01-04 12:10

I was told by my colleague based on one of my classes (it is an instance class) that if you have no fields in your class (backing fields), just make all methods static in th

10条回答
  •  情深已故
    2021-01-04 12:39

    Most of the time it's OK to make the class static. But a better question is why do you have a class without state?

    There are very rare instances where a stateless class is good design. But stateless classes break object oriented design. They are usually a throwback to functional decomposition (all the rage before object oriented techniques became popular). Before you make a class static, ask yourself whether the data that it is working on should be included int he class or whether all of the functionality in the utility class shouldn't be broken up between other classes that may or may not already exist.

提交回复
热议问题