Creational Patterns : Prototype (Make new objects by cloning the objects which you set as prototypes)
When to use ?
Prototype design is one of the design pattern under creational design pattern. This pattern describes how to clone object. Whenever application doesn’t need to create new object or object creation is more expensive than cloning will help to resolve.
Example:
We will consider Product example where we can clone product with Book and Cloths Classes.Product.java is a abstract java class which implements Cloneable interface. Class ’title’ & ‘price’ and there getter & setter methods and also implemeted clone() method. This method creates and return the copy of the super class.
/**
*
* @author R.Amirtharaj
*/
public abstract class Product implements Cloneable {
private String title;
private String price;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public Object clone() {
Object clone = null;
try {
clone = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
}
Book.java and Cloths.java extends Product and having separate variables author & size which have getter and setter methods in it.
/**
*
* @author R.Amirtharaj
*/
public class Book extends Product {
private String author;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
/**
*
* @author R.Amirtharaj
*/
public class Cloths extends Product {
private int size;
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
ProductFactory.java is the factory where it will produce the products and adding in HashMap based on key as product title and value as product.
/**
*
* @author R.Amirtharaj
*/
public class ProductFactory {
private static Hashtable productMap = new Hashtable();
public static Product getProduct(String productCode) {
Product cachedProduct = (Product) productMap.get(productCode);
return (Product) cachedProduct.clone();
}
public static void loadCache() {
Book book = new Book();
book.setTitle("Java best practices");
book.setAuthor("Amirtharaj");
book.setPrice("1000");
productMap.put(book.getTitle(), book);
Cloths cloths = new Cloths();
cloths.setTitle("Jeans");
cloths.setSize(32);
cloths.setPrice("450");
productMap.put(cloths.getTitle(),cloths);
}
}
Demo.java is product outlet, which will load the cashe from the ProductFactory by calling ProductFactory.loadCache(); and clone the product by getting the product from ProductFactory
/**
*
* @author R.Amirtharaj
*/
public class Demo {
public static void main(String[] args) {
ProductFactory.loadCache();
Book clonedBook = (Book) ProductFactory.getProduct("Java best practices");
System.out.println("Product cloned as Book");
System.out.println("Title = " + clonedBook.getTitle());
System.out.println("Author = " + clonedBook.getAuthor());
System.out.println("Price = " + clonedBook.getPrice());
System.out.println("Product cloned as Cloths");
Cloths clonedCloths = (Cloths) ProductFactory.getProduct("Jeans");
System.out.println("Title = " + clonedCloths.getTitle());
System.out.println("Size = " + clonedCloths.getSize());
System.out.println("Price = " + clonedCloths.getPrice());
}
}
Advantage of prototype design pattern
Adding and removing objects at runtime
Creating cloned object based on the values
It reduced the inheritance of the class (sub classing)
Usage of prototype design pattern
When the classes to instantiate are specified at run time.
When you want to avoid building a class hierarchy of factories that parallels the class hierarchy of products.
When instances of a class can have one of only a few combinations of state.
No comments:
Post a Comment