gpt4 book ai didi

C# 类设计 : strategy pattern with some classes that have caching functionality?

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:54 25 4
gpt4 key购买 nike

我很确定有更好的方法可以做到这一点,我想获得一些反馈。

组件

我有以下(简化):

  • Item , 它实现了 IItem
  • A Doodad ,其中包含一个 List<IItem>
  • IItemExistenceVerifier需要 bool ItemExists(string ItemToCheck) 的界面
  • A StandardCachedItemExistenceVerifier ,其中包括 FillCache(List<IITem> items)
  • A DoodadValidator它接受 IItemExitenceVerifier并且有一个 Validate调用 ItemExists(item) 的方法对于 Doodad 的项目列表中的每个项目。

尝试用图表来表达这一点:

A class diagram attempting to illustrate the problem

目标

  • 我想要一个 StandardCachedItemExistenceVerifier和一个 StandardNonCachedItemVerifier我可以将其传递给 Doodad 验证器

问题/问题

在当前结构中:

  • 验证者将收到 IItemExistenceVerifier并且不知道它是否使用缓存。存在验证器将提前更新,因此我无法创建新的验证器并将项目传递到构造函数中)。
  • 我不能总是调用FillCache()作为验证的一部分,因为接口(interface)不需要它。

潜在选项

也许我可以:

  • 选项 1:实现 FillCache()甚至在 StandardNonCacheItemVerifier让它什么都不做? (这似乎是一种气味)
  • 选项 2:在验证器中,检查 IItemExistenceVerifier 是否实现一些其他接口(interface)( ICacheDrivenVerifier )或其他东西,然后调用 FillCache()如果是的话?
    • 选项 3:比这两者都更聪明的东西。

最佳答案

通常由 Steven 声明在他的 blog ,缓存是一个横切关注点,因此可以使用装饰器来解决。我不知道你的最终目标,但我假设它是如果缓存存在,使用缓存。如果没有,从查询中获取并放入缓存

您需要的是将操作分为 2 个类,一个类用于检索对象,另一个类用于验证。

这是一个例子:

public interface IItemRetriever{
public IEnumerable<IItem> GetList();
}

public class StandardItemRetriever : IItemRetriever{
public IEnumerable<IItem> GetList(){
// returning the data
}
}

public class CachedStandardItemRetriever : IItemRetriever{
public CachedStandardItemRetriever(StandardItemRetriever standardItemRetriever){
// property assignment
}

IEnumerable<IItem> items;
public IEnumerable<IItem> GetList(){
if(items == null || !items.Any())
{
items = this.standardItemRetriever.GetList();
}
return items;
}
}

public class StandardItemExistanceVerifier{
public StandardItemExistanceVerifier(IItemRetriever iItemRetriever){
// property assignment
}
}

有了这个,您的验证器将只需要由 IItemRetriever 注入(inject),其中可以是标准的或缓存的。

new StandardItemExistanceVerifier( new CachedStandardItemRetriever(new StandardItemRetriever()) );
new StandardItemExistanceVerifier( new StandardItemRetriever() );

关于C# 类设计 : strategy pattern with some classes that have caching functionality?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19524920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com