Creating a New Custom Object vs Setting Multiple Values to Null of the Same Object in Java

underdog :

So, I've come across this situation

I have a Restful controller method which returns a list of objects Match

@RequestMapping(value = "/fetchFootballMatchesToday", method = RequestMethod.GET)
public @ResponseBody List<Match> fetchMatchesScheduledToday() throws ParseException {
    return matchesToday;
   }

The object Match has say 15 properties & I need only 3 properties on the UI. I do not want the user to see the other properties of the object.

There are two ways to do it:

  1. Run a for loop & explicitly set the properties to null, for every Match object, which are not meant to be sent to the UI. Like so

    List<Match> matchesToday = matchInvService.fetchMatchByDate(today);
    
    for (Match stats : matchesToday) {
     if (stats != null) {
    /* user should not know these details, so they are being set to null */
        stats.setMatchSuccessIndex(null);
        stats.setTeamRanking(null);
        stats.setStadiumInfrastructureLevel(null); 
        ..... & so on
      }
    }
    

The problem with this approach is as the number of properties add up in future. This code needs to be updated too. Also, if the number of properties of an object is large. I need to keep setting null here, that many times.

  1. Second approach is create a new Match object within the loop & just set the 3 required values to it.

    List<Match> matchesToday = matchInvService.fetchMatchByDate(today);
    List<Match> responseList = new ArrayList<Match>();  
    
    for (Match stats : matchesToday) {
     if (stats != null) {
       Match tmpMatch = new Match();
       match.setProperty1(stats.getProperty1());
      }
     responseList.add(tmpMatch); 
    }
    
     return responseList;
    

This approach creates additional Match objects every time the loop runs. Also, the creation of objects spike up if that particular method is called too often. Though the objects will be garbage collected, I ain't sure if this is an optimal way.

Need your suggestion guys. Is this a trade off between writing more code vs saving memory? What would be the best approach to tackle this?

davidxxx :

The object Match has say 15 properties & I need only 3 properties on the UI. I do not want the user to see the other properties of the object.

Setting all fields to null is indeed cumbersome and error prone.
So I would avoid the first way.

The second approach seems to go in a better direction.
But you should not be afraid to map objects to other objects because clients need to see a particular view of them.
Simple mapping operations for a "reasonable" list size is cheap. And I suppose that you will not display millions of rows in the UI, so the size should be reasonable.
Otherwise you should re-think the whole UI design by considering the paging concept.

I would use a third option : create a class declaring the 3 properties the client needs to know and map Match to this class. It makes things much clearer.

List<Match> matchesToday = matchInvService.fetchMatchByDate(today);
List<MatchDTO> responseList = new ArrayList<Match>();  

for (Match stats : matchesToday) {
 if (stats != null) {
    MatchDTO match = new MatchDTO(stats);
    responseList.add(match); 
  } 
}

return responseList;

Where MatchDTO(Match) is a constructor that copies from Match instance the 3 needed fields :

public MatchDTO(Match match){
  this.foo = match.getFoo();
  this.bar = match.getBar();
  this.fooBar = match.getFooBar();
}

or in Java 8 :

List<MatchDTO> responseList =   
    matchInvService.fetchMatchByDate(today)
                   .stream()
                   .filter(Objects::nonNull)
                   .map(MatchDTO::new)
                   .collect(Collectors.toList);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=113590&siteId=1