适当使用静态方法 [英] Appropriate use of Static Method
问题描述
从概念上讲,它是适合使用一个静态方法(C#)时,该方法只需要输入和重新输入作为输出?例如:
Conceptually, is it appropriate to use a static method (C#) when the method will only take inputs and reformat the input as the output? For example:
public static string FormatString(string inputString){
return "some formatting" + inputString + "Some other formatting";
}
如果我现在能有几个这些类型的方法,将静态实用级是一个好主意?
If I were to have a few of these types of methods, would a static "utility" class be a good idea?
推荐答案
我会用其他的答案一致,到目前为止,它肯定是有道理的一很多的时间。
I'd agree with the other answers so far that it certainly makes sense a lot of the time.
的有时的,您可能希望通过定义一个接口,实现真正给自己多一点灵活性,以实例的方法。这使您使用的选项的不同的的方法,在你的代码的道路。
Sometimes, you may want to actually give yourself a little more flexibility by defining an interface and implementing that with instance methods. This gives you the option of using different methods in your code down the road.
下面是我的意思的例子。假设你正在使用你这个的formatString
法中的某些代码的地方,看起来像这样:
Here's an example of what I mean. Say you are using this formatString
method of yours in some code somewhere that looks like this:
public void DumpToConsole()
{
foreach (DataField field in m_fields)
{
Console.WriteLine(StringUtils.formatString(field.ToString()));
}
}
OK,这是伟大的。 (其实,这是愚蠢的,但不管,只用于说明!),但你可以做这样的方法更灵活有它接受的接口的,其中您可能有提供完全不同种类的各种实现格式:
OK, this is great. (Actually, it's stupid, but whatever—for illustration only!) But you could make such a method more flexible by having it accept an interface, of which you might have various implementations which provide completely different sorts of formatting:
public void DumpToConsole(IFormatter<DataField> formatter = null)
{
// Perhaps you could specify a default. Up to you.
formatter = formatter ?? Formatter<DataField>.Default;
foreach (DataField field in m_fields)
{
Console.WriteLine(formatter.Format(field));
}
}
然后,而不是 StringUtils的
是一个静态实用类,这将仅仅是有一个的实施类,提供了一个办法格式化某种类型的对象(在你的情况下,字符串
的对象;在我的例子,这些想象中的数据字段
对象)
Then instead of StringUtils
being a static utility class, it would be merely one implementation of a class that offers a way to format a certain type of object (in your case, string
objects; in my example, these imaginary DataField
objects).
所以这是说,所有的非常啰嗦的方式,这取决于。如果你瞄准了超灵活性在路上,的也许的你应该考虑实现一个接口,而不是一个静态辅助类去。
So this is all a very long-winded way of saying, it depends. If you're aiming for super flexibility down the road, maybe you should consider implementing an interface instead of going with a static helper class.
请注意,我在上面的例子,逼近问题的另一个完全可以接受的方式可以一直接受 Func键< T,串>
委托,而不是这个假设的 IFormatter< T>
接口。这主要是一个风格上的选择,在我看来。但往往接口都成为当有要自定义多种行为更逼真;即,定义方法接受3,4或更多的代表可以相比于接受单一接口很快变得繁琐。
Do note that in my example above, another completely acceptable way of approaching the problem could've been to accept a Func<T, string>
delegate instead of this hypothetical IFormatter<T>
interface. This is mostly a stylistic choice, in my opinion. But often interfaces become more realistic when there are multiple behaviors that you want to customize; i.e., defining methods that accept 3, 4, or more delegates can quickly become cumbersome compared to accepting a single interface.
这篇关于适当使用静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!