Feb 16, 2012

Translator pattern

One common task in banking is translating from one form to another. I couldn't find an effectife solution for this problem, but TranslatorPattern offers interesting approach.
I've used similar functionality in Amoeba utility classes:

 private static interface Amoeba<Value> {

  boolean consume(Amoeba<Value> amoeba);

  Value produce() throws Exception;
 }

 private static class AmoebaUtils {

  static <Value> boolean populate(Collection<Amoeba<Value>> colony, Amoeba<Value> nominate) {
   for (Amoeba<Value> citizen : colony) {
    if (citizen.equals(nominate)) {
     return citizen.consume(nominate);
    }
   }
   return colony.add(nominate);
  }

  static <Value> Collection<Value> produce(Set<Amoeba<Value>> colony) throws Exception {
   Set<Value> values = new HashSet<Value>();
   for (Amoeba<Value> citizen : colony) {
    values.add(citizen.produce());
   }

   return values;
  }
 }

This gives ability to absorb source notes by related destination nodes, providing natural grouping and translating of information. This is kind of builder and translator, with non-linear rules of transformation (source node != dest node).
Post a Comment