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/
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· dotnet 源代码生成器分析器入门
· 官方的 MCP C# SDK:csharp-sdk
· 一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
· 一步一步教你部署ktransformers,大内存单显卡用上Deepseek-R1
· 一次Java后端服务间歇性响应慢的问题排查记录