I have an existing microservice that talks to a Natural Language Processing (NLP) product and fetches around 50 fields. I need to create domain objects in Java now from these fields.

I read about builder pattern and read Effective Java. I understand builder pattern and using fluent approach is a good way to build objects, when we know some mandatory properties and have other optional properties.

In my case though, no fields are optional. I want to know what is the best practice to build objects in such a scenario.

I will have a wrapper MqObject which will be made of many user-defined domain objects, as shown below:

public class MqObject {

private UserDefinedClassA a;
private UserDefinedClassB b;
private UserDefinedClassC c;
private UserDefinedClassD d;

//getters and setters
  • so the 50 fields are for the dto. Is your question about the creation of MqObject or about the creation of UserDefinedClassX? Are all the fields used in the construction of all domain objects, are several fields used for more than one domain object or is the construction of each domain object independent of the others?
    – Christophe
    May 23 '21 at 15:51
  • You need to clarify @Christophe 's questions to improve the question. Jun 24 '21 at 8:03

Joshua Blocks builder pattern (not to be confused with GoFs builder pattern) simulates named parameters in languages that do not have them (Java doesn't. C# does). This allows avoidance of constructors with a large number of parameters when one can't use setters because of a desire to create an immutable object.

If you're thinking of making 50 methods to set 50 fields after 50 calls to the database NLP product then please stop now. You're using a monkey wrench as a hammer.

A domain object is supposed to have an infrastructure layer to abstract away the details of the database outside world. One of the infrastructure layers jobs is to take whatever data structure the database outside world provides and digest it into something (container, parameter object, DTO, ...) that is easy for the domain object to consume.

Also, just because a field exists doesn't mean it needs to be public. Real objects hide their data and instead use it to control their own behavior.

  • Thanks for your reply. No there won't be 50 calls there is 1 call which stores the result in one big DTO. Now I need to create different domain objects from this DTO. So, I was wondering it there is a best practise for this scenario. May 23 '21 at 15:11
  • Avoid creating a God object with 50 fields. Break them up into groupings where fields that are used together live together. In 50 fields there must be more then one idea. I'd be able to give you further advice if I knew about this DTO. Can you point me to it? May 23 '21 at 15:15
  • Thanks. I got what you are saying, DTO is the existing code actually. I need to extract data to domain objects (which I will have in the client jar of the new microservice). While creating domain objects from DTO, shall I go with the builder pattern in Effective Java, I am not sure because here I dont have optional fields. Alll the fields in all domain objects are required. May 23 '21 at 15:31
  • The Bloch Builder works fine with required parameters if you disallow all nulls on build. Otherwise you’ll need a step builder that won’t let you call build until all other steps have been called. They work but are a pain to set up. Can’t show me the existing DTO? May 23 '21 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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