Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 54447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:13:17+00:00 2026-05-10T17:13:17+00:00

A program receives a list of Messages (base type). Each message in the list

  • 0

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?

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:13:18+00:00Added an answer on May 10, 2026 at 5:13 pm

    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.

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

    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];  } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 61k
  • Answers 61k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Elaborating on grieve's response, why not use a functor? E.g.:… May 11, 2026 at 9:56 am
  • added an answer It looks like it is a bug, and isn't dependent… May 11, 2026 at 9:56 am
  • added an answer I'm a real moron. That's how it appears in the… May 11, 2026 at 9:56 am

Related Questions

A program receives a list of Messages (base type). Each message in the list
We've run into a thorny problem. We are writing a c++ program that receives
A program that I work on assumes that the UUID generated by the Windows
Occasionally a program on a Windows machine goes crazy and just hangs. So I'll
In a program that I'm writing, I wanted to make a ConfigParser that's read
On a program of me, the splint checker warns: expat-test.c:23:1: Function exported but not
I am creating a program that will be installed using the .net installer project.
Imagine we have a program trying to write to a particular file, but failing.
I am working on a program that needs to create a multiple temporary folders
I'm currently designing a program that will involve some physics (nothing too fancy, a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.