Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>They are both examples of the <em>factory method</em> pattern. The only difference is that the second example has the method in its own static class. </p> <p>This would be an example of the <em>abstract factory</em> pattern:</p> <pre><code>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); } } </code></pre> <p>Each MessageProcessor gets its own factory class which makes use of polymorphism.</p> <p>Passing a ProcessBuilder to create the process would be the <em>strategy</em> pattern:</p> <pre><code>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()); </code></pre> <p>The simplest solution would be to encapsulate a factory method:</p> <pre><code>static void Process(Message msg, DataDomain data) { var p = getProcessor(msg.GetType()); p.Process(msg, data); } </code></pre> <p>If it's a small known number of types, you can use the series of type checks:</p> <pre><code>private static MessageProcessor getProcessor(Type msgType) { return (msgType == typeof(FooMessage)) ? new FooMessageProcessor() : (msgType == typeof(BarMessage)) ? new BarMessageProcessor() : new DefaultMessageProcessor(); } </code></pre> <p>Otherwise use a dictionary:</p> <pre><code>Dictionary&lt;Type,MessageProcessor&gt; processors; private static MessageProcessor getProcessor(Type msgType) { return processors[msgType]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload