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

  • SEARCH
  • Home
  • 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 7082717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:05:20+00:00 2026-05-28T07:05:20+00:00

I have the following db table: id method_id 1 1 1 2 1 3

  • 0

I have the following db table:

id method_id
1   1
1   2
1   3

and 2 classes:

EmailController and Smscontroller

in my code, I need to iterate over the table and according to the method_id (1 or 2) to invoke the send method of either EmailController or Smscontroller.

What is the recommended design pattern for it?

EDITED

There could be 100 methods! I put only 3. This is why I do not prefer the if else.

As well, the object that I send to EmailController send method is different than the one that I send to SmsController send method.

In EmailController I need to send User object.
In SmsController I need to send Manager object

  • 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-28T07:05:21+00:00Added an answer on May 28, 2026 at 7:05 am

    I can’t think of a design pattern. But for ultimate flexibility you can have a design similar to this:

    public interface Sendable /* or Sender, SendingManager, etc. */ {
      public int getId();
      public void send();
    }
    
    public class EmailController implements Sendable {
    }
    
    public class SmsController implements Sendable {
    }
    
    public class Sendables {
    
     private Map<Integer, Sendable> sendables = new HashMap<Integer, Sendable>();
    
     public void addSendable(Sendable s) {
       this.sendables.put(s.getId(), s);
     }
    
     public void sendById(Integer id) {
       this.sendables.get(id).send();
     }
    
    }
    

    Then you can use it like this:

    Sendables sendables = new Sendables();
    sendables.add(new EmailController());
    sendables.add(new SmsController());
    sendables.add(new ChatController());
    // etc.
    
    
    Row row = table.getRow(...); // let's assume this gets a row from your table
    sendables.send(row.getId());
    

    Another solution could be to have an extra table like this:

    TABLE: CLASS_NAMES
    method_id class_name
    1         "com.foo.SmsController"
    2         "com.foo.EmailController"
    

    And then pass class_name to Class.forName and let it instantiate the appropriate controller for you to use.

    EDIT: A reflection-based version of the code as suggested by Luis. Note that for production use you should ensure that the passed parameters are valid (not null, etc.) and also handle exceptions with rigor.

    TABLE: CLASS_NAMES

    method_id class_name                 param_class_name
    1         "com.foo.SmsController"    "com.foo.Manager"
    2         "com.foo.EmailController"  "com.foo.User"
    

    SendManager

    public class SendManager {
    
        private static final String SEND_METHOD_NAME = "send";
    
        /* DAO for the CLASS_NAMES tables */
        private ClassNameDAO classNameDao;
    
        /**
         * Gets the row corresponding to methodId, for example
         * (1, "com.foo.SmsController", "com.foo.Manager") then using reflection
         * instantiates an instance of SmsController and invokes its send method
         * with <code>param</code> passed to it.
         */
        public void send(int methodId, Object param) throws Exception {
            ClassNameRow classNameRow = classNameDao.findByMethodId(methodId);
    
            String senderParameterClassName = className.senderParameterClassName();
            Class paramClass = Class.forName(senderParameterClassName);
    
            if (!paramClass.isInstance(param)) {
                throw new IllegalArgumentException("methodId and param are not compatible");
            }
    
            String senderClassName = classNameRow.getSenderClassName();
            Class senderClass = Class.forName(senderClassName);     
    
            /* Your sender classes must be JavaBeans and have no-arg constructors */
            Object sender = senderClass.newInstance();
    
            Class paramClass = Class.forName(senderParameterClassName);
    
            Method send = senderClass.getMethod(SEND_METHOD_NAME, paramClass);
    
            send.invoke(sender, param);
        }
    
    }
    

    Sample Usage

    SendManager sendManager = new SendManager();
    
    Manager m = ...;
    sendManager.send(1, m);
    
    User u = ...;
    sendManager.send(2, u);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that uses the SqlClient.ExecuteScalar method to return an ID
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have the following table relationship in my database: Parent / \ Child1 Child2
I have the following table schema; CREATE TABLE `db1`.`sms_queue` ( `Id` INTEGER UNSIGNED NOT
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
I have the following table in MySQL (version 5): id int(10) UNSIGNED No auto_increment
I have the following table custid ordid qty datesold 1 A2 12 2008-01-05 2
I have the following table: 'committee' table commname profname ======================== commA bill commA jack
I have the following table and data in SQL Server 2005: create table LogEntries
I have the following table in MSSQL2005 id | business_key | result 1 |

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.