Is there anything wrong with a class with all stat

2019-03-08 13:12发布

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 passing along all or some of the arguments the entrance method received.

It isn't like a Math class with largely unrelated utility functions. In my own normal programming, I rarely write methods where Resharper pops and says "this could be a static method", when I do, they tend to be mindless utility methods.

Is there anything wrong with this pattern? Is this just a matter of personal choice if the state of a class is held in fields and properties or passed around amongst static methods using arguments?

UPDATE: the particular state that is being passed around is the result set from the database. The class's responsibility is to populate an excel spreadsheet template from a result set from the DB. I don't know if this makes any difference.

标签: c# java oop
16条回答
Ridiculous、
2楼-- · 2019-03-08 13:35

One of the disadvantages of using a static class is that its clients cannot replace it by a test double in order to be unit tested.

In the same way, it's harder to unit test a static class because its collaborators cannot be replaced by test doubles (actually,this happens with all the classes that are not dependency-injected).

查看更多
▲ chillily
3楼-- · 2019-03-08 13:38

if there's no need of creating an object of a class, then there's no issue in creating all method as static of that class, but i wanna know what you are doing with a class fullof static methods.

查看更多
乱世女痞
4楼-- · 2019-03-08 13:39

Does it help to rephrase the question:

Can you describe the data that the static methods operates on as an entity having:

  • a clear meaning
  • responsibility for keeping it's internal state consistent.

In that case it should be an instantiated object, otherwise it may just be a bunch of related functions, much like a math library.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-03-08 13:39

There is nothing wrong with this pattern. C# in fact has a construct called static classes which is used to support this notion by enforcing the requirement that all methods be static. Additionally there are many classes in the framework which have this feature: Enumerable, Math, etc ...

查看更多
我只想做你的唯一
6楼-- · 2019-03-08 13:40

Passing all state as method parameters can be a useful design pattern. It ensures that there is no shared mutable state, and so the class is intrinsicly thread-safe. Services are commonly implemented using this pattern.

However, passing all state via method parameters doesn't mean the methods have to be static - you can still use the same pattern with non-static methods. The advantages of making the methods static is that calling code can just use the class by referencing it by name. There's no need for injection, or lookup or any other middleman. The disadvantage is maintanability - static methods are not dynamic dispatch, and cannot be easily subclassed, nor refactored to an interface. I recommend using static methods when there is intrinsicly only one possible implementation of the class, and when there is a strong reason not to use non-static methods.

查看更多
聊天终结者
7楼-- · 2019-03-08 13:43

Nothing is wrong with it. It is a more "functional" way to code. It can be easier to test (because no internal state) and better performance at runtime (because no overhead to instance an otherwise useless object).

But you immediately lose some OO capabilities Static methods don't respond well (at all) to inheritance. A static class cannot participate in many design patterns such as factory/ service locator.

查看更多
登录 后发表回答