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?

1
  • If all your getters and setters do is get and set (no checking or anything) then just make the variables public. Commented Dec 13, 2012 at 17:57

5 Answers 5

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;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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);

Comments

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:

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.