gpt4 book ai didi

c# - 复合设计模式 : how to pass results from one component into another?

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

我有以下代码:

interface IService
{
void Execute();
}

class ServiceA : IService
{
public void Execute() { ... }
}

class ServiceB : IService
{
public void Execute() { ... }
}

class ServiceComposite : IService
{
List<IService> _services = new List<IService>();

public ServiceComposite()
{
_services.Add(new ServiceA());
_services.Add(new ServiceB());
}

public void Execute()
{
foreach (IService service in _services)
{
service.Execute();
}
}
}

问题在于 ServiceB 依赖于 ServiceA 的某些结果。我的想法是创建用于存储结果的容器类,然后将其注入(inject)到 ServiceA 和 ServiceB 中:

class ServiceResults
{
public string SomeProperty {get; set;}
}

public ServiceComposite()
{
ServiceResults result = new ServiceResults();
_services.Add(new ServiceA(result));
_services.Add(new ServiceB(result));
}

我想知道这是否是解决问题的最佳方法。也许它违反了一些我不知道的原则或规则,或者仅仅是“代码味道”。执行此操作的更好方法是什么?

最佳答案

如果 ServiceB 需要 ServiceA 的结果才能正常运行,那么为什么不根据 ServiceA 使用 ServiceB >?

public interface IService 
{
void Execute();
}

public class ServiceA : IService
{
public void Execute() { ... }
}

class ServiceB : IService
{
public ServiceB(IService service)
{
Service = service;
}

public void Execute() { ... }

public IService Servie { get; set; }
}

然后如果您执行一个集合中的所有 Service,您可以添加一个 ServiceBase 以确保该服务只执行一次:(完整示例)

在这个基本实现上,您可以添加:异步 ExecuteInnerExecute 执行的线程安全检查、用于生成特定 的享元工厂code>IService,有一个 ResponseBase,其中包含每个 Service 的派生响应....

public class ServiceResponse { }
public interface IService
{
ServiceResponse Execute();
}

public abstract class ServiceBase : IService
{
public ServiceResponse Execute()
{
if (_response == null)
{
_response = InnerExecute();
}
return _response;
}

public abstract ServiceResponse InnerExecute();

private ServiceResponse _response;
}

public class ServiceA : ServiceBase
{
public override ServiceResponse InnerExecute()
{
return new ServiceResponse();
}
}

public class ServiceB : ServiceBase
{
public ServiceB(IServiceFactory serviceFactory)
{
ServiceFactory= serviceFactory;
}

public override ServiceResponse InnerExecute()
{
return ServiceFactory.GetServices(ServicesTypes.ServiceA).Execute();
}

public IServiceFactory ServiceFactory { get; set; }
}

无论谁使用这些服务:

public enum ServicesTypes 
{
ServiceA,
ServiceB....
}

public interface IServiceFactory
{
IEnumerable<IService> GetServices();

IService GetServices(ServicesTypes servicesTypes);
}

public class SomeOtherThatExecuteServices
{
public SomeOtherThatExecuteServices(IServiceFactory serviceFactory)
{
ServiceFactory = serviceFactory;
}

public IEnumerable<ServiceResponse> ExecuteServices()
{
return ServiceFactory.GetServices()
.Select(service => service.Execute());
}

public IServiceFactory ServiceFactory { get; set; }
}

您可能希望通过一些映射键访问工厂,并且您可能希望在 SomeOtherThatExecuteServices 中有适当的逻辑,但所有这些都会让您走上正确的道路(+使用一些适当的IoC 容器)

关于c# - 复合设计模式 : how to pass results from one component into another?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38481859/

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