Is there anything wrong with a class with all static methods?

后端 未结 16 1277
余生分开走
余生分开走 2021-01-30 16:43

I\'m doing code review and came across a class that uses all static methods. The entrance method takes several arguments and then starts calling the other static methods passin

16条回答
  •  不要未来只要你来
    2021-01-30 16:53

    The biggest problem IMO is that if you want to unit test classes that are calling the class you mention, there is no way to replace that dependency. So you are forced to test both the client class, and the staticly called class at once.

    If we are talking about a class with utility methods like Math.floor() this is not really a problem. But if the class is a real dependency, for instance a data access object, then it ties all its clients in to its implementation.

    EDIT: I don't agree with the people saying there is 'nothing wrong' with this type of 'structured programming'. I would say a class like this is at least a code smell when encountered within a normal Java project, and probably indicates misunderstanding of object-oriented design on the part of the creator.

提交回复
热议问题