ASP.NET Hosting

Design Patterns potential hazards

As Object Oriented Programming fans, you all know Design Patterns and rely on them for your designs.

This post by Chris Johnson about a potential hazard with the Singleton pattern is a good reminder that Design Patterns are not magical recipes. They can be sources of problems like any other piece of code if not implemented carefully.

In the comments, people linked to interesting articles about the correct implementation of the Singleton pattern in C#:

Better check your implementations accordingly...

As Mark Townsend says in his article: Design patterns are very useful software design concepts that allow teams to focus on delivering the very best type of applications, whatever they may be. The key is to make proper and effective use of design patterns.

2 Comments

  • Okey. Okey.

    Now everybody know how to create correct Singleton Pattern in C#.



    But how about another problem - currently lifetime for Singleton is infinite.

    And in case if it was used only once - it will remain in memory for-ever.



    But in case if Singleton used soly as cache for data what you can recreate (for example pre-parsed configuration file data) you can save memory.



    How to create SingletonCache pattern with memory/resources freed if it's not used for long time ??



  • Why not put your singleton in the Cache instead of a static variable?

    System.Web.Caching.Cache can be used even outside of ASP.NET applications.

Comments have been disabled for this content.