<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12
      代碼改變世界

      打造屬于自己的設計模式

      2011-09-05 12:05  熬夜的蟲子  閱讀(1091)  評論(4)    收藏  舉報

      設計模式 一般初級、中級、高級程序員都喜歡掛在嘴邊的詞。想必大家身邊也有不少關于設計模式的書。設計模式是程序員的老前輩們根據自己的項目經驗積累起來的解決方案集。所以,沒必要把設計模式看成是硬性的東西,別人的經驗參考一下即可,了解設計思路,為什么這種場景要用這種模式。也許是老一輩的程序員們確實比現在的程序員要強很多,畢竟現在網上很難找到自己摸索的設計模式了。

      蟲子不才就先拋磚引玉了。

      簡單介紹2個我項目中經常用到的模式

      1.機器人插件模式

      何所謂機器人插件,可以這樣理解。你有一個機器人,但是需要這個機器人干什么并不確定。插入不同的卡片可以讓機器人做出不同的行為。原理和aop類似,aop是站在系統級的角度,插件模式基于行為。

      直接看代碼

      定義行為:

      View Code
       public static event EventHandler<RobotCancelEventArgs> DeleteingRobot;
              protected virtual void OnDeleteingRobot(RobotCancelEventArgs e)
              {
                  EventHandler<RobotCancelEventArgs> tmp = DeleteingRobot;

                  if (tmp != null)
                      tmp(this, e);

              }

              public static event EventHandler<RobotCancelEventArgs> bhingRobot;
              /// <summary>
              
      /// 機器人行動前觸發事件
              
      /// </summary>
              protected virtual void OnbhingRobot(RobotCancelEventArgs e)
              {
                  EventHandler<RobotCancelEventArgs> tmp = bhingRobot;
                  if (tmp != null)
                      tmp(this, e);
              }

              public static event EventHandler<EventArgs> bhedRobot;
              /// <summary>
              
      /// 機器人行動后觸發
              
      /// </summary>
              protected virtual void OnbhedRobot(EventArgs e)
              {
                  EventHandler<EventArgs> tmp = bhedRobot;
                  if (tmp != null)
                      tmp(this, e);
              }

              public static event EventHandler<ServingEventArgs> ServingRobot;
              /// <summary>
              
      /// 調用機器人行為時觸發
              
      /// </summary>
              protected virtual void OnServing(ServingEventArgs e)
              {
                  EventHandler<ServingEventArgs> tmp = ServingRobot;
                  if (tmp != null)
                      tmp(this, e);
              }

       行為實例:

      View Code
       public bool bhRobot(out string Message)
              {
                  Message = string.Empty;
                  RobotCancelEventArgs e = new RobotCancelEventArgs();
                  OnServing(e);
                  if (!e.Cancel)
                  {
                      var v = RobotService.bhRobot(thisout Message);

                      if (v)
                      {
                          OnbhedRobot(EventArgs.Empty);
                          return true;
                      }
                      return false;
                  }
                  else
                  {
                      Message = e.Message;
                      return false;
                  }
              }

      注冊卡片:

      View Code
       [Extension("""1.0""熬夜的蟲子")]
          public class CardRobot
          {

              static CardRobot()
              {
              
                  Robot.ServingRobot += new EventHandler<ServingEventArgs>(Comment_ServingDelegate);
              }

              static void Comment_ServingDelegate(object sender, ServingEventArgs e)
              {
                  try
                  {
                    //do something
                  }
                  catch (Exception ex)
                  {
                      //Log4N.WarnLog("BadWordFilterExtension Exception:", ex);
                  }
              }
          }

      根據自定義屬性反射加載

      View Code
       void Application_Start(object sender, EventArgs e) 
          {
              ExtensionManager.InitExtension();
              System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin"));
              foreach (var item in di.GetFiles("*.dll", System.IO.SearchOption.TopDirectoryOnly))
              {
                  System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(item.FullName);
                  Type[] types = assembly.GetTypes();
                  foreach (Type type in types)
                  {
                      object[] attributes = type.GetCustomAttributes(typeof(CommentEngine.Core.ExtensionAttribute), false);
                      foreach (object attribute in attributes)
                      {
                          if (ExtensionManager.ExtensionIsEnable(type.Name))
                              assembly.CreateInstance(type.FullName);
                      }
                  }
              }
          }

       

      2.單例擴展模式

      先看看傳統的單例模式 撇開懶漢式等區分

      負責創建Singleton類自己的唯一實例,并提供一個getInstance的方法,讓外部來訪問這個類的唯一實例。

      View Code
       public static T GetInstance(object lockKey, T instance, Func<T> onCreateInstance)
              {
                  if (instance == null)
                  {
                      if (lockKey == null)
                          lockKey = LockKey;
                      lock (lockKey)
                      {
                          if (instance == null)
                          {
                              try
                              {
                                  if (onCreateInstance == null)
                                      instance = new T();
                                  else
                                      instance = onCreateInstance();
                              }
                              catch
                              {
                                  instance = default(T);
                              }
                          }
                      }
                  }
                  return instance;
              }

      優點:節省系統資源。

      適用場景:

      當需要控制一個類的實例只能有一個,而且客戶只能從一個全局訪問點訪問它時,可以選用單例模式,這些功能恰好是單例模式要解決的問題。

      擴展場景:

      海量即時消息,消息處理機制含有復雜邏輯,體積龐大。這個時候你用單例模式可能導致消息不即時,不用單例資源占用太大。

      可能有人就會思考,能不能控制實例數目為2個,3個,或者是任意多個呢?目的都是一樣的,節約資源啊,有些時候單個實例不能滿足實際的需要,會忙不過來,根據測算,6個實例剛剛好。
      思路很簡單,就是利用上面通過Map來緩存實現單例的示例,進行變形,一個Map可以緩存任意多個實例。

      也可以做得更通用一點,將單例的似有構造函數公開。實例的排他性通過對象鎖來實現

      部分代碼

      View Code
       private static Hashtable<String, Example> map = new System.Collections.Hashtable();
              private static int flag = 1;  
              private static int MAX = 6;           
              public static Example getInstance()
              {

                  String value = "aoyedechongzi" + flag;
                  Example Example = map.get(value);
                  if (Example == null)
                  {
                      Example = new Example();
                      map.put(value, Example);
                  }
                  flag++;
                  if (flag > MAX)
                  {
                      flag = 1;
                  }
                  return Example;
              }  

       

       

       

       

      主站蜘蛛池模板: 色欲综合久久中文字幕网| 久久丁香五月天综合网| 国产精品中文第一字幕| 国产精品亚洲av三区色| 免费看黄片一区二区三区| 中国少妇人妻xxxxx| 久久精品夜夜夜夜夜久久| 国产国拍亚洲精品永久软件| 国产成人一区二区三区视频免费 | 国内精品久久久久久久97牛牛| 国产短视频精品一区二区| 国产福利深夜在线播放| 一边添奶一边添p好爽视频| 国产精品一品二区三区日韩| 国产精品免费中文字幕| 国产人与zoxxxx另类| 国产肥臀视频一区二区三区| 色777狠狠狠综合| 绯色蜜臀av一区二区不卡| 亚洲丰满熟女一区二区v| 久久精品午夜视频| 免费无遮挡无码永久视频| 国产视频精品一区 日本| 亚洲成在人线AⅤ中文字幕| 国产成人MV视频在线观看| 亚洲色拍拍噜噜噜最新网站| 中文字幕av无码免费一区| 和黑人中出一区二区三区| 亚洲av无码乱码在线观看野外| 国产欧美久久一区二区三区| 国产99视频精品免费视频6| 色综合色国产热无码一| 韩国午夜福利片在线观看| 不卡乱辈伦在线看中文字幕| 精品人妻av区乱码| 亚洲精品不卡av在线播放| 国产精品乱码高清在线观看 | 国产精品 无码专区| 无码免费大香伊蕉在人线国产| 免费视频国产在线观看| 亚洲国产一区二区精品专|