• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

design patterns

Prototype design pattern

April 6, 2017 By j2eereference Leave a Comment

Prototype design pattern

The idea of prototype pattern is having a blueprint from which we can create multiple instances.

When to use Prototype design pattern

This pattern can be used when creation of object is costly. For example, Consider a class which required configuration data from  a file / database / over a network for initializing. This kind of operations make the object creation very costly especially if you want to create multiple objects of such type. The best solution for this problem is to use the Prototype design pattern – Create the object once and create the subsequent objects by cloning this object.

What are the benefits of using prototype design patterns

Prototype pattern helps us mainly in Time and Memory saving.

  • Time :

Creating the object once and create the subsequent objects by cloning this object helps reducing the time for creating the objects.

  • Memory :

Consider an object which has more number of String objects, creating a new instance would need to create entirely new immutable strings . By Using the prototype pattern we can avoid this problem by cloning the already created object.

Implementation of prototype pattern:

Lets understand the implementation of prototype design pattern with a simple example. This example is just for understanding the concept. The benefit you are going to get by using the prototype design pattern in real time applications will be more than this example.

Here we have a Box class and we need to create multiple instances of the BOX class. These objects are different only in colors.

Box.java  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.j2eereference.designPattern.prototype;
 
public class Box implements Cloneable {
private int width;
private int length;
private int depth;
String color;
 
Box(int width,int length,int depth){
this.width=width;
this.length=length;
this.depth=depth;
}
 
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getLenght() {
return length;
}
public void setLenght(int lenght) {
this.length = lenght;
}
public int getDeptht() {
return depth;
}
public void setDeptht(int deptht) {
this.depth = deptht;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Box clone() throws CloneNotSupportedException {
Box cloned = (Box)super.clone();
return cloned;
}
@Override
public String toString() {
return "Width :"+width+" Length : "+length+" Color : "+color;
}
}

PrototypeDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.j2eereference.designPattern.prototype;
 
public class PrototypeDemo {
public static void main(String[] args) {
Box box=new Box(10,10, 10);
try {
Box redBox=box.clone();
redBox.setColor("RED");
 
Box greenBox=box.clone();
greenBox.setColor("GREEN");
 
System.out.println(redBox);
System.out.println(greenBox);
 
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
 
}

 Output:

Width :10 Length : 10 Color : RED

Width :10 Length : 10 Color : GREEN

Here you can notice that we created the blueprint of Box and did color property settings for creating different objects. This pattern will be more useful if the initialization is costly.

Related Posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Difference between Stack and Heap memory
  • What is ThreadMXBean in java?
  • What is serialVersionUID
  • What is exchanger in concurrency?

Filed Under: Core Java Tagged With: design patterns, java design pattern, prototype design pattern

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.