Best practice for Utility class?

We currently have a utility class that handles a lot of string formatting, date display and similar functions, as well as a generic / static class.

Is this the "right" way to do things, or should we instantiate the utility class as needed?

Our main goal here is to reduce the amount of memory, but application performance is also considered.

PS. We are using .NET 2.0

+5
source share
6 answers

If the class has any state at all, then it is better to make objects out of it. Singletones are pain associated with object dependencies and thread safety.

, , , .

+3

"" - ?

OOD : -)

Java/#. # , .

, (, ), , (, ).

, , , , , . , ( ), . , , .

Java " " , .

+4

.NET, ? , .

+3

.NET? , , . . , ,

public static string SpecialFormat(string text)

public static string SpecialFormat(this string text)

, SpecialFormat , .

0

, / / .

, . final/closed/nonvirtual/non-extendable, .

If your language supports it, you might consider overriding the built-in classes / definitions for types that accept these functions, but be careful with namespace conflicts. In this case, set aside the style guide or the actual standard of your language of choice to choose an idiomatic approach.

0
source

Static classes are great for what you described - just be careful that any common state (static member variables) is shared in a thread-safe way.

0
source

All Articles