C Sharp/Design Patterns

Design Patterns are common building blocks designed to solve everyday software issues. Some basic terms and example of such patterns include what we see in everyday life. Key patterns are the singleton pattern, the factory pattern, and chain of responsibility patterns.

工厂格式编辑

单例编辑

public class MySingletonExample
{
   private static object obj = new object();
   private volatile static Hashtable _sharedHt;

   public static Hashtable Singleton
   {
     get 
      {
         if(_sharedHt == null){
             lock(obj){
                  if(_sharedHt == null){
                     _sharedHt = new Hashtable();
                  }
             }
         }
         return _sharedHt;
      }
      // set { ; }
     // Not implemented for a true singleton
   }

   // Class implementation here..
}

单例模式的使用:

  • ConfigurationSettings (Generic settings reader)
  • HttpApplication (ASP .NET的Application对象)
  • HttpCacheUtility (ASP .NET的Cache对象)
  • HttpServerUtility (ASP .NET的Server对象)