GoF Definition: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
The prototype pattern provides an alternative method for instantiating new objects by copying or cloning an instance of an existing one. Creating a new instance, in a real-world scenario, is normally treated as an expensive operation. This pattern helps us to deal with this issue. Our focus here is to reduce the expense of this creational process of a new instance.
Suppose we have a master copy of a valuable document. We want to make some change to it to get a different feel. We can make a photocopy of this document and then try to edit our changes.
Suppose we have made an application. The next time we want to create a similar application with some small changes, we must start with a copy from our master copy application and make the changes. We’ll not start from the scratch.
In my example, I am going to follow the structure shown here:
Here BasicCar is our prototype. Nano and Ford are our Concrete Prototypes and they need to implement the Clone() method defined in BasicCar. Here we notice that a BasicCar model is created with some default price. Later we have modified that price as per the model. Please also note that PrototypePatternEx is the client here. As usual, the related parts are separated by the packages for better readability.
High-level structure of the parts of the program is as follows:
//BasicCar.java
package car;
import java.util.Random;
public abstract class BasicCar implements Cloneable
{
public String modelname;
public int price;
public String getModelname()
{
return modelname;
}
public void setModelname(String modelname)
{
this.modelname = modelname;
}
public static int setPrice()
{
int price = 0;
Random r = new Random();
int p = r.nextInt(100000);
price = p;
return price;
}
public BasicCar clone() throws CloneNotSupportedException
{
return (BasicCar)super.clone();
}
}
//Ford.java
package car;
public class Ford extends BasicCar
{
public Ford(String m)
{
modelname = m;
}
@Override
public BasicCar clone() throws CloneNotSupportedException
{
return (Ford)super.clone();
}
}
//Nano.java
package car;
public class Nano extends BasicCar
{
public Nano(String m)
{
modelname = m;
}
@Override
public BasicCar clone() throws CloneNotSupportedException
{
return (Nano)super.clone();
}
}
//PrototypePatternEx.java
package prototype.pattern.demo;
import car.*;
public class PrototypePatternEx
{
public static void main(String[] args) throws CloneNotSupportedException
{
System.out.println("***Prototype Pattern Demo*** ");
BasicCar nano_base = new Nano("Green Nano");
nano_base.price=100000;
BasicCar ford_basic = new Ford("Ford Yellow");
ford_basic.price=500000;
BasicCar bc1;
//Clone Nano Object
bc1 =nano_base.clone();
//Price will be more than 100000 for sure
bc1.price = nano_base.price+BasicCar.setPrice();
System.out.println("Car is: "+ bc1.modelname+" and it’s price is Rs."+bc1.price);
//Clone Ford Object
bc1 =ford_basic.clone();
//Price will be more than 500000 for sure
bc1.price = ford_basic.price+BasicCar.setPrice();
System.out.println("Car is: "+ bc1.modelname+" and it’s price is Rs."+bc1.price);
}
}
What are the advantages of the prototype pattern?
What are the disadvantages of the prototype pattern?
18.222.32.67