Reputation: 308
I'm having a special case where I have implemented builder pattern. One of the variable is of type ExternalClassA, Where as the parameter passed to builder is of type InternalClassA. Currently I have used a method something like below:
Class Parent {
String variableX;
int variableY;
ExternalZ variableZ;
public static builder {
....
ExternalZ varz;
/* .. builder logic */
private builder withExternalZ(InternalZ internalZ) { //This is the transform method I'm using currently
this.variableZ.getSomeObject().setX(this.internalZ.getSomeObject().getX()); //Similar kind of lines making my transform method look ugly
}
}
Is there any way to make my withExternalZ method look simpler? Currently I'm thinking either to implement Mapper pattern or Java8 Function (As explained in http://www.leveluplunch.com/java/tutorials/016-transform-object-class-into-another-type-java8/). Mapper seems not a good choice as I only need one way conversion.
If anyone has better and simpler approach, kindly share.
Upvotes: 1
Views: 560
Reputation: 1375
Currently you are passing an InternalZ instance to withExternalZ() method which actually needs\expects an ExternalZ object.But what you have in hand is an InternalZ method.
Adapter pattern is built for such scenarios as it adapts/converts the target interface(InternalZ) to the client expected one (ExternalZ). In short you need to write an adapter which converts from InternalZ to ExternalZ object. Adapter class code will roughly be along the following lines -
public class IntToExtZAdapter extends ExternalZ{
InternalZ internalZ=null;
public IntToExtZAdapter(InternalZ internalZ){
this.internalZ=internalZ;
}
public X getX(){
//Logic for getting X from internalZ object in adapter
}
public setX(X x){
//Logic for setting X in internalZ object in adapter
}
//...and so on
}
Then instead of doing this -
private builder withExternalZ(InternalZ internalZ) { //This is the transform method I'm using currently
this.variableZ.getSomeObject().setX(this.internalZ.getSomeObject().getX()); //Similar kind of lines making my transform method look ugly
}
What you now do is -
this.variableZ=new IntToExtZAdapter (internalZ);
Now you can use variableZ as it is and internally IntToExtZAdapter will adapt/convert from InternalZ to ExternalZ and back.
All the "ugly" logic of converting back and forth from InternalZ to ExternalZ and vice-a-versa is now "hidden" inside the adapter.
Upvotes: 1
Reputation: 379
What do you mean when you say mapper? which design pattern is that? The data mapper I'm familiar with are explicitly used for persistent storage as described by Martin fowler.
Anyways I think you can use a variation of the Decorator pattern without adding functionality. Assuming you can derive from the ExternalZ and override the getters you can create a class which wraps the InternalZ class.
class ExternalZOverInternalZ extends ExternalZ {
private final InternalZ internalZ;
ExternalZOverInternalZ(InternalZ internalZ) {
this.interalZ = internalZ;
}
@Override
public int getX() {
return internalZ.getX();
}
}
Upvotes: 1
Reputation: 2468
If you have the possibility to add dependencies to your project, guava from google is a good option, it provides Functions (like Java 8) in older versions of Java
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Function.html
You can add it to maven with
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
Upvotes: 0