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

后端 未结 16 1268
余生分开走
余生分开走 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 17:08

    Here's a refactor workflow that I frequently encounter that involves static methods. It may lend some insight into your problem.

    I'll start with a class that has reasonably good encapsulation. As I start to add features I run into a piece of functionality that doesn't really need access to the private fields in my class but seems to contain related functionality. After this happens a few times (sometimes just once) I start to see the outlines of a new class in the static methods I've implemented and how that new class relates to the old class in which I first implemented the static methods.

    The benefit that I see of turning these static methods into one or more classes is, when you do this, it frequently becomes easier to understand and maintain your software.

提交回复
热议问题