samedi 29 octobre 2016

Design Pattern to transform object to detailed version

Lets say I have an object Foo and I wish to transform it to a FooDetailed object. In order to do so, I have to augment certain aspects of Foo to have more detail. The details are fetched from a helper class. See below for an example:

public class Foo {
   String name;
   IBar bar;
}

public class ExtendedFoo {
   String name;
   IBar bar; // this has to be an extendedBar implementation.
}



public interface IIBar {
   public int getId();

   default int getType()
   { 
      return null;
   }
}

public class Bar implements IBar {
   public int getId();
}

public class ExtendedBar implements IBar {
   Bar bar;

   public int getId() { 
      bar.getId()); 
   }

   public int getType(){};
}

public static Helper {
   public static ExtendedBar convert(Bar bar) { 
      // makes some db calls to get type info to create an Extended Bar
   }
}

Is there a design pattern or some Builder esque approach to create a FooConverter that internall has all the info to convert a Foo->ExtendedFoo using the Helper etc where I can just do the following:

List<Foo> foos;

List<ExtendedFoo> extendedFoos = FooConverter.get().process(foos).transform();

Keeping in mind, that in the future, I may add to Foo other simple -> detailed transformations within Foo like a IBizz with a SimpleBiz and ExtendedBiz implementations

Aucun commentaire:

Enregistrer un commentaire