Search code examples
javaoopobjectparadigms

How should I populate my object?


I have an object that I want to populate with information. I retrieve the information from a number of different services. I made a helper class that has one public method and then has a number of private methods that do the work to call the services. What I have written works fine but I'm not sure if it is the correct way to do this.

You may be wondering why I need an object holding all this information. I need it all in one object because I create a json object from this java object and pass that to the javascript layer.

What is wrong with my approach and is there a programming paradigm I should be following to do something like this?

Example:

Person object with getters and setters for firstName, lastName, age, height, weight, list of favourite foods, list of favourite countries, list of comments.

  • Service 1 gives firstName, lastName, age, height and weight
  • Service 2 gives list of favourite countries and list of favourite foods
  • Service 3 gives a list of the comments made by the person

I have a personHelper class that looks like this:

public class PersonHelper{

public Person getPerson(userDetails){
    Person person = new Person();
    this.setPersonDetails(person, userDetails);
    this.setFavourites(person, userDetails);
    this.setComments(person, userDetails);

    return person;
}

private Person setPersonalDetails(Person person, UserDetails userDetails){

    returnedObj = callToService1(userDetails);
    person.setFirstName(returnedObj.getFirstName());
    person.setLastName(returnedObj.getLastName());
    person.setAge(returnedObj.getAge());
    person.setHeight(returnedObj.getHeight();
    person.setWeight(returnedObj.getWeight());

    return person;
}

private Person setFavourites(Person person, UserDetails userDetails){

    <List>favsList = callToService2(userDetails);
    person.setFavourites(returnedObj.getFavs(favsList));

    return person;
}

private Person setComments(Person person, UserDetails userDetails){

    <List>commentsList = callToService3(userDetails);
    person.setComments(returnedObj.getComments(commentsList));

    return person;
}

}

and then in my controller I call

person = personHelper.getPerson(userDetails);
jsonResponse = jsonProcessor.writeAsString(person);

return jsonResponse; // returns the ajax response to js

Thanks in advance for any help or suggestions.

EDIT: After more research I found that the object I am populating is referred to as a Data Transfer Object and I am populating it using the Java Bean method.


Solution

  • There is no general paradigm for your question, but here are a few tips for your design:

    1. It seems that your person data (names, favourites) is distributed among several data stores and you have to gether it all in your PersonHelper class. I don't know if this services are used anywhere else, but from the controller point of view this helper should be a service too.

    2. Since your service invocations are independent, you can execute them in parallel

    3. For some kind of applications it can be even better if you expose these services for UI level. For example, if data is presented in different UI blocks, client can make several asynchronous requests and display the data as soon as responses are received.