1

This is not the first time that I've found myself in a situation in which I have to adapt two objects with almost the same data, for example:

User.java  (Object returned from another library)
private String name;
private String surname;
private String email;
private String telephone;
...
getters and setters();
constructor();

MyUser.java
private String name;
private String surname;
private String email;
private String telephone;
...
getters and setters();
constructor();

I usually create a method to convert one object into another one, like this:

 User m1 = new User();
 MyUser m2 = new MyUser();
 m2.setName(m1.getName());
 m2.setsurmame(m1.getsurname());

...and so on...

Does anybody know a different way to do this kind of stuff?

cadrell0
  • 17,109
  • 5
  • 51
  • 69
migueloop
  • 525
  • 9
  • 21
  • If all your getters and setters do is get and set (no checking or anything) then just make the variables public. – tckmn Dec 13 '12 at 17:57

5 Answers5

1

There is a AutoMapper project in C Sharp.

In the gist of it it provides an easy way of mapping properties from a source instance to a destination instance where the source and destination instances can be of different classes.

this link shares some interesting thoughts about similar projects in Java : Automapper for Java

Community
  • 1
  • 1
frictionlesspulley
  • 11,070
  • 14
  • 66
  • 115
1

One thing you can do is pass that User object in a method of MyUser class or constructor of MyUser class and then perform those setters.

Using constructor :

public MyUser(User u){
    setName(u.getName());
    setSurname(u.getSurname());
    ...
}

Or creating a seperate method :

public void setMyUser(User u){
   setName(u.getName());
   setSurname(u.getSurname());
   ...
}

Then you can use it like this:

User u = new User();
//hope all values are set in User u object
MyUser m = new MyUser(u);
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
1

Use Object Composition For objects that you create using the other library, create an instance of ExternalUser. But if you want to create them locally, create a BrandNewUser. Then you can just treat them the same way, with one version using the pass-through composition methods, and the ones created by your code using your own internal implementation.

You can create your object like this:

public interface MyUser {
  // all the methods you need
  String getSurname();
}

public class ExternalUser implements MyUser {
  private User _user;
  private ExternalUser() { }
  public ExternalUser(User u) {
    this._user = u;
  }
  public String getSurname() {
    return _user.getSurname();
  }
}

public class BrandNewUser implements MyUser {
  private String _surname;
  public ExternalUser(String name, String surname) {
    this._surname = surname;
  }
  public String getSurname() {
    return _surname;
  }
}
durron597
  • 31,968
  • 17
  • 99
  • 158
1

In cases where appropriate, refactor those objects to inherit from each other, rather than duplicate properties and logic.

In cases where the objects must remain distint, you can use any one of a variety of clone tools to perform deep copies from object to object. Here is a decent, non-exhaustive list:

Perception
  • 79,279
  • 19
  • 185
  • 195
0

Maybe you can use beanutils which provides copy properties function. http://commons.apache.org/beanutils/