Builder,Factory Method和Abstract Factory模式之间有什么区别?

时间:2022-02-17 20:11:00

A program receives a list of Messages (base type). Each message in the list has to be processed according to it's type (descendant type). However, different messages need different inputs in order to be processed correctly.

程序接收消息列表(基本类型)。列表中的每条消息都必须根据其类型(后代类型)进行处理。但是,不同的消息需要不同的输入才能正确处理。

What is the following technique called? (I haven't checked this code in a compiler)

下面介绍的技术是什么? (我没有在编译器中检查过这段代码)

abstract class MessageProcessor
{
    public static MessageProcessor GetProcessor(Message message, DataDomain data)
    {
        if (message.GetType() == typeof(FooMessage))
        {
            return new FooMessageProcessor(message, data.Name, data.Classification);

        }
        else if (message.GetType() == typeof(BarMessage))
        {
            return new BarMessageProcessor(message, data.AccountNo, data.CreditLimit);

        }
        else
            throw new SomeException("Unrecognized type");

    }

    public abstract void Process();     
}

And this one?

还有这个?

static class MessageProcessorFactory
{
    public static MessageProcessor GetProcessor(Message message, DataDomain data)
    {
        if (message.GetType() == typeof(FooMessage))
        {
            return new FooMessageProcessor(message, data.Name, data.Classification);

        }
        else if (message.GetType() == typeof(BarMessage))
        {
            return new BarMessageProcessor(message, data.AccountNo, data.CreditLimit);

        }
        else
            throw new SomeException("Unrecognized type");
    }
}

And what is it called if I can inject the ProcessBuilder class into a MessageProcessor (using a property or Setter) and then call Process?

如果我可以将ProcessBuilder类注入MessageProcessor(使用属性或Setter)然后调用Process,它会被调用什么?

What technique would be the best pattern for solving this problem?

什么技术是解决这个问题的最佳模式?

2 个解决方案

#1


10  

They are both examples of the factory method pattern. The only difference is that the second example has the method in its own static class.

它们都是工厂方法模式的示例。唯一的区别是第二个示例在自己的静态类中有方法。

This would be an example of the abstract factory pattern:

这将是抽象工厂模式的一个例子:

abstract class MessageProcessorFactory
 { public abstract MessageProcessor GetProcessor
                                     (Message message, DataDomain data);
 }

class FooMessageProcessorFactory :  MessageProcessorFactory
 { public override MessageProcessor GetProcessor
                                     (Message message, DataDomain data)
    { return new FooMessageProcessor(data.Name, data.Classification);
    }
 }

Each MessageProcessor gets its own factory class which makes use of polymorphism.

每个MessageProcessor都有自己的工厂类,它使用多态。

Passing a ProcessBuilder to create the process would be the strategy pattern:

传递ProcessBuilder来创建流程将是策略模式:

class MessageProcessor
 { ProcessBuilder builder;

   public MessageProcessor(ProcessBuilder builder)
    { this.builder = builder;
    }

   public void Process()
    { builder.BuildMessage();
      builder.BuildProcess();
      builder.Process();
    }
 }

var mp = new MessageProcessor(new FooProcessBuilder());

The simplest solution would be to encapsulate a factory method:

最简单的解决方案是封装工厂方法:

static void Process(Message msg, DataDomain data)
 { var p = getProcessor(msg.GetType());
   p.Process(msg, data);
 }

If it's a small known number of types, you can use the series of type checks:

如果它是一个很小的已知类型,您可以使用一系列类型检查:

private static MessageProcessor getProcessor(Type msgType)
 { return   (msgType == typeof(FooMessage)) ? new FooMessageProcessor()
          : (msgType == typeof(BarMessage)) ? new BarMessageProcessor()
          :                                   new DefaultMessageProcessor();
 }

Otherwise use a dictionary:

否则使用字典:

Dictionary<Type,MessageProcessor> processors;    

private static MessageProcessor getProcessor(Type msgType) 
 { return processors[msgType];
 }

#2


2  

In my understanding factory method defnies the abstract type of class with which to work with but delegates the creation of the concrete type to the succeeding/implementing classes. Abstract factory would define an interface for a manufactorer of a set of corresponding classes. A builder works simply to build an object step by step giving some control to the calling instance.

在我的理解中,工厂方法定义了要使用的类的抽象类型,但是将具体类型的创建委托给后续/实现类。抽象工厂将为一组相应类的制造商定义接口。构建器仅用于逐步构建对象,从而为调用实例提供一些控制。

#1


10  

They are both examples of the factory method pattern. The only difference is that the second example has the method in its own static class.

它们都是工厂方法模式的示例。唯一的区别是第二个示例在自己的静态类中有方法。

This would be an example of the abstract factory pattern:

这将是抽象工厂模式的一个例子:

abstract class MessageProcessorFactory
 { public abstract MessageProcessor GetProcessor
                                     (Message message, DataDomain data);
 }

class FooMessageProcessorFactory :  MessageProcessorFactory
 { public override MessageProcessor GetProcessor
                                     (Message message, DataDomain data)
    { return new FooMessageProcessor(data.Name, data.Classification);
    }
 }

Each MessageProcessor gets its own factory class which makes use of polymorphism.

每个MessageProcessor都有自己的工厂类,它使用多态。

Passing a ProcessBuilder to create the process would be the strategy pattern:

传递ProcessBuilder来创建流程将是策略模式:

class MessageProcessor
 { ProcessBuilder builder;

   public MessageProcessor(ProcessBuilder builder)
    { this.builder = builder;
    }

   public void Process()
    { builder.BuildMessage();
      builder.BuildProcess();
      builder.Process();
    }
 }

var mp = new MessageProcessor(new FooProcessBuilder());

The simplest solution would be to encapsulate a factory method:

最简单的解决方案是封装工厂方法:

static void Process(Message msg, DataDomain data)
 { var p = getProcessor(msg.GetType());
   p.Process(msg, data);
 }

If it's a small known number of types, you can use the series of type checks:

如果它是一个很小的已知类型,您可以使用一系列类型检查:

private static MessageProcessor getProcessor(Type msgType)
 { return   (msgType == typeof(FooMessage)) ? new FooMessageProcessor()
          : (msgType == typeof(BarMessage)) ? new BarMessageProcessor()
          :                                   new DefaultMessageProcessor();
 }

Otherwise use a dictionary:

否则使用字典:

Dictionary<Type,MessageProcessor> processors;    

private static MessageProcessor getProcessor(Type msgType) 
 { return processors[msgType];
 }

#2


2  

In my understanding factory method defnies the abstract type of class with which to work with but delegates the creation of the concrete type to the succeeding/implementing classes. Abstract factory would define an interface for a manufactorer of a set of corresponding classes. A builder works simply to build an object step by step giving some control to the calling instance.

在我的理解中,工厂方法定义了要使用的类的抽象类型,但是将具体类型的创建委托给后续/实现类。抽象工厂将为一组相应类的制造商定义接口。构建器仅用于逐步构建对象,从而为调用实例提供一些控制。