I working on a project requiring quite a lot handlers to handle requests.
In general, i'm comunicating with a communication card, receiving requests from any source
(User Interface, Server it doesn't really matter to me), i need to process the request and send the appropriate data to the card.
The current hierarchy :

The simplified UML

Example usage :

   IContext context = /* Initialized with the required values */;
   IHandlerFactory factory = /* Maybe a singleton instance ? */;

   IHandler handler = factory.GetHandler(context);
   var dataToSend = handler.Handle(context);

The problem is that i wanted to use the Command Pattern and Factory Pattern eventually combining them somehow (Command Creator).
But as you can see from the example below (in the CanCreate method) i have a lot to check before determining the appropriate handler, so :

  • Map wont do here because of the excessive check before returning the appropriate handler
  • Creating a factory with a large (not friendly) if-else is definitely not an option

Example for command :
(Command creator)

 public class JammerHandlerCreator : IHandlerCreator
        public bool CanCreate(IContext context)
            // There is no actual meaning to the equality, just for the example sake
            return (
                (context.Operator == 0x50 ||
                 context.Operator == 0x60 ||
                 context.Operator == 0x87)
                 context.Extra != 0);

            // The condition is usually longer and contain more validations 
            // (context object is also bigger)

        public IHandler Create()
            return new JammerHandler();


  public class JammerHandler : IHandler
        private const byte JAM_COMMAND = 0xA;
        public void Handle(IContext context)
            byte[] data = new byte[4];
            data[0] = JAM_COMMAND;
            data[1] = 0x0;
            data[2] = (byte)(context.Extra >> 8);
            data[3] = (byte)context.Extra;

Im going back and forth for quite a long time thinking on whats the write way to implement it.

Any suggestion on how to design it ?
Thanks in advance !

1 Answer 1


I would get rid of the IHandlerCreator all together, and add a CanHandle method to the IHandler interface. My reasoning is that the logic for handling and determining if you CanHandle are closely related, and likely to change at the same time. In your IHandlerFactory, keep an array of IHandler objects, which could be injected in the constructor of the IHandlerFactory. And just iterate over the array until you find an IHandler that can handle it. Of course you may end up with multiple IHandlers that can handle a request, so you need to work out what you want to have happen. Do you want multiple IHandlers to handle it? or only the first one? Or maybe you want to throw an exception in that case. Whatever way you want to handle it, you should write tests for your IHandlerFactory that spell that out explicitly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.