In what situations is static method a good practic

2019-01-13 01:08发布

I have read the following discussions:

Should private helper methods be static if they can be static , and
Should all methods be static if their class has no member variables

It seems that people in general would accept static methods, but are a little bit skeptical about it, for the following 2 reasons:

  1. They are hard to test.
  2. They violate the OO principle. (They are functions, not methods, said a person.)

And the most acceptable static methods are private static ones. But then why do static methods exist at all, and in what situations they are the first priority to be adopted?

11条回答
Juvenile、少年°
2楼-- · 2019-01-13 01:10

Think of this for a moment. In OO coding, every single function call actually looks like this:

method(object this, object arg1, object arg2) where this is the object you are calling. All it really is is syntax sugar for this. Additionally it allows you to clearly define scope of variables because you have object variables etc.

Static methods simply don't have a "this" parameter. Ie you pass variables in and possibly get a result back out. 1.) is the main reason people avoid them, you can't create an interface to a static method (yet) so you can't mock out the static method to test it.

Secondly OO are procedures functions etc. Static methods make a lot of sense in certain situations, but they can always be made into a method on an object.

Mind you, you couldn't remove this without a hack:

static void Main(string[] args)
{
}

The code that starts your application MUST be callable WITHOUT a reference to an object. So they give you flexibility, whether you choose to use them in your scenario will be predicated by your requirements.

查看更多
Bombasti
3楼-- · 2019-01-13 01:10

Another good scenario for static methods are implementations of the Factory pattern, where you are allowing for instances of a class to be constructed in a specific manner.

Consider the Calendar class, which has a set of static getInstance methods, each returning instances primed with the desired TimeZone and/or Locale or the default.

查看更多
Luminary・发光体
4楼-- · 2019-01-13 01:10

I think a definite case for static methods is when you cannot make them dynamic, because you cannot modify the class.

This is typical for JDK objects, and also all objects coming from external libraries, and also primitive types.

查看更多
成全新的幸福
5楼-- · 2019-01-13 01:13

It's much easier to refactor methods that are static. It discourages unnecessary references to field members that make the coupling tight. It's also easier to understand calling code because it explicitly passes any objects it interacts with.

查看更多
叛逆
6楼-- · 2019-01-13 01:14

First of all, you can't dismiss static-methods there is a reason people still use it.

  1. some design patterns are based on static methods, singleton for example:

    Config.GetInstance();

  2. Helper functions, lets say you have a byte-stream and you want a function to convert it into a string of Hex numbers.

there are many uses for static-methods, saying that does not mean that some people abuse it too much(Code Review is best option when people abuse code).

查看更多
相关推荐>>
7楼-- · 2019-01-13 01:14

Util classes can be OK to be static. As in someone elses example above, with escaping strings. The problem lies when those utils classes perform functions that we would want to mock. For example, the FileUtils class in apache commons - it is often the case that I want to mock file interactions without having to play with real files. If this was an instance class, it would be easy.

查看更多
登录 后发表回答