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/

转载于:https://www.cnblogs.com/yangsofter/p/Adapter.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值