Prototype Pattern

Prototype Design Pattern

Prototype Pattern says that cloning of an existing object instead of creating new one and can also be customized as per the requirement.
This pattern should be followed, if the cost of creating a new object is expensive and resource intensive.

Advantage of Prototype Pattern

The main advantages of prototype pattern are as follows:
  • It reduces the need of sub-classing.
  • It hides complexities of creating objects.
  • The clients can get new objects without knowing which type of object it will be.
  • It lets you add or remove objects at runtime.
  • Adding and removing products at run-time – Prototypes let you incorporate a new concrete product class into a system simply by registering a prototypical instance with the client. That’s a bit more flexible than other creational patterns, because a client can install and remove prototypes at run-time.
  • Specifying new objects by varying values – Highly dynamic systems let you define new behavior through object composition by specifying values for an object’s variables and not by defining new classes.
  • Specifying new objects by varying structure – Many applications build objects from parts and subparts. For convenience, such applications often let you instantiate complex, user-defined structures to use a specific subcircuit again and again.
  • Reduced subclassing – Factory Method often produces a hierarchy of Creator classes that parallels the product class hierarchy. The Prototype pattern lets you clone a prototype instead of asking a factory method to make a new object. Hence you don’t need a Creator class hierarchy at all.

Disadvantage of Prototype Pattern

  • Overkill for a project that uses very few objects and/or does not have an underlying emphasis on the extension of prototype chains.
  • It also hides concrete product classes from the client
  • Each subclass of Prototype must implement the clone() operation which may be difficult, when the classes under consideration already exist. Also implementing clone() can be difficult when their internals include objects that don’t support copying or have circular references.

Usage of Prototype Pattern

  • When the classes are instantiated at runtime.
  • When the cost of creating an object is expensive or complicated.
  • When you want to keep the number of classes in an application minimum.
  • When the client application needs to be unaware of object creation and representation.
The UML Diagram of the Prototype Design Pattern

// A Java program to demonstrate working of
// Prototype Design Pattern with example 
// of a ColorStore class to store existing objects.
  
import java.util.HashMap;
import java.util.Map;
  
  
abstract class Color implements Cloneable {
      
    protected String colorName;
       
    abstract void addColor();
       
    public Object clone()     {
        Object clone = null;
        try         {
            clone = super.clone();
        
        catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return clone;
    }
}
  
class blueColor extends Color {
    public blueColor()      {
        this.colorName = "blue";
    }
   
    @Override
    void addColor()  {
        System.out.println("Blue color added");
    }
      
}
  
class blackColor extends Color{
   
    public blackColor() {
        this.colorName = "black";
    }
   
    @Override
    void addColor()  {
        System.out.println("Black color added");
    }
}
   
class ColorStore {
   
    private static Map<String, Color> colorMap = new HashMap<String, Color>(); 
       
    static {
        colorMap.put("blue", new blueColor());
        colorMap.put("black", new blackColor());
    }
       
    public static Color getColor(String colorName) {
        return (Color) colorMap.get(colorName).clone();
    }
}
  
  
// Driver class
class Prototype {
    public static void main (String[] args) {
        ColorStore.getColor("blue").addColor();
        ColorStore.getColor("black").addColor();
        ColorStore.getColor("black").addColor();
        ColorStore.getColor("blue").addColor();
    }
}
Output :
Blue color added
Black color added
Black color added
Blue color added

Comments

Popular posts from this blog

gsutil Vs Storage Transfer Service Vs Transfer Appliance

SQL basic interview question