Prototype Pattern
Author: Deron Eriksson
Description: This Java tutorial describes the prototype pattern, a creational design pattern.
Tutorial created using: Windows Vista || JDK 1.6.0_11 || Eclipse JEE Ganymede SR1 (Eclipse 3.4.1)


The prototype pattern is a creational design pattern. In the prototype pattern, a new object is created by cloning an existing object. In JavaSW, the clone() method is an implementation of this design pattern. The prototype pattern can be a useful way of creating copies of objects. One example of how this can be useful is if an original object is created with a resource such as a data stream that may not be available at the time that a clone of the object is needed. Another example is if the original object creation involves a significant time commitment, such as reading data from a databaseW or over a network. An added benefit of the prototype pattern is that it can reduce class proliferation in a project by avoiding factory proliferation.

Normally in Java, if you'd like to use cloning (ie, the prototype pattern), you can utilize the clone() method and the Cloneable interface. By default, clone() performs a shallow copy. In another tutorial, we demonstrate how Serializable can be used to simplify deep copying.

However, we can implement our own prototype pattern. To do so, we'll create a Prototype interface that features a doClone() method.

Prototype.java

package com.cakes;

public interface Prototype {

	public Prototype doClone();

}

The Person class implements the doClone() method. This method creates a new Person object and clones the name field. It returns the newly cloned Person object.

Person.java

package com.cakes;

public class Person implements Prototype {

	String name;

	public Person(String name) {
		this.name = name;
	}

	@Override
	public Prototype doClone() {
		return new Person(name);
	}

	public String toString() {
		return "This person is named " + name;
	}
}

The Dog class also implements the doClone() method. This method creates a new Dog object and clones the sound field. The cloned Dog object is returned.

Dog.java

package com.cakes;

public class Dog implements Prototype {

	String sound;

	public Dog(String sound) {
		this.sound = sound;
	}

	@Override
	public Prototype doClone() {
		return new Dog(sound);
	}

	public String toString() {
		return "This dog says " + sound;
	}
}

The Demo class creates a Person object and then clones it to a second Person object. It then creates a Dog object and clones it to a second Dog object.

Demo.java

package com.cakes;

public class Demo {

	public static void main(String[] args) {

		Person person1 = new Person("Fred");
		System.out.println("person 1:" + person1);
		Person person2 = (Person) person1.doClone();
		System.out.println("person 2:" + person2);

		Dog dog1 = new Dog("Wooof!");
		System.out.println("dog 1:" + dog1);
		Dog dog2 = (Dog) dog1.doClone();
		System.out.println("dog 2:" + dog2);

	}

}

The console output of executing Demo is shown here.

Console Output

person 1:This person is named Fred
person 2:This person is named Fred
dog 1:This dog says Wooof!
dog 2:This dog says Wooof!


Related Tutorials: