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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:46:13+00:00 2026-05-13T09:46:13+00:00

I am dealing with a set of message objects, each of which has a

  • 0

I am dealing with a set of message objects, each of which has a unique identifier corresponding to them. Each message can be constructed either from a Map, or from a ByteBuffer (the messages are binary, but we know how to transfer to and from a binary representation).

The current implementation for constructing these messages is roughly as follows:

public static Message fromMap(int uuid, Map<String, Object> fields) {
    switch (uuid) {
      case FIRST_MESSAGE_ID:
        return new FirstMessage(fields);
        .
        .
        .
      default:
          // Error
          return null;
    }
}

public static Message fromByteBuffer(int uuid, ByteBuffer buffer) {
    switch (uuid) {
      case FIRST_MESSAGE_ID:
        return new FirstMessage(buffer);
        .
        .
        .
      default:
          // Error
          return null;
    }
}

Now, Josh Bloch’s Effective Java talks about Item 1: Consider static factory methods instead of constructors, and this seems to be a place where this pattern is useful (clients don’t directly access the constructors of the Message subtypes; instead they go through this method). But I do not like the fact that we have to remember to keep two switch statements updated (violates the DRY principle).

I would appreciate any insight into the best way to accomplish this; we’re not caching objects (each call to fromMap or fromByteBuffer will return a new object), which negates some of the benefit of using a static factory method like this. Something about this code strikes me as wrong, so I would love to hear the community’s thoughts on whether this is a valid way to construct new objects, or if not what a better solution would be.

  • 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-13T09:46:13+00:00Added an answer on May 13, 2026 at 9:46 am

    Maybe you could create an interface MessageFactory and implementations of it:

    public interface MessageFactory {
       Message createMessage(Map<String, Object> fields);
       Message createMessage(ByteBuffer buffer);
    }
    
    public class FirstMessageFactory implements MessageFactory {
      public Message createMessage(Map<String, Object> fields){
        return new FirstMessage(fields);
      }
    
      public Message createMessage(ByteBuffer buffer){
        return new FirstMessage(buffer);
      }
    
    }
    

    next, a method getFactoryFromId in the same class as the methods above:

    public static MessageFactory getMessageFactoryFromId(int uuid){
     switch (uuid) {
      case FIRST_MESSAGE_ID:
        return new FirstMessageFactory();
        ...
      default:
          // Error
          return null;
      }
    }
    

    However, instead of this, it is better to create a Hashmap containing the ids and the factories, so you don’t have to create a new Factory object everytime you are creating a message. See also the comment below.

    and your methods:

    public static Message fromMap(int uuid, Map<String, Object> fields)  {
      getMessageFactoryFromId(uuid).createMessage(fields);
    }
    
    public static Message fromByteBuffer(int uuid, ByteBuffer buffer) {
      getMessageFactoryFromId(uuid).createMessage(buffer);
    }
    

    This way, you are using the factory pattern, and there is no need to have two times the same switch statement.

    (didn’t test this, so possibly some compile-errors/typos)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem while dealing with a data set which the value range
I'm having some trouble compiling/linking a set of classes, several of them dealing with
C++ programs can define and set a new_handler() that should be called from memory
I'm dealing with a data set that has some obvious errors in the data
I am dealing with a set of native functions that return data through dynamically-allocated
Dealing with Java - which the option you prefer in the most cases -
ive been dealing with a kind of no error message my application is throwing
I can set an EntityReference on an entity without having to load the relevent
I'm dealing with a problem in a kernel module that get data from userspace
I am dealing with a large data set and it takes some days to

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.