简体   繁体   English

用于处理的设计模式(如果使用不同的实现)

[英]Design pattern for handling if else with different implementations

I have the following two types of processors 我有以下两种类型的处理器

public interface IDefaultProcessor1
{
    void Process(IProcess p);
}

public interface IDefaultProcessor2
{
    T Process<T>(IProcess p);
}

public class DefaultProcessor : IDefaultProcessor1
{
    public void Process(IProcess p)
    {
        try
        {
            foreach ...
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            throw;
        }
    }
}

public class AnotherProcessor : IDefaultProcessor2
{
    public T Process<T>(IProcess p)
    {
        try
        {
            foreach ...
            return p.Result()...
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            throw;
        }
    }
}

Most of the implementation code is exactly the same (error checking etc) except one returns a value. 大多数实现代码是完全相同的(错误检查等),只是返回一个值。 Is there a pattern to clean this up? 有没有一种模式可以解决这个问题?

Yes, there is. 就在这里。

Define both methods on the same interface: 在同一接口上定义两个方法:

public interface IDefaultProcessor
{
    void Process(IProcess p);
    TResult Process<TResult>(IProcess p);
}

And then: 接着:

public class DefaultProcessor : IDefaultProcessor
{
     public void Process(IProcess p)
     {
         DoProcess(p);
     }

     public TResult Process<TResult>(IProcess p)
     {
         object result = DoProcess(p);
         return (TResult)result;
     }

     private object DoProcess(IProcess p)
     {
         try
         {
             foreach ...
             return p.Result();
         }
         catch(Exception ex)
         {
              Console.WriteLine(ex.Message);
              throw;
         }
     }
}

There are many ways you can do this. 您可以通过多种方式执行此操作。 One thing you have to keep in mind is that there is no way you can have a method with a optional return. 您要记住的一件事是,您不可能有一个带有可选返回值的方法。 That been said, you can try one of the aproaches below: 话虽如此,您可以尝试以下方法之一:

Implement Template Pattern and return null to your process that don't return anything: 实现模板模式,并向不返回任何内容的过程返回null:

public abstract class AbstractProcess<T>
{
    public abstract T DoProcess();

    public T Process()
    {
        //do your common tasks

        return DoProcess();
    }
}

public class Process1 : AbstractProcess<Process1>
{
    public override Process1 DoProcess()
    {
        return new Process1();
    }
}

public class Process2 : AbstractProcess<Process2>
{
    public override Process2 DoProcess()
    {
        return null;
    }
}

Create an Interface with both methods, and then choose what is the best method to call (as pointed by Matias Cícero ) 使用这两种方法创建一个接口,然后选择最佳的调用方法(如MatiasCícero所指出)

public interface IProcessor
{
    T Process<T>();
    void Process();
}

public class Processor : IProcessor
{
    public void Process()
    {
        DoWork();
    }

    public T Process<T>()
    {
        return (T)DoWork();
    }

    public object DoWork()
    {
        // do your common tasks
    }
}

Create an Interface with a single method and decide if you will return something (or null) based on the type: 使用单个方法创建一个Interface并根据类型决定是否返回某些值(或null):

public interface IProcessor
{
    T Process<T>() where T : class;
}

public class Processor : IProcessor
{
    public T Process<T>() where T : class
    {
        var result = (T)DoWork();

        if (typeof(T) == typeof(Process2))
            return result;

        return null;
    }

    public object DoWork()
    {
        // do your common tasks
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM