room,id,entity构造这样一个数据结构需要大量运用设计的思想

[ 1896 查看 / 2 回复 ]

  1.     class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             BizHandler biz = BizHandler.Instance;
  6.             biz.Register("register", "2", new Entity()
  7.             {
  8.                 Ip = "1.2.3.4",
  9.                 port = "80",
  10.                 Type = "register",
  11.                 ServerName = "cj"
  12.             });
  13.             biz.Register("getList", "5", new Entity()
  14.             {
  15.                 Ip = "1.2.3.4",
  16.                 port = "80",
  17.                 Type = "register",
  18.                 ServerName = "cj"
  19.             });

  20.             biz.PrintAllRooms();
  21.         }
  22.     }

  23.     class Entity
  24.     {
  25.         public string Type { get; set; }
  26.         public string Ip { get; set; }
  27.         public string port { get; set; }
  28.         public string ServerName { get; set; }

  29.         public override string ToString()
  30.         {
  31.             return this.Ip + "-" + this.ServerName;
  32.         }
  33.     }

  34.     /// <summary>
  35.     /// 简单工厂类,返回一个Container
  36.     /// </summary>
  37.     class ContainerFactory
  38.     {
  39.         public static Container<string, string, Entity> Instance
  40.         {
  41.             get
  42.             {
  43.                 return container;
  44.             }
  45.         }
  46.         //构造器私有化
  47.         private ContainerFactory() { }

  48.         private static Container<string, string, Entity> container = new Container<string, string, Entity>();
  49.     }

  50.     //自定义一个泛型类,作为 3个元素 存储的容器——相当于三层架构中的数据存储
  51.     //开写这个底层容器之前还没有感觉,现在发现写下来以后,这个容器还是有不少作用的。
  52.     //比较像的是 DataBase中的 分组 功能。如相同的房间room中,有不同的id号的人,拿到他们的Entity。
  53.     //T1对应于String的room,T2多半就是UniqueIdentifier的唯一标识,T3可以是任何object
  54.     class Container<T1, T2, T3>
  55.     {
  56.         private Dictionary<T1, Dictionary<T2, T3>> dictionary = null;
  57.         public Container()
  58.         {
  59.             dictionary = new Dictionary<T1, Dictionary<T2, T3>>();
  60.         }

  61.         //存入相对简单,因为是 黑匣 操作。用户无需关心底层是如何实现存储的。
  62.         /// <summary>
  63.         /// 类似于数据库中的‘增’
  64.         /// </summary>
  65.         /// <param name="t1"></param>
  66.         /// <param name="t2"></param>
  67.         /// <param name="t3"></param>
  68.         public void PutThree(T1 t1, T2 t2, T3 t3)
  69.         {
  70.             Dictionary<T2, T3> subMap = null;
  71.             if (subMap == null)
  72.             {
  73.                 subMap = new Dictionary<T2, T3>();
  74.                 dictionary.Add(t1, subMap);
  75.             }
  76.             subMap.Add(t2, t3);
  77.         }

  78.         //查就要麻烦的多

  79.         /// <summary>
  80.         /// 拿到最大的那个集合中的Key的List
  81.         /// 亦即返回所有的Rooms组成一个集合(看看一共有多少Rooms)
  82.         /// </summary>
  83.         /// <returns></returns>
  84.         public ICollection<T1> GetKeys()
  85.         {
  86.             return dictionary.Keys;

  87.         }

  88.         /// <summary>
  89.         /// 根据指定的T1,获取 子Map(SubMap)
  90.         /// 亦即根据Room,获得其中的 唯一标识+实体 List
  91.         /// </summary>
  92.         /// <param name="t1"></param>
  93.         /// <returns></returns>
  94.         public Dictionary<T2, T3> GetSubMap(T1 t1)
  95.         {
  96.             return dictionary[t1];
  97.         }

  98.         /// <summary>
  99.         /// 得到小集合的键的List
  100.         /// 亦即给定一个 子集合,就得到一个 唯一标识 组成的List
  101.         /// </summary>
  102.         /// <param name="subMap"></param>
  103.         /// <returns></returns>
  104.         public ICollection<T2> GetSubKeys(Dictionary<T2, T3> subMap)
  105.         {
  106.             return subMap.Keys;
  107.         }

  108.         /// <summary>
  109.         /// 根据给定的一个 大集合 的Key,得到小集合的Key组成的集合:重载的方法
  110.         /// 亦即给定 Room ,就可以得到该room中所有的 唯一标识 List
  111.         /// </summary>
  112.         /// <param name="t1"></param>
  113.         /// <returns></returns>
  114.         public ICollection<T2> GetSubKeys(T1 t1)
  115.         {
  116.             return dictionary[t1].Keys;
  117.         }

  118.         /// <summary>
  119.         /// 根据给定的 子集合 和 子集合的键 得到对应的 值
  120.         /// 亦即给定一个 唯一标识+实体 List 和 标识,可以得到对应的实体
  121.         /// </summary>
  122.         /// <param name="subMap"></param>
  123.         /// <param name="t2"></param>
  124.         /// <returns></returns>
  125.         public T3 GetSubValues(Dictionary<T2, T3> subMap, T2 t2)
  126.         {
  127.             return subMap[t2];
  128.         }

  129.         /// <summary>
  130.         /// 给定 大集合的主键和小集合的主键,就能得到对应的值,很像 复合主键:重载的方法
  131.         /// 亦即给定 room 和 id ,就能得到对应的 object
  132.         /// </summary>
  133.         /// <param name="t1"></param>
  134.         /// <param name="t2"></param>
  135.         /// <returns></returns>
  136.         public T3 GetSubValues(T1 t1, T2 t2)
  137.         {
  138.             //var subMap = this.GetSubMap(t1);
  139.             return this.GetSubValues(this.GetSubMap(t1), t2);

  140.         }

  141.    
  142.     }

  143.     //增加一层中间层,将DAL与UI隔离开,也便于测试。且实现单例模式
  144.     class BizHandler
  145.     {
  146.         private static BizHandler _instance = _instance = new BizHandler();

  147.         internal static BizHandler Instance
  148.         {
  149.             get { return BizHandler._instance; }

  150.         }
  151.         private BizHandler()
  152.         {

  153.         }

  154.         //持有一个下层的引用
  155.         Container<string, string, Entity> container = ContainerFactory.Instance;

  156.         /// <summary>
  157.         /// 类似于数据库中的‘增’
  158.         /// </summary>
  159.         /// <param name="t1"></param>
  160.         /// <param name="t2"></param>
  161.         /// <param name="t3"></param>
  162.         public void Register(string room, string id, Entity server)
  163.         {
  164.             container.PutThree(room, id, server);
  165.         }

  166.         public void PrintAllRooms()
  167.         {
  168.             if (container.GetKeys().Count == 0)
  169.             {
  170.                 Console.WriteLine("还没有放入任何元素");
  171.             }
  172.             else
  173.             {
  174.                 foreach (var keys in container.GetKeys())
  175.                 {
  176.                     Console.WriteLine(keys);
  177.                     Console.WriteLine("------房间内的东西-------");

  178.                     foreach (var subkey in container.GetSubKeys(keys))
  179.                     {
  180.                         Console.Write(subkey + ":::::::::::");
  181.                         Console.WriteLine(container.GetSubValues(keys, subkey));
  182.                     }
  183.                     Console.WriteLine();
  184.                     Console.WriteLine();
  185.                 }
  186.             }
  187.         }

  188.      



  189.     }
复制代码
1

评分次数

    本主题由 管理员 张波老师 于 2010-1-7 18:09:41 执行 设置精华/取消 操作
    TOP

    class Entity 这个类是不是可以用结构呢。

    原来用到的简单工厂 创建类时也是可以把类放到属性里的和放到方法里不一样 当然这里只有一个单独的类。

    private static BizHandler _instance = _instance = new BizHandler();
    这句为什么赋值了两个相同的变量?

    基本都看的董 没有在细看。
    TOP