jeudi 27 septembre 2018

Java: How cast an Object to another Object with the same attributes using design pattern? without libraries

I´ve two POJO´s in Java, Movie to use in my database and other Movie to use like result from request to webservice.

package .database
public class Movie{
 private String title;
 private String year;
 private String poster
}

package .service
public class Movie{
  private String title;
  private String year;
  private String poster;
}

I´ve solve this, create a class that do the cast.

 public class MovieObjectAdapter {
    public static List<service.Movie> castFrom(List<database.Movie>moviesDatabase){
        List<service.Movie> moviesModel = new ArrayList<>();
        for (database.Movie movie:
             moviesDatabase) {
            service.Movie movieModel = new service.Movie();
            movieModel.setTitle(movie.getTitle());
            movieModel.setPoster(movie.getPoster());
            movieModel.setYear(movie.getYear());
            moviesModel.add(movieModel);
        }
        return moviesModel;
    }
}

But i´m not very happy with this. So Which design pattern can you recommend me to use?

Aucun commentaire:

Enregistrer un commentaire