Alternatives to the Singleton Design Pattern
page 4 of 6
by Steven Smith
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 29317/ 59

Testing

Singletons are death to testability.  They cause tight coupling between different parts of your application.  If you must have them, then a common workaround is to add a Reset() or set() method to the Singleton so that test classes are able to change the instance that is being used, or to reset it to null before each test.  If you're not using an IOC Container to manage the liftetime of your objects, then the only way to test them effectively is to hack in back doors to your beautifully crafted Singleton pattern and then add a bunch of comments telling developers "don't call this outside of test classes".  It's crap.  It stinks.  Don't do it.


View Entire Article

User Comments

Title: Just use public static readonly   
Name: Ramon Smits
Date: 2010-12-08 7:42:43 PM
Comment:
Just use a public static readonly member. Easy, fast, lazy and threadsafe!

public class MyClass
{
public static readonly MyClass Instance = new MyClass();
private MyClass(){}
}

Much easier then this will be difficult to do!

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 4:51:46 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search