Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Prototype 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.

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.

UML for Prototype Pattern

prototype

  • We are going to create an interface Prototype that contains a method getClone() of Prototype type.
  • Then, we create a concrete class EmployeeRecord which implements Prototype interface that does the cloning of EmployeeRecord object.
  • PrototypeDemo class will uses this concrete class EmployeeRecord.

Example of Prototype Design Pattern

Let’s see the example of prototype design pattern.

File: Prototype.java
  1. interface Prototype {
  2.      public Prototype getClone();
  3. }//End of Prototype interface.
File: EmployeeRecord.java
  1. class EmployeeRecord implements Prototype{
  2.    private int id;
  3.    private String name, designation;
  4.    private double salary;
  5.    private String address;
  6.    public EmployeeRecord(){
  7.             System.out.println(”   Employee Records of Oracle Corporation “);
  8.             System.out.println(“———————————————“);
  9.             System.out.println(“Eid”+“\t”+“Ename”+“\t”+“Edesignation”+“\t”+“Esalary”+“\t\t”+“Eaddress”);
  10. }
  11.  public  EmployeeRecord(int id, String name, String designation, double salary, String address) {
  12.         this();
  13.         this.id = id;
  14.         this.name = name;
  15.         this.designation = designation;
  16.         this.salary = salary;
  17.         this.address = address;
  18.     }
  19.   public void showRecord(){
  20.         System.out.println(id+“\t”+name+“\t”+designation+“\t”+salary+“\t”+address);
  21.    }
  22.     @Override
  23.     public Prototype getClone() {
  24.         return new EmployeeRecord(id,name,designation,salary,address);
  25.     }
  26. }//End of EmployeeRecord class.
File: PrototypeDemo.java
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. class PrototypeDemo{
  5.      public static void main(String[] args) throws IOException {
  6.         BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
  7.         System.out.print(“Enter Employee Id: “);
  8.         int eid=Integer.parseInt(br.readLine());
  9.         System.out.print(“\n”);
  10.         System.out.print(“Enter Employee Name: “);
  11.         String ename=br.readLine();
  12.         System.out.print(“\n”);
  13.         System.out.print(“Enter Employee Designation: “);
  14.         String edesignation=br.readLine();
  15.         System.out.print(“\n”);
  16.         System.out.print(“Enter Employee Address: “);
  17.         String eaddress=br.readLine();
  18.         System.out.print(“\n”);
  19.         System.out.print(“Enter Employee Salary: “);
  20.         double esalary= Double.parseDouble(br.readLine());
  21.         System.out.print(“\n”);
  22.         EmployeeRecord e1=new EmployeeRecord(eid,ename,edesignation,esalary,eaddress);
  23.         e1.showRecord();
  24.         System.out.println(“\n”);
  25.         EmployeeRecord e2=(EmployeeRecord) e1.getClone();
  26.         e2.showRecord();
  27.     }
  28. }//End of the ProtoypeDemo class.

download this Prototype Pattern Example

Output

prototypeoutput.jpg

Comments

comments