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對象)