Arun is a curious programmer who has spent the last 9 years trying to create better and faster programs. When the sun goes down, he experiments with various technologies and programming languages. He blogs at Arun is a DZone MVB and is not an employee of DZone and has posted 24 posts at DZone. You can read more from them at their website. View Full User Profile

Prototype Design Pattern: Mindmap and Implementation

  • submit to reddit

I was going through the Gang of Four again after a long time as part of my college elective and had a very interested experience.

There are some which I misunderstood or realised had few other interesting implementations or forgot they even existed.

Now, comparing and contrasting design patterns is a funny task when all my struggle goes into remembering them.

I have been using mindmaps lately to help organize my thoughts and thought I’ll convert few design patterns into maps. Hope you would find it useful too.

Click to see the full web optimized image. Mail me/Drop a comment for a bigger one or the iMindmap file.



This implementation snippet is from my college CA task. The idea is to read a property file and create new instances of Coin objects. The CashPropertyLoader uses the StoreObjectFactory to clone and initialize the Coin object. Both these methods are available in the Coin object. The StoreObjectFactory also uses a StoreObjectRegistry to cache the prototypes. The StoreObjectRegistry acts as the prototype manager

Class Diagram

Cash Property Loader
 public CashStoreItem getItem(int idx){
      CashStoreItem   cit;

      Map<String,String> params=new HashMap<String, String>();
      String  item, itemv;

      item = new String("Name" + idx);
      itemv = getValue(item);
      params.put("name", itemv);
      item = new String("Weight" + idx);
      itemv = getValue(item);
      params.put("weight", itemv);

      item = new String("Value" + idx);
      itemv = getValue(item);
      params.put("value", itemv);
      Coin coin = (Coin)StoreObjectRegistry.lookup(StoreObjectEnum.COIN);
      System.out.println("ToString" +("Idx :"+idx+ "\nCoin "+coin));

      item = new String("Quantity" + idx);
      itemv = getValue(item);

      cit = new CashStoreItem(coin, Integer.parseInt(itemv));
      //System.out.println("ToString" +("Idx :"+idx+ "\nCashstore item "+cit));
      return cit;
public class StoreObjectRegistry {

  private static Map<StoreObjectEnum, StoreObject> storeObjectRegistry=new HashMap<StoreObjectEnum, StoreObject>();
  public static StoreObject lookup(StoreObjectEnum storeObjecType)  {
      StoreObject storeObject=null;
      try {
          StoreObject storeObjectImpl=null;
              case COIN:
                  storeObjectImpl=new Coin();
              case DRINK:
                  storeObjectImpl=new DrinksBrand();
              if (storeObjectImpl!=null){
                  storeObjectRegistry.put(storeObjecType, storeObjectImpl);  
                  System.err.println("Store Object Implementation could not be resolved. Please check your store object type and store object mapping");
              storeObject =(StoreObject)storeObjectRegistry.get(storeObjecType).clone();
      } catch (CloneNotSupportedException e) {
      return storeObject;
public void initialize(Map<String, String> params){

      ArrayList<Field> fieldsList=new ArrayList<Field>();
      addDeclaredAndInheritedFields(this.getClass(), fieldsList);
      for (Field field : fieldsList) {
          if (params.get(field.getName())==null){
          try {

               if (field.getType() == boolean.class) {
                  field.setBoolean(this, new Boolean(params.get(field.getName())));
              } else if (field.getType() == int.class) {
              } else if (field.getType() == double.class) {
                  field.setDouble(this,Double.parseDouble(params.get(field.getName() )));
              } else {
          } catch (Exception e) {
              System.out.println("Unable to map field : " + field.getName());

    private static void addDeclaredAndInheritedFields(Class c, List<Field> fields) {
        Class superClass = c.getSuperclass();
        if (superClass != null) {
            addDeclaredAndInheritedFields(superClass, fields);

    public Object clone() throws CloneNotSupportedException {
        return super.clone();


Published at DZone with permission of Arun Manivannan, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)