Monday, March 3, 2014

Going Technical , Prototype pattern

Well this is the year of 2014 and I have not made a single post yet for this year. Frankly I did not have a good real life example for explaining the "Prototype" design pattern. Yes I know there is a very famous example of cells and the way those get divided could be used to explain this pattern. Hey tell you what , it has been overly used by many and I really wanted a completely new example.

Now , you must be thinking what analogy I have found for this. Let me tell you. Nowadays there is this news of "3D printing". I hope you guys have heard of this novel way of printing three dimensional object using a printer capable of doing it. Even NASA has used it in its' space program recently.This simple concept can be helpful in understanding the pattern "Prototype".

Assuming that I have a 3D printer there is a chance that I can print a three dimensional version of the very same printer effectively cloning it (this cloning is loosely referring the shape of the printer and the look. We may not be able to print a completely working version at one shot ). So the printer knows how to produce something exactly like the printer itself. As a client or the user all I have to do is to buy a 3D printer and press the "print" button on the printer after connecting to the power. Every time when I press the print button , it creates a three dimensional object exactly like itself. Not only that I do not have to spend the money and the time that may have been spent by the original manufacturer who manufactured the printer. In addition to that every time when my 3D printer prints an object which resembles itself I have the privilege of painting that with a color that I want. Well , all the description I made is exactly what we get in the case of Prototype pattern. Let me tell you all that in a more computer language specific way

So my 3D printer analogues to the prototype itself , the button which exposes the printing functionality analogues to the interface contract that I have to follow , the objects that I print with the 3D printer which are similar to the printer itself are the objects that could have been expensive to buy If I had attempted to buy them outside from a manufacturer and the ability to color the objects that I produced with my 3D printer can be seen as configuring cloned object in the way that a program requires.

I hope this explanation gives you a new way to think about the Prototype pattern. Let me write some code then,

public interface Cloneable{
   public Object clone();
}

public interface Printable{
    public Object print();
}

public class THREE_D_Printer implements Printable , Cloneable{

    public Object print(){
        THREE_D_Printer aCloneOfPrinter = (THREE_D_Printer)this.clone();
        //once the object is cloned it is natural to have that cloned object configured
        //in the way that is required
        Object config = new Object();
        aCloneOfPrinter.configure(config);
        return aCloneOfPrinter;
    }
    public Object clone(){
        //you either copy this object and change its' specific configuration
        //to suite to the one you need. Usually when initializing this object
        //it may be very expensive in terms of resources. So instead of simply
        //creating new objects by calling "new" operator on this the object
        //may be copied to another and configure the way it is needed
    }

   private void configure(Object cfg){
       //do configuration specific to your need
    }

}



//This is going to be the buyer class , it represents me buying the printer from a store
public class Buyer{
    public THREE_D_Printer buyPrinter(){
        return new THREE_D_Printer();
    }
}
   //main method
   ....
   public static void main(String[] args){
        Buyer buyer = new Buyer();
        THREE_D_Printer iiiDPrinter = buyer.buy();
        //use this iiiDPrinter as the prototype for creating other
        //printer objects with different colors or configurations
        THREE_D_Printer clonedPrinter = iiiDPrinter.print();
        //use iiiDPrinter instance to make any number of cloned objects
   }
   ....

No comments: