By Patrick


2015-10-13 15:00:06 8 Comments

In my application, I have to instantiate many different types of objects. Each type contains some fields and needs to be added to a containing type. How can I do this in an elegant way?

My current initialization step looks something like this:

public void testRequest() {

        //All these below used classes are generated classes from xsd schema file.

        CheckRequest checkRequest = new CheckRequest();

        Offers offers = new Offers();
        Offer offer = new Offer();
        HotelOnly hotelOnly = new HotelOnly();
        Hotel hotel = new Hotel();
        Hotels hotels = new Hotels();
        Touroperator touroperator = new Touroperator();
        Provider provider = new Provider();
        Rooms rooms = new Rooms();
        Room room = new Room();
        PersonAssignments personAssignments = new PersonAssignments();
        PersonAssignment personAssignment = new PersonAssignment(); 
        Persons persons = new Persons();
        Person person = new Person();
        Amounts amounts = new Amounts();

        offers.getOffer().add(offer);
        offer.setHotelOnly(hotelOnly);

        room.setRoomCode("roomcode");
        rooms.getRoom().add(room);

        hotels.getHotel().add(hotel);
        hotel.setRooms(rooms);

        hotelOnly.setHotels(hotels);

        checkRequest.setOffers(offers);

        // ...and so on and so on
    } 

I really want to avoid writing code like this, because it's a little messy having to instantiate each object separately and then initialize each field across multiple lines of code (e.g. having to call new Offer() and then setHotelOnly(hotelOnly) and then add(offer)).

What elegant methods can I use instead of what I have? Are there any "Factories" that can be used? Do you have any references/examples to avoid writing code like this?

I'm really interested in implementing clean code.


Context:

I'm developing a RestClient Application for sending post requests to a Webservice.

The API is represented as a xsd schema file and I created all the Objects with JAXB

Before sending a request I have to instantiate many Objects because they have dependencies with each other. (An Offer has Hotels, a Hotel has Rooms, a Room has Persons... And these Classes are the generated ones)

Thanks for your help.

6 comments

@snickers10m 2015-10-13 15:35:58

You can either use a constructor or a builder pattern or a variation of the builder pattern to fix the problem of having too many fields in your initialization step.

I'm going to extend your example a bit to prove my point of why these options are useful.

Understanding your example:

Lets say an Offer is simply a container class for 4 fields:

public class Offer {
    private int price;
    private Date dateOfOffer;
    private double duration;
    private HotelOnly hotelOnly;
    // etc. for as many or as few fields as you need

    public int getPrice() {
        return price;
    }

    public Date getDateOfOffer() {
        return dateOfOffer;
    }

    // etc.
}

As it stands in your example, to set values to these fields, you use setters:

    public void setHotelOnly(HotelOnly hotelOnly) {
        this.hotelOnly = hotelOnly;
    }

Unfortunately, this means if you need an offer with values in all of the fields, you have to do what you have:

Offers offers = new Offers();
Offer offer = new Offer();
offer.setPrice(price);
offer.setDateOfOffer(date);
offer.setDuration(duration);
offer.setHotelOnly(hotelOnly);
offers.add(offer);

Now let's look at improving this.

Option 1: Constructors!

A constructor other than the default constructor (the default constructor is currently Offer() ) is useful for initializing the values of the fields in your class.

A version of Offer using constructors would look like this:

public class Offer {
    private int price;
    private Date dateOfOffer;
    //etc.

    // CONSTRUCTOR
    public Offer(int price, Date dateOfOffer, double duration, HotelOnly hotelOnly) {
        this.price = price;
        this.dateOfOffer = dateOfOffer;
        //etc.
    }

    // Your getters and/or setters
}

Now, we can initialize it in one line!

Offers offers = new Offers();
Offer offer = new Offer(price, date, duration, hotelOnly);
offers.add(offer);

Even better, if you never use offer other than that single line: offers.add(offer); you don't even need to save it in a variable!

Offers offers = new Offers();
offers.add( new Offer(price, date, duration, hotelOnly) ); // Works the same as above

Option 2: Builder Pattern

A builder pattern is useful if you want the option of having default values for any of your fields.

The problem a builder pattern solves is the following messy code:

public class Offer {
    private int price;
    private Date dateOfOffer;
    // etc.

    // The original constructor. Sets all the fields to the specified values
    public Offer(int price, Date dateOfOffer, double duration, HotelOnly hotelOnly) {
        this.price = price;
        this.dateOfOffer = dateOfOffer;
        // etc.
    }

    // A constructor that uses default values for all of the fields
    public Offer() {
        // Calls the top constructor with default values
        this(100, new Date("10-13-2015"), 14.5, new HotelOnly());
    }

    // A constructor that uses default values for all of the fields except price
    public Offer(int price) {
        // Calls the top constructor with default values, except price
        this(price, new Date("10-13-2015"), 14.5, new HotelOnly());
    }

    // A constructor that uses default values for all of the fields except Date and HotelOnly
    public Offer(Date date, HotelOnly hotelOnly) {
        this(100, date, 14.5, hotelOnly);
    }

    // A bunch more constructors of different combinations of default and specified values

}

See how messy that can get?

The builder pattern is another class that you put inside your class.

public class Offer {
    private int price;
    // etc.

    public Offer(int price, ...) {
        // Same from above
    }

    public static class OfferBuilder {
        private int buildPrice = 100;
        private Date buildDate = new Date("10-13-2015");
        // etc. Initialize all these new "build" fields with default values

        public OfferBuilder setPrice(int price) {
            // Overrides the default value
            this.buildPrice = price;

            // Why this is here will become evident later
            return this;
        }

        public OfferBuilder setDateOfOffer(Date date) {
            this.buildDate = date;
            return this;
        }

        // etc. for each field

        public Offer build() {
            // Builds an offer with whatever values are stored
            return new Offer(price, date, duration, hotelOnly);
        }
    }
}

Now, you can not have to have so many constructors, but still are able to choose which values you want to leave default, and which you want to initialize.

Offers offers = new Offers();
offers.add(new OfferBuilder().setPrice(20).setHotelOnly(hotelOnly).build());
offers.add(new OfferBuilder().setDuration(14.5).setDate(new Date("10-14-2015")).setPrice(200).build());
offers.add(new OfferBuilder().build());

That last offer is simply one with all default values. The others are default values except the ones that I set.

See how that makes things easier?

Option 3: Variation of Builder Pattern

You can also use the builder pattern by simply making your current setters return the same Offer object. It's exactly the same, except without the extra OfferBuilder class.

Warning: As user WW states below, this option breaks JavaBeans - a standard programming convention for container classes such as Offer. So, you shouldn't use this for professional purposes, and should limit your use in your own practices.

public class Offer {
    private int price = 100;
    private Date date = new Date("10-13-2015");
    // etc. Initialize with default values

    // Don't make any constructors

    // Have a getter for each field
    public int getPrice() {
        return price;
    }

    // Make your setters return the same object
    public Offer setPrice(int price) {
        // The same structure as in the builder class
        this.price = price;
        return this;
    }

    // etc. for each field

    // No need for OfferBuilder class or build() method
}

And your new initialization code is

Offers offers = new Offers();
offers.add(new Offer().setPrice(20).setHotelOnly(hotelOnly));
offers.add(new Offer().setDuration(14.5).setDate(new Date("10-14-2015")).setPrice(200));
offers.add(new Offer());

That last offer is simply one with all default values. The others are default values except the ones that I set.


So, while it's a lot of work, if you want to clean up your initialization step, you need to use one of these options for each of your classes that have fields in them. Then use the initialization methods that I included with each method.

Good luck! Does any of this need further explanation?

@Patrick 2015-10-14 06:42:05

Thats a really good answer. Just one question. I already have the generated classes like Offer, Hotel... and dont want to edit those. Should I create new classes with an extension of the generated classes? Like public class OfferBuilder extends Offer and give fields like Hotel ?

@snickers10m 2015-10-14 16:09:13

Yes! Sort of. You can make OfferBuilder in a separate file so long as it has some way of setting those fields in an Offer. So Offer will either need setters or constructors, and then OfferBuilder will use the build pattern, with new fields like buildHotel, then will have a construct() or build() method where it returns a new Offer with the fields equal to those in the builder, by using those setters or constructors in Offer. Does that make sense?

@frifle 2015-10-16 07:47:45

If you want to use your Offeras a Javabean, eg as an entity, I would choose Option 2 because the setters return void as it shoud be for a bean.

@Patrick 2015-10-16 14:22:25

Option 2 is also my favorite option at the moment. But Iam still in improving the options in my code.

@snickers10m 2015-10-16 20:25:33

@Patrick I'm sorry, your latest comment doesn't make sense to me.

@WW. 2015-10-21 23:33:01

Good answer. Option 2 is the best. Option 1 gets out of hand and forces you to set things when you could live with defaults. Option 3 breaks java-bean conventions. We use option 2 extensively when setting up complex objects for tests.

@Conffusion 2015-10-22 16:00:47

Option 2 is my favorite. If you are using Eclipse and let it generate the getter/setter methods you can change the java code template to: ${field} = ${param}; return this;

@Filip 2015-10-19 08:37:12

I've always preferred using builder-pattern-with-a-twist because it provides much more than the basic approach of the builder pattern.

But what happens when you want to tell the user that she must call one builder method or the other, since it is crucial for the class you’re trying to build.

Think about a builder for a URL component. How would one think about the builder methods for encapsulating access to URL attributes, are they equally important, do they interact with each other, etc? While the query parameters or fragment are optional the hostname is not; you could say that protocol is also required but for that you can have a meaningful default, like http right?

Anyway, I don't know if this makes sense to your particular problem but I thought it would be worth mentioning for others to have a look at it.

@Patrick 2015-10-19 13:31:04

Thanks Filip for this answer. The part with mandatory fields is very useful.

@Filip 2015-10-19 13:35:39

Glad it helps :)

@HRgiger 2015-10-22 14:54:01

Dozer framework provides nice way to do copy values from ws object to your dto. Here is another example. Additionally if the getter/setter names are the same of both class you dont need custom converter

@Prasoon Joshi 2015-10-22 10:45:31

Ideally, an object should not be concerned about instantiating its dependencies. It should only worry about things that it is supposed to do with them. Have you considered any dependency injection framework? Spring or Google's Juice are quite versatile and have a small footprint.

The idea is simple, you declare the dependencies and let the framework decide when/how/where to create them and 'inject' it into your classes.

If you don't want to use any framework, you can take design notes from them and try to emulate their design patterns and tweak it for your use-case.

Also, you can simplify things to a certain extent by making proper use of Collections. For example, what additional feature does Offers have other than storing a collection of Offer? I'm not sure what your constraints there are but, if you can make that part a bit more cleaner you would have massive gains in all places where you are instantiating the objects.

@Patrick 2015-10-21 12:31:07

I just provide this answer because it was mentioned in a comment and I think it should also be a part of this enumeration of Design Patterns.


Null Object Design Pattern

Intent

The intent of a Null Object is to encapsulate the absence of an object by providing a substitutable alternative that offers suitable default do nothing behavior. In short, a design where "nothing will come of nothing"

Use the Null Object pattern when

  • an object requires a collaborator. The Null Object pattern does not introduce this collaboration--it makes use of a collaboration that already exists
  • some collaborator instances should do nothing
  • you want to abstract the handling of null away from the client

Here you find the full part of "Null Object" Design Pattern

@Verhagen 2015-10-21 08:23:08

Some nice answeres are already given here!

What came to my mind as an addition is Domain Driven Design. Specific the Building blocks part, with Entity, Value Object, Aggregate, Factory etc.

A nice introduction is given in Domain Driven Design - Quickly (pdf).

@Chris Moutray 2017-04-21 09:12:46

I don't see how referencing DDD like this directly answers the question.

@Verhagen 2017-04-21 11:30:56

Read it in the context of the answer where good examples are given, but a reference to the DDD theory is missing. Classes like: Offers, Hotels, Rooms - are DDD Repository classes. Classes like: Offer, Hotel, Room - are DDD Entity classes. Creating Builder classes for those entity classes would be a good way for creating the Inmutable entity classes. new Hotel.Builder().setName("Hampton by Hilton Amsterdam").create();

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] In Java, what is the best way to determine the size of an object?

  • 2008-09-09 17:07:03
  • Jay R.
  • 277783 View
  • 590 Score
  • 25 Answer
  • Tags:   java memory

9 Answered Questions

[SOLVED] Good or bad practice? Initializing objects in getter

4 Answered Questions

[SOLVED] Create the perfect JPA entity

9 Answered Questions

[SOLVED] Best Practice: Initialize JUnit class fields in setUp() or at declaration?

  • 2009-02-04 16:44:06
  • Craig P. Motlin
  • 137382 View
  • 112 Score
  • 9 Answer
  • Tags:   java junit

2 Answered Questions

[SOLVED] How to declare an object array length within another object

  • 2017-12-11 20:39:23
  • Helppp
  • 57 View
  • -3 Score
  • 2 Answer
  • Tags:   java

3 Answered Questions

[SOLVED] What is the correct way to initialize fields in Struts2?

3 Answered Questions

[SOLVED] Easy way to fill object's fields

  • 2015-06-25 06:00:05
  • Vlad Shevchenko
  • 118 View
  • 1 Score
  • 3 Answer
  • Tags:   java

7 Answered Questions

[SOLVED] Elegant way to iterate in C++

0 Answered Questions

Elegant way to chain data transformations in Scala?

Sponsored Content