复合设计模式:如何将结果从一个组件传递到另一个组件? [英] Composite design pattern: how to pass results from one component into another?

查看:143
本文介绍了复合设计模式:如何将结果从一个组件传递到另一个组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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的一些结果。我的想法是创建容器类来存储结果,然后将其注入到ServiceA和ServiceB中:

The problem is that ServiceB depends on some results from ServiceA. My idea is to create container class for storing the results, then inject it into both ServiceA and ServiceB:

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

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

我想知道是否是解决问题的最佳方式。也许它打破了我不知道的一些原则或规则,或者只是代码气味。这样做的最好方法是什么?

I am wondering if it is the best possible way to solve the problem. Maybe it breaks some principles or rules of which I don't know, or is simply "code smell". What are the better ways to do this?

推荐答案

如果 ServiceB ServiceA 的结果使其正常工作,为什么不具有 ServiceB 取决于 ServiceA

If ServiceB needs ServiceA's result for it to function properly then why not have ServiceB depending on ServiceA?

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; }
}

然后在执行所有服务的情况下在集合中,您可以添加一个 ServiceBase ,以确保此服务仅执行一次:(全例)

Then in the case that you execute all Services in a collection you can add a ServiceBase to make sure this service executes only once: (Full example)

在这个基本的实现中,你可以添加:Async 执行,线程安全检查执行 InnerExecute Flyweight factory for the生成具体的 IService ,有一个 ResponseBase 与每个服务的派生响应 ....

On this basic implementation you can add: Async Execute, Thread-safe checking of the executing of InnerExecute, Flyweight factory for the generation of the specific IService, Have a ResponseBase with derived responses for each 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; }
}

无论谁使用这些服务 s:

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容器)

Probably you will want to access the factory by some mapping key and probably you will want proper logic in the SomeOtherThatExecuteServices but all this will put you on the right way (+Using some proper IoC Container)

这篇关于复合设计模式:如何将结果从一个组件传递到另一个组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆