程序接收消息列表(基本類型)。列表中的每條消息必須根據其類型(後代類型)進行處理。但是,不同的消息需要不同的輸入才能正確處理。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