玉不琢,不成器。批评与自我批评,会成为前进的动力。这个代码的味道,闻起来的感觉,要优雅很多了。
- /// <summary>
- /// 模板方法的抽象基类
- /// 这里使用 范型类 以获得更高的通用性 Genaric and Extensible
- /// </summary>
- /// <typeparam name="T"></typeparam>
- abstract class TemplateMethod<T>
- {
- public List<T> ReturnList(params int[] list)
- {
- DataStorageServiceClient client = new DataStorageServiceClient();
- // 使用 "client" 变量在服务上调用操作。
- T[] retVal = RetrieveFromProxy(client, list[0]);
- //DataUsage[] retVal = client.GetUsageList();
- // 始终关闭客户端。
- client.Close();
- return new List<T>(retVal);
- }
- protected abstract T[] RetrieveFromProxy(DataStorageServiceClient client, int id);
- }
- /// <summary>
- /// 取得应用清单
- /// </summary>
- class DataUsageArchieve : TemplateMethod<DataUsage>
- {
- protected override DataUsage[] RetrieveFromProxy(DataStorageServiceClient client, int id)
- {
- return client.GetUsageList();
- }
- }
- /// <summary>
- /// 取得组别清单
- /// </summary>
- class DataGroupArchieve : TemplateMethod<DataGroup>
- {
- protected override DataGroup[] RetrieveFromProxy(DataStorageServiceClient client, int id)
- {
- return client.GetGroupList(id);
- }
- }
- /// <summary>
- /// 取得项目清单
- /// </summary>
- class DataItemArchieve : TemplateMethod<DataItem>
- {
- protected override DataItem[] RetrieveFromProxy(DataStorageServiceClient client, int id)
- {
- return client.GetItemsList(id);
- }
- }
- }
复制代码