程序接收消息列表(基本类型)。列表中的每条消息必须根据其类型(后代类型)进行处理。但是,不同的消息需要不同的输入才能正确处理。Builder,Factory Method和Abstract Factory模式之间有什么区别?
什么是以下技术称为? (我没有在编译器中检查过这个代码)
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();
}
而这个呢?
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");
}
}
如果我可以将ProcessBuilder类注入到MessageProcessor中(使用属性或Setter)然后调用Process,那叫什么?
什么技术将是解决这个问题的最佳模式?
我会这样做,但我只是使用DataDomain对象作为示例。在我们的应用程序中,没有封装所有输入值的类型,并且在某些情况下,这意味着将参数传递给一个方法不需要它。 – ilitirit 2008-10-08 20:39:41