We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 23 July 2017

Design Patterns - Prototype Pattern

Prototype Pattern

Definition

The Prototype pattern is basically the creation of new instances through cloning existing instances. By creating a prototype, new objects are created by copying this prototype.

Where to use

•When a system needs to be independent of how its objects are created, composed, and represented.
•When adding and removing objects at runtime.
•When specifying new objects by changing an existing objects structure.
•When configuring an application with classes dynamically.
•When keeping trying to keep the number of classes in a system to a minimum.
•When state population is an expensive or exclusive process.

Benefits

•Speeds up instantiation of large, dynamically loaded classes.
•Reduced subclassing.

Drawbacks/consequences

Each subclass of Prototype must implement the Clone operation. Could be difficult with existing classes with internal objects with circular references or which does not support copying.

Prototype Pattern class-diagram


In the class-diagram above:
•Prototype declares an interface for cloning itself.
•ConcretePrototype implements an operation for cloning itself.
•Client creates a new object by asking a prototype to clone itself.

You could use a PrototypeManager to keep track on the different types of prototypes. The PrototypeManager maintains a list of clone types and their keys. The client, instead of writing code that invokes the "new" operator on a hard-wired class name, calls the clone() method on the prototype.

Prototype Pattern example


import java.util.Hashtable;
public class PrototypeExample {
Hashtable productMap = new Hashtable();
public Product getProduct(String productCode) {
Product cachedProduct
=(Product)productMap.get(productCode);
return (Product)cachedProduct.clone();
}
public void loadCache() {
//for each product run expensive query and instantiate
// product productMap.put(productKey, product);
// for exemplification, we add only two products
Book b1 = new Book();
b1.setDescription("Oliver Twist");
b1.setSKU("B1");
b1.setNumberOfPages(100);
productMap.put(b1.getSKU(), b1);
DVD d1 = new DVD();
d1.setDescription("Superman");
d1.setSKU("D1");
d1.setDuration(180);
productMap.put(d1.getSKU(), d1);
}
public static void main(String[] args) {
PrototypeExample pe = new PrototypeExample();
pe.loadCache();
Book clonedBook = (Book)pe.getProduct("B1");
System.out.println("SKU = " + clonedBook.getSKU());
System.out.println("SKU = " +
clonedBook.getDescription());
System.out.println("SKU = " +
clonedBook.getNumberOfPages());
DVD clonedDVD = (DVD)pe.getProduct("D1");
System.out.println("SKU = " + clonedDVD.getSKU());
System.out.println("SKU = " + clonedDVD.getDescription());
System.out.println("SKU = " + clonedDVD.getDuration());
}
}

/** Prototype Class * */
public abstract class Product implements Cloneable {
private String SKU;
private String description;
public Object clone() {
Object clone = null;
try {
clone = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
public String getDescription() {
return description;
}
public String getSKU() {
return SKU;
}
public void setDescription(String string) {
description = string;
}
public void setSKU(String string) {
SKU = string;
}
}
/** Concrete Prototypes to clone * */
public class Book extends Product {
private int numberOfPages;
public int getNumberOfPages() {
return numberOfPages;
}
public void setNumberOfPages(int i) {
numberOfPages = i;
}
}
/** Concrete Prototypes to clone * */
public class DVD extends Product {
private int duration;
public int getDuration() {
return duration;
}
public void setDuration(int i) {
duration = i;
}
}
When PrototypeExample is executed the result is:
c:>SKU = B1
c:>SKU = Oliver Twist
c:>SKU = 100
c:>SKU = D1
c:>SKU = Superman
c:>SKU = 180

Usage example

If you are designing a system for performing bank account transactions, then you would want to make a copy of the Object which holds your account information, perform transactions on it, and then replace the original Object with the modified one. In such cases, you would want to use clone() instead
of new.

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Design Patterns - Prototype Pattern Rating: 5 Reviewed By: eHowToNow