Should all methods be static if their class has no member variables

前端 未结 20 2265
悲哀的现实
悲哀的现实 2020-12-02 14:18

I\'ve just had an argument with someone I work with and it\'s really bugging me. If you have a class which just has methods like calculateRisk or/and calc

20条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 15:22

    There is a general feeling against static classes these days, for testability reasons.

    This is because static classes cannot implement an interface and so cannot be easily substituted for test classes if needed. I think that cases like this that deal with price are an excellent example of this. It is possible in future that there may be multiple ways of calculating a price, and it is nice to be able to substitute these calculators for one another as the need arises.

    It may not be useful now but I see more advantages in not using a static class than using one.

提交回复
热议问题