• Adapter模式进行代码重构


    Adapter(适配器)模式主要是将一个类的某个接口转换成一个兼容的接口。

    下面是实现一个商品检索的示例
    【Bad Code】

     public class Product
     {
     }
    
     public class ProductRepository
     {
         public IList<Product> GetAllProductsIn(int categoryId)
         {
             IList<Product> products = new List<Product>();
             //从数据库中读取Product数据
             return products;
         }
     }
    
     public class ProductService
     {
         private ProductRepository _productRepository;
    
         public ProductService()
         {
             _productRepository = new ProductRepository();
         }
    
         public IList<Product> GetAllProductsIn(int categoryId)
         {
             IList<Product> products;
             string storegeKey = string.Format("products_in_category_id_{0}", categoryId);
             products = (List<Product>)HttpContext.Current.Cache.Get(storegeKey);
             if (products == null)
             {
                 products = _productRepository.GetAllProductsIn(categoryId);
                 //将商品列表保存到缓存中
                 HttpContext.Current.Cache.Insert(storegeKey, products);
             }
             return products;
         }
     }
    

    这段代码中的主要问题:

    • ProductService类强依赖ProductRepository类。
    • 强依赖于HttpContext缓存。

    【Code Refactoring】

     public class Product
     {
     }
    
     public class ProductRepository : IProductRepository
     {
         public IList<Product> GetAllProductsIn(int categoryId)
         {
             IList<Product> products = new List<Product>();
             //从数据库中读取Product数据
             return products;
         }
     }
    
     public class ProductService
     {
         //private ProductRepository _productRepository;
         //依赖抽象,而不是依赖具体
         private IProductRepository _productRepository;
         private ICacheStorage _cacheStorage;
    
         //通过构造函数注入,由客户端代码去实现IProductRepository、ICacheStorage接口
         public ProductService(IProductRepository productRepository, ICacheStorage cacheStorage)
         {
             _productRepository = productRepository;
             _cacheStorage = cacheStorage;
         }
    
         public IList<Product> GetAllProductsIn(int categoryId)
         {
             IList<Product> products;
             string storegeKey = string.Format("products_in_category_id_{0}", categoryId);
    
             //  products = (List<Product>)HttpContext.Current.Cache.Get(storegeKey);
             //不再依赖HttpContext缓存
             products = _cacheStorage.Retrieve<List<Product>>(storegeKey);
    
             if (products == null)
             {
                 products = _productRepository.GetAllProductsIn(categoryId);
                 //将商品列表保存到缓存中
                 //HttpContext.Current.Cache.Insert(storegeKey, products);
    
                 _cacheStorage.Store(storegeKey, products);
             }
             return products;
         }
     }
    
     public interface IProductRepository
     {
         IList<Product> GetAllProductsIn(int categoryId);
     }
    
     public interface ICacheStorage
     {
         void Remove(string key);
         void Store(string key, object data);
         //检索
         T Retrieve<T>(string key);
     }
    
     /// <summary>
     /// HttpContext缓存实现
     /// </summary>
     public class HttpContextCacheAdapter : ICacheStorage
     {
         public void Remove(string key)
         {
             HttpContext.Current.Cache.Remove(key);
         }
    
         public T Retrieve<T>(string key)
         {
             T itemStored = (T)HttpContext.Current.Cache.Get(key);
             if (itemStored == null)
                 itemStored = default(T);
             return itemStored;
         }
    
         public void Store(string key, object data)
         {
             HttpContext.Current.Cache.Insert(key, data);
         }
     }
    

    Adapter模式的目的就是提供一个兼容的接口,比如上面的缓存机制改为Memcached,只需要增加一个MemcachedCacheAdapter的实现,而不用去该ProductService类,也就是达到了对类扩展开放,对修改封闭的原则。

    ASP.NET设计模式:https://book.douban.com/subject/6958404/

  • 相关阅读:
    025、MySQL字符串大小写转化函数,文本转化大写,文本转化小写
    024、MySQL字符串替换函数,文本替换函数
    023、MySQL取文本长度取字符串长度
    022、MySQL字符串的拼接
    021、MySQL变量的使用,在MySQL中创建存储过程,并添加变量
    020、MySQL创建一个存储过程,显示存储过程,调用存储过程,删除存储过程
    019、MySQL取本季度开始时间和本季度结束时间
    018、MySQL取满足日期在两个日期之间的所有数据
    017、MySQL取第4本季度开始和结束日期
    016、MySQL取本年第一季度开始日期
  • 原文地址:https://www.cnblogs.com/yangsofter/p/Adapter.html
Copyright © 2020-2023  润新知