CHAPTER 19

image

Prototype Patterns

GoF Definition: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

Concept

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.

Real-Life Example

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.

Computer World Example

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.

Illustration

In my example, I am going to follow the structure shown here:

9781484218013_unFig19-01.jpg

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.

UML Class Diagram

9781484218013_unFig19-02.jpg

Package Explorer view

High-level structure of the parts of the program is as follows:

9781484218013_unFig19-03.jpg

Implementation

//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);

    }
}

Output

9781484218013_unFig19-04.jpg

Note

  1. When the system does not care about the creational mechanism of the products, this pattern is very helpful.
  2. We can use this pattern when we need to instantiate classes at runtime.
  3. In our example, we have used the default clone() method in Java, which is a shallow copy. Thus, it is inexpensive compared to a deep copy.

What are the advantages of the prototype pattern?

  1. We can include or discard products at runtime.
  2. We can create new instances with a cheaper cost.

What are the disadvantages of the prototype pattern?

  1. Each subclass has to implement the cloning mechanism.
  2. Implementing the cloning mechanism can be challenging if the objects under consideration do not support copying or if there is any kind of circular reference.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.32.67