A prototype is a template of any
object before the actual object is constructed. In java also, it holds the same
meaning. Prototype design pattern is used in scenarios where application needs
to create a number of instances of a class, which has almost same state or
differs very little.
In this design pattern, an instance of
actual object (i.e. prototype) is created on starting, and thereafter whenever
a new instance is required, this prototype is cloned to have another instance.
The main advantage of this pattern is to have minimal instance creation process
which is much costly than cloning process.
Problem Statement
Let’s understand this pattern using an
example. I am creating an entertainment application that will require instances
of Movie, Album and Show classes very frequently. I do not want to create their
instances every time as it is costly. So, I will create their prototype
instances, and every time when i will need a new instance, I will just clone
the prototype.
Implementation of Prototype Design Pattern
Above class diagram explains the
necessary classes and their relationship.
Only one interface, “PrototypeCapable”
is new addition in solution. The reason to use this interface is broken
behavior of Cloneable interface. This interface helps in achieving following
goals:
1. Ability to clone
prototypes without knowing their actual types
2. Provides a type
reference to be used in registry
Their workflow will look like this:
PrototypeCapable.java
public interface PrototypeCapable extends Cloneable
{
public PrototypeCapable clone() throws CloneNotSupportedException;
}
|
Movie.java,
Album.java and Show.java
public class Movie implements PrototypeCapable
{
private String name = null;
public String getName() {
return name;
}
public void setName(String
name) {
this.name
= name;
}
@Override
public Movie clone() throws CloneNotSupportedException {
System.out.println("Cloning
Movie object..");
return (Movie) super.clone();
}
@Override
public String toString() {
return "Movie";
}
}
public class Album implements PrototypeCapable
{
private String name = null;
public String getName() {
return name;
}
public void setName(String
name) {
this.name
= name;
}
@Override
public Album clone() throws CloneNotSupportedException {
System.out.println("Cloning
Album object..");
return (Album) super.clone();
}
@Override
public String toString() {
return "Album";
}
}
public class Show implements PrototypeCapable
{
private String name = null;
public String getName() {
return name;
}
public void setName(String
name) {
this.name
= name;
}
@Override
public Show clone() throws CloneNotSupportedException {
System.out.println("Cloning
Show object..");
return (Show) super.clone();
}
@Override
public String toString() {
return "Show";
}
}
|
PrototypeFactory.java
public class PrototypeFactory
{
public static class ModelType
{
public static final String MOVIE = "movie";
public static final String ALBUM = "album";
public static final String SHOW = "show";
}
private static java.util.Map<String , PrototypeCapable> prototypes = new java.util.HashMap<String ,
PrototypeCapable>();
static
{
prototypes.put(ModelType.MOVIE,
new Movie());
prototypes.put(ModelType.ALBUM,
new Album());
prototypes.put(ModelType.SHOW,
new Show());
}
public static PrototypeCapable getInstance(final String s) throws CloneNotSupportedException {
return ((PrototypeCapable) prototypes.get(s)).clone();
}
}
|
TestPrototypePattern
public class TestPrototypePattern
{
public static void main(String[] args)
{
try
{
String
moviePrototype = PrototypeFactory.getInstance(ModelType.MOVIE).toString();
System.out.println(moviePrototype);
String
albumPrototype =
PrototypeFactory.getInstance(ModelType.ALBUM).toString();
System.out.println(albumPrototype);
String
showPrototype = PrototypeFactory.getInstance(ModelType.SHOW).toString();
System.out.println(showPrototype);
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
}
}
|
When you run the client code, following
is the output.
Cloning Movie object..
Movie
Cloning Album object..
Album
Cloning Show object..
Show
[Design Patterns Interview Questions]
We recommend you take Big Data Hadoop class room training at eMexo Technologies in electronic city, Bangalore to learn more about Big Data Hadoop.
0 Comments:
Post a Comment