运用范型技术重构后的代码

[ 544 查看 / 0 回复 ]

玉不琢,不成器。批评与自我批评,会成为前进的动力。这个代码的味道,闻起来的感觉,要优雅很多了。
  1.     /// <summary>
  2.     /// 模板方法的抽象基类
  3.     /// 这里使用 范型类 以获得更高的通用性 Genaric and Extensible
  4.     /// </summary>
  5.     /// <typeparam name="T"></typeparam>
  6.     abstract class TemplateMethod<T>
  7.     {
  8.         public List<T> ReturnList(params int[] list)
  9.         {
  10.             DataStorageServiceClient client = new DataStorageServiceClient();
  11.             // 使用 "client" 变量在服务上调用操作。

  12.             T[] retVal = RetrieveFromProxy(client, list[0]);
  13.             //DataUsage[] retVal = client.GetUsageList();


  14.             // 始终关闭客户端。
  15.             client.Close();

  16.             return new List<T>(retVal);
  17.         }
  18.         protected abstract T[] RetrieveFromProxy(DataStorageServiceClient client, int id);
  19.     }

  20.     /// <summary>
  21.     /// 取得应用清单
  22.     /// </summary>
  23.     class DataUsageArchieve : TemplateMethod<DataUsage>
  24.     {
  25.         protected override DataUsage[] RetrieveFromProxy(DataStorageServiceClient client, int id)
  26.         {
  27.             return client.GetUsageList();
  28.         }
  29.     }

  30.     /// <summary>
  31.     /// 取得组别清单
  32.     /// </summary>
  33.     class DataGroupArchieve : TemplateMethod<DataGroup>
  34.     {
  35.         protected override DataGroup[] RetrieveFromProxy(DataStorageServiceClient client, int id)
  36.         {
  37.             return client.GetGroupList(id);
  38.         }
  39.     }

  40.     /// <summary>
  41.     /// 取得项目清单
  42.     /// </summary>
  43.     class DataItemArchieve : TemplateMethod<DataItem>
  44.     {
  45.         protected override DataItem[] RetrieveFromProxy(DataStorageServiceClient client, int id)
  46.         {
  47.             return client.GetItemsList(id);
  48.         }
  49.     }
  50. }
复制代码
TOP