JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Prototype Pattern
  • prototype pattern is a creational design pattern used in software development when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. This pattern is used to:
  • Aavoid subclasses of an object creator in the client application, like the abstract factory pattern does.
  • Avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is prohibitively expensive for a given application.
  • To implement the pattern, declare an abstract
    base class that specifies a pure virtual clone() method
Purpose
  • Specify the kinds of objects to create using a prototypical instance, and create new objects
    by copying this prototype
Application

  • When the classes to instantiate are specified at run-time, for example, by dynamic loading.
  • 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 different combinations of state. It may be more convenient to
  • Install a corresponding number of prototypes and clone them rather than instantiating the class manually, each time with the appropriate state

Examples of Visitor

  • The Prototype pattern specifies the kind of objects to create using a prototypical instance. Prototypes of new products are often built prior to full
    production, but in this example, the prototype is passive and does not
    participate in copying itself.
  •  The mitotic division of a cell - resulting in two identical cells - is an example of a prototype that plays an active role in copying itself and thus, demonstrates the Prototype pattern.
  •  When a cell splits, two cells of identical genotvpe result. In other words, the cell clones itself

Example:-

Product.java
package com.cloneables;

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;
  }
}

Book.java
package com.cloneables;

public class Book extends Product {
  private int numberOfPages;

  public int getNumberOfPages() {
    return numberOfPages;
  }

  public void setNumberOfPages(int i) {
    numberOfPages = i;
  }
}

DVD.java
package com.cloneables;

public class DVD extends Product {
  private int duration;

  public int getDuration() {
    return duration;
  }

  public void setDuration(int i) {
    duration = i;
  }
}

ProductCache.java
package com.cloneFactory;

import java.util.*;

import com.cloneables.Book;
import com.cloneables.DVD;
import com.cloneables.Product;

public class ProductCache {
  private static Hashtable <String,Product>productMap =
 
new Hashtable<String,Product>();

  public static Product getProduct(String productCode) {
    Product cachedProduct = productMap.get(productCode);
    return (ProductcachedProduct.clone();
  }

  public static 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);
    System.out.println(b1.hashCode());
    productMap.put(b1.getSKU(), b1);
    DVD d1 = new DVD();
    d1.setDescription("Superman");
    d1.setSKU("D1");
    d1.setDuration(180);
    System.out.println(d1.hashCode());
    productMap.put(d1.getSKU(), d1);
  }
}

TestPrototype.java
package com.test;

import com.cloneFactory.ProductCache;
import com.cloneables.Book;
import com.cloneables.DVD;

public class TestPrototype {
  public static void main(String[] args) {
    ProductCache.loadCache();

    Book clonedBook = (BookProductCache.getProduct("B1");
    System.out.println("SKU = " + clonedBook.getSKU());
    System.out.println("Description = " + clonedBook.
getDescription
());
    System.out.println("NoOfPages = " + clonedBook.
getNumberOfPages
());
    System.out.println(clonedBook.hashCode());
    DVD clonedDVD = (DVDProductCache.getProduct
("D1");
    System.out.println("SKU = " + clonedDVD.getSKU());
    System.out.println("Description = " + clonedDVD
.getDescription
());
    System.out.println("Duration = " + clonedDVD.
getDuration
());
    System.out.println(clonedDVD.hashCode());
  }
}

Output:-


Download Zip code

Example:-

Shape.java
package com.model;

public abstract class Shape {
public abstract void draw();
}

Square.java
package com.model;

public class Square extends Shape {
  public void draw(){
    System.out.println("Draw Square");
  }
}

Circle.java
package com.model;

public class Circle extends Shape{
  public void draw(){
    System.out.println("Draw Circle");
  }
}

Line.java
package com.model;

public class Line extends Shape{
public void draw(){
  System.out.println("Draw Line");
}
}

Painting.java
package com.model;

public class Painting {

  
  public static void main(String[] args) {
  Shape s1=new Line();
  Shape s2=new Square();
  Shape s3=new Circle();
    paint(s1);
    paint(s2);
    paint(s3);
  }
  public static void paint(Shape s){
    s.draw();
  }

}

Output:-



Download Zip Code