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 7073465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:55:58+00:00 2026-05-28T05:55:58+00:00

So, let’s start with some background [modified to make more concrete[. I have realised

  • 0

So, let’s start with some background [modified to make more concrete[. I have realised I can replace the following:

abstract class MessageHandler {
  public void handleMessage(Message m) {
    validateMessage(m);
    processMessage(m);
  }

  protected void validateMessage(Message m) {
    // Default validation logic
  }

  protected abstract void processMessage(Message m);
}

class FakeMessageHandler extends MessageHandler {
  proteced void processMessage(Message m) {}
}

with the next block of code:

interface IMessageProcessor {
  public void processMessage(Message m);
}

class FakeMessageProcessor implements IMessageProcessor {
  public void processMessage(Message m) {}
}

class MessageHandler {
  private IMessageProcessor processor;
  public MessageHandler(IMessageProcessor processor) {
    this.processor = processor;
  }

  public void handleMessage(Message message) {
    validateMessage(message);
    processor.processMessage(message);
  }

  protected void validateMessage(Message message) {
    // Default validation logic.
  }
}

That is, I can replace the abstract method with an injected interface to allow for easier testing. Now let’s say that the design stipulates that people can optionally override the methods:

class FakeMessageHandler extends MessageHandler {
  protected void validateMessage(Message m) {}
  protected void processMessage(Message m) {}
}

The injected interface now cannot be used as there is only 1 abstract method in MessageHandler. Yet, I can’t force the injected interface to contain method validateMessage(Message message) as the original point of using the abstract class was to define a default implementation of this method.

Is there some sort of elegant pattern to convert this into composition for the purposes of dependency injection and easier testing?

  • 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. Editorial Team
    Editorial Team
    2026-05-28T05:55:59+00:00Added an answer on May 28, 2026 at 5:55 am

    Here’s my take on this:

    Instead of extending the MessageHandler, I have a single MessageHandler-class, that is a composition of IMessageProcessor and IMessageValidator:
    Class diagram

    Hope I got the UML-diagram right, it’s been a while…

    Anyway, let’s take a look at the MessageHandler:

    
    class MessageHandler
    {
        private IMessageProcessor processor;
        private IMessageValidator validator;
    
        public MessageHandler(IMessageProcessor processor)
        {
            this.processor = processor;
    
            //Use the given processor as validator, if it implements the IMessageValidator-interface
            if(IMessageValidator.class.isAssignableFrom(processor.getClass()))
            {
                this.validator = (IMessageValidator)processor;
            }   
        }
    
        public void setMessageValidator(IMessageValidator validator)
        {
            this.validator = validator;
        }
    
        public void handleMessage(Message message)
        {
            validateMessage(message);
            processor.processMessage(message);
            System.out.println("Message " + message + " handled by MessageHandler");
        }
    
        protected void validateMessage(Message message)
        {
            if(validator != null)
            {
                validator.validateMessage(message);
            }
            else
            {
                System.out.println("No IMessageValidator-implementation set, using default validation for message " + message);
            }
        }
    }
    

    The MessageHandler has two private members, IMessageProcessor and IMessageValidator (It is a composition of a processor and a validator). Validator can be left unset, in which case the default-validation logic will kick in when handling a message.

    In this example, if the passed-in processor also implements the IMessageValidator-interface,it will be used as the validator. This is probably what you wanted, because you can use the same constructor to build MessageHandlers using the default validation or custom-validation logic, depending if the passed in object implements only IMessageProcessor or both IMessageProcessor and IMessageValidator (for convenience, I have extended a third interface, IValidatingMessageProcessor from these interfaces). If the validator-logic is implemented separately (only implements IMessageValidator), it can be set using the setValidator-method.

    There’s no need to extend MessageHandler, as you can implement both the processing and validation -logic outside of the handler, either separately or in a single class that implements both processing and validation.

    Here are the classes I’ve used, hope this helps:

    Zip-package in MediaFire

    Text-form:

    Message.java:

    
    public class Message 
    {
        private int number;
    
        public Message(int number)
        {
            this.number = number;
        }
    
        public String toString()
        {
            return "Msg " + number;
        }
    }
    

    IMessageProcessor.java:

    
    interface IMessageProcessor 
    {
      public void processMessage(Message m);
    }
    

    IMessageValidator.java:

    
    public interface IMessageValidator 
    {
        public void validateMessage(Message m);
    }
    

    IValidatingMessageProcessor.java:

    
    public interface IValidatingMessageProcessor extends IMessageProcessor, IMessageValidator
    {
    }
    

    FakeMessageProcessor.java:

    
    public class FakeMessageProcessor implements IMessageProcessor
    {
        public void processMessage(Message m)
        {
            System.out.println("Using FakeMessageProcessor to process message " + m);
        }
    }
    

    FakeMessageValidator.java:

    
    public class FakeMessageValidator implements IMessageValidator
    {
        public void validateMessage(Message m)
        {
            System.out.println("Using FakeMessageValidator to validate message " + m);      
        }
    }
    

    FakeMessageProcessorAndValidator.java:

    
    public class FakeMessageProcessorAndValidator implements IValidatingMessageProcessor
    {
        public void validateMessage(Message m)
        {
            System.out.println("Using FakeMessageProcessorAndValidator for validating message " + m);
        }
    
        public void processMessage(Message m)
        {
            System.out.println("Using FakeMessageProcessorAndValidator for processing message " + m);       
        }
    }
    

    Simple testing main for the above classes (just outputs stuff):

    
    public class MessageTest
    {   
        public static void main(String[] args)
        {
            //Using processor implementing only IMessageProcessor, MessageHandler will use default validation
            IMessageProcessor processor = new FakeMessageProcessor();
            MessageHandler handler = new MessageHandler(processor);
    
            handler.handleMessage(new Message(1));
    
            //Setting separate validator to existing MessageHandler-instance
            handler.setMessageValidator(new FakeMessageValidator());
    
            handler.handleMessage(new Message(2));
    
            //Using processor implementing both IMessageProcessor and IMessageValidator
            processor = new FakeMessageProcessorAndValidator();
            handler = new MessageHandler(processor);
    
            handler.handleMessage(new Message(3));
        }
    }
    

    output:

    
    No IMessageValidator-implementation set, using default validation for message Msg 1
    Using FakeMessageProcessor to process message Msg 1
    Message Msg 1 handled by MessageHandler
    Using FakeMessageValidator to validate message Msg 2
    Using FakeMessageProcessor to process message Msg 2
    Message Msg 2 handled by MessageHandler
    Using FakeMessageProcessorAndValidator for validating message Msg 3
    Using FakeMessageProcessorAndValidator for processing message Msg 3
    Message Msg 3 handled by MessageHandler
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have some text as follows: do this, do that, then this,
let's say I have the following string: string s = A B C D
Let's see if I make myself clear. I have an old set of scripts
Let's say I have the following entity: public class Store { public List<Product> Products
Let's say, I have a .NET 2 installed. Can I programmatically install version 4
Let say I have the following desire, to simplify the IConvertible's to allow me
Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's say I have the string: hello world; some random text; foo; How could

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.