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.

Related Questions

Sponsored Content

28 Answered Questions

[SOLVED] How do I declare and initialize an array in Java?

  • 2009-07-29 14:22:27
  • bestattendance
  • 4511950 View
  • 2053 Score
  • 28 Answer
  • Tags:   java arrays

12 Answered Questions

[SOLVED] How to directly initialize a HashMap (in a literal way)?

53 Answered Questions

15 Answered Questions

[SOLVED] Initialize class fields in constructor or at declaration?

  • 2008-08-23 19:59:42
  • mmcdole
  • 133655 View
  • 412 Score
  • 15 Answer
  • Tags:   java

23 Answered Questions

[SOLVED] How to initialize HashSet values by construction?

15 Answered Questions

19 Answered Questions

[SOLVED] How do I make the method return type generic?

Sponsored Content