Prototype Design Pattern Tutorial

Prototype Design Pattern TutorialWelcome to my Prototype Design Pattern Tutorial. The Prototype design pattern is used for creating new objects (instances) by cloning (copying) other objects.

It allows for the adding of any subclass instance of a known super class at run time. It is used when there are numerous potential classes that you want to only use if needed at runtime. The major benefit of using the Prototype pattern is that it reduces the need for creating potentially unneeded subclasses.

All of the code follows the video to help you learn.

If you like videos like this, please tell Google [googleplusone]

Sharing is super great

Code from the Video


// By making this class cloneable you are telling Java
// that it is ok to copy instances of this class
// These instance copies have different results when
// System.identityHashCode(System.identityHashCode(bike))
// is called 

public interface Animal extends Cloneable {
	public Animal makeCopy();


public class Sheep implements Animal {

	public Sheep(){
		System.out.println("Sheep is Made");
	public Animal makeCopy() {
		System.out.println("Sheep is Being Made");
		Sheep sheepObject = null;
		try {
			// Calls the Animal super classes clone()
			// Then casts the results to Sheep
			sheepObject = (Sheep) super.clone();
		// If Animal didn't extend Cloneable this error 
		// is thrown
		catch (CloneNotSupportedException e) {
			System.out.println("The Sheep was Turned to Mush");
		return sheepObject;
	public String toString(){
		return "Dolly is my Hero, Baaaaa";


public class CloneFactory {
	// Receives any Animal, or Animal subclass and
	// makes a copy of it and stores it in its own
	// location in memory
	// CloneFactory has no idea what these objects are
	// except that they are subclasses of Animal
	public Animal getClone(Animal animalSample) {
		// Because of Polymorphism the Sheeps makeCopy()
		// is called here instead of Animals
		return animalSample.makeCopy();


public class TestCloning {
	public static void main(String[] args){
		// Handles routing makeCopy method calls to the 
		// right subclasses of Animal
		CloneFactory animalMaker = new CloneFactory();
		// Creates a new Sheep instance
		Sheep sally = new Sheep();
		// Creates a clone of Sally and stores it in its own
		// memory location
		Sheep clonedSheep = (Sheep) animalMaker.getClone(sally);
		// These are exact copies of each other
		System.out.println("Sally HashCode: " + System.identityHashCode(System.identityHashCode(sally)));
		System.out.println("Clone HashCode: " + System.identityHashCode(System.identityHashCode(clonedSheep)));

20 Responses to “Prototype Design Pattern Tutorial”

  1. Shriram says:

    I just came across your design pattern tutorials.You have explained the concepts in a simple manner and its amazing..!!!

    Looking forward to view and learn all your videos….

    Great JOB..

    • admin says:

      Thank you very much 🙂 I did my best to make them easy to understand. I’ll dive into using them and recognizing when they can help in my next tutorial on refactoring. Thank you for taking the time to tell me you liked them

  2. Arun says:

    I really appreciate the way you have explained the java design pattern. It clears all my doubts.

    Thank you again for your awesome description..:)

  3. neha says:

    Very good, clean and clear tutorial. Best part is the availability of the code to be practiced by the learner.
    If possible, if you include class diagram then it would be perfect.

  4. rotem says:

    Realy helpful!! thank you..(:

  5. Helton says:

    Hi, Derek!

    Your tutorials are awesome! Keep the good work.
    I’ve a suggestion…
    In the classes Dog and Sheep you can return Dog and Sheep types (respectively) in makeCopy method, because they’re subtypes of Animal:

    // file
    public Dog makeCopy() {

    // file
    public Sheep makeCopy() {

    So, you’ll avoid cast to this types later, but you won’t able to use CloneFactory without do the cast. It’ll seems like that:

    Sheep clonedSheep = sally.makeCopy();


  6. Inderjeet says:

    Hi Derek,
    I never thought in my life that design pattern could be learned in such an easy way. I would really thanks from my bottom of my heart. The way you are explaining the stuff is exceptional. I became your great friend… One small request please upload few core java related stuff such as: collections, Java memory management , Multi Threads , Syncronization. Thanks again and god bless you…

    • Derek Banas says:

      Thank you 🙂 I’m very happy that they have helped. I plan on going back and covering all of the topics you have mentioned and much more. I’m sorry it is taking so long. May God bless you and your family as well

  7. Ryan says:

    I would like to ask an other question , What about making our object’s constructors private to prevent direct instantiation ?

  8. Matias says:

    Not only the explanations are great, but also the quality of the videos. Congrats and thanks for such a hard and great work!

  9. Apil Tamang says:

    I appreciate the videos as they seem to be gentle introductions to design patterns, but a little bit of explanation on how they improves quality would be very nice. For instance, with this video, why go through the trouble of creating a factory, when one could very easily have done this:

    Sheep clonedSheep=(Sheep)sally.makeCopy();

    I realize that there is some underlying reason as to why the above design is better, and it maybe that the benefits of applying the design patterns do not manifest in small programs , but a gentle reminder of why they ultimately are better would complement these tutorial videos very nicely. Or else, these tutorials would just be a ‘hey, so this is this and that is that…’, and not ‘Hey, but this is why this is so and that is such!’

  10. themis says:

    thank you ever so much mate! i consider you one of these few rare individuals that really try to make the world a better place, i wish i could shake your hand and buy you a beer 🙂

  11. Rupesh Kumar Tiwari says:

    Hi Derek,
    I have watched many videos on Design Patterns but the way you have explained is very nice and your examples are so practical and real that it is easy to remember because it related with real world problems.

    Please keep up the good work. I would suggest you to pick up such complex concept and keep explaining them in your way…

Leave a Reply

Your email address will not be published.