I am creating a utility method GetServiceTicketNumber() inside a utility class, since the method will be used often i do not want to instantiate each and every time so i made the method & _ticket as static.
UtilityManager contains several other methods too.
My question is :
1) Is this the right way to implement the functionality ?
2) whether to make UtilityManager also a static class/not?, what difference does it make ?
3) Whether the below code (for TicketProvider functionality) is written in singleton pattern ? (Considering most of the singleton class instantiate the same class UtilityManager.)
other info : class called in Asp.Net Application
public sealed class UtilityManager
{
public static readonly TicketProvider _ticket = new TicketProvider();
public static int GetServiceTicketNumber()
{
return _ticket.GetTicket();
}
}
Utility methods are best declared static and many code checker tools like stylecop will actually recommend your utility functions to be static, so you’re on the right track. If you want to have a singleton instance of the TicketProvider, you can use a static constructor to make sure that the field gets initialized before it is actually accessed and initialized only once. Also you can make the class static to indicate that this class is not designed to be instantiated but for utility use only. Below is my recommendation: