I would agree with the other answers so far that this certainly makes sense a lot of the time.
Sometimes you may need to actually increase flexibility a bit by defining an interface and implementing it using instance methods. This gives you the opportunity to use various methods in your code along the way.
Here is an example of what I mean. Suppose you use this formatString method in some code somewhere that looks like this:
public void DumpToConsole() { foreach (DataField field in m_fields) { Console.WriteLine(StringUtils.formatString(field.ToString())); } }
OK that's great. (Actually, this is stupid, but anything — just for illustration!) But you could make this method more flexible if it adopted an interface from which you can have various implementations that provide completely different types of formatting:
public void DumpToConsole(IFormatter<DataField> formatter = null) {
Then instead of StringUtils , which is a static utility class, this will be just one implementation of the class that offers a way to format a specific type of object (in your case, string objects; in my example, these are imaginary DataField objects).
So this is a very long way of saying it depends. If you want maximum flexibility in the future, perhaps you should consider introducing an interface instead of going with a static helper class.
Note that in my example above, another quite acceptable way to approach the problem could be to accept a delegate Func<T, string> instead of this hypothetical IFormatter<T> interface. In my opinion, this is mainly a stylistic choice. But often, interfaces become more realistic when there are several behaviors that you want to customize; that is, defining methods that accept 3, 4 or more delegates can quickly become cumbersome compared to adopting a single interface.
Dan tao
source share