vendredi 27 mars 2015

what pattern should i use with transformers?

i have my transformer abstract class:



public abstract class Transformer<T, S> {

public abstract S transform(T object);

public abstract T revert(S object);

public List<S> transformArray(Collection<T> iterable) {
try {
List<S> transformed = new ArrayList<S>();
Iterator<T> iterator = iterable.iterator();
while (iterator.hasNext()) {
T element = iterator.next();
transformed.add(this.transform(element));
}
return transformed;
} catch (Exception e) {
e.printStackTrace();
return null;
}

}

public List<T> revertArray(Collection<S> iterable) {
try {
List<T> transformed = new ArrayList<>();
Iterator<S> iterator = iterable.iterator();
while (iterator.hasNext()) {
S element = iterator.next();
transformed.add(this.revert(element));
}
return transformed;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

}


And i want to have my DTOTransformer, which is like



public abstract class DTOTransformer<DTOREQ, OTHER, DTORES> {

Transformer<DTOREQ, OTHER> forwardTransformer;
Transformer<OTHER, DTORES> backwardTransformer;

// Not overriding, since isn't and hasn't a transformer
public OTHER transform(DTOREQ object) {
return forwardTransformer.transform(object);
}

// Not overriding, since isn't and hasn't a transformer
public DTORES revert(OTHER object) {
return backwardTransformer.transform(object);
}

}


The problem is that i have not actually any implementation for backwardTransformer and forwardTransformer's transform() method. What i want is that when i create a subclass from DTOTransformer, for example, ClientDTOTransformer then i have to implement the transform and revert methods (which are not the same of the methods that Transformer's class offers since i have a "triangular" transformation).


I have already tried using the adapter pattern but i couldn't implement it.


Do you know how i could do this? Or if you know a better way to do this, i am open to suggestionsRegards!


Aucun commentaire:

Enregistrer un commentaire