Design Patterns: Prototype

Prototype is a design pattern that belongs in the “creational patterns” category. This pattern is used in order to avoid the usage of “new” operator, especially in case that there is a need to create new objects at run-time.

So, there is the possibility to clone an object instead of using the “new” operator in order to get a new object. This new object is very different from the initial object, but the new one keeps the state of the old object.

To be more precise, before cloning an object, we have to create one. That means we have to get an instance of the class using new operator as it’s already known. Continuing, we are ready to get more instances of the object just by cloning it.

In order to clone an object, the object must always do implements Cloneable interface.

The advantage of using cloning is less costly than creating a new one with “new” operator.

Also, an object which is Cloneable is able to be cloned every time during code execution. Usage of Cloneable is suggested in this occasion and can improve the execution time.

Equally important and useful is when a class consists of more than one states. So, it’s preferable to create instances for every single state and clone each one of them instead of using “new” operator.

Continuing, we will see an example of using prototype design pattern. You can download the source code from GitHub.

Prototype Example

We will use the same example that we used in Object Pooling design pattern.

So, we will have a class that it’s going to create a dot.

This class is named DotPrototype and implements Cloneable Interface as I said earlier.

You can see source code of DotPrototype below.

As you can see this class takes only two arguments which depict a dot. Also, it accepts the dot’s color.

At the end of the class there is a function named clone() which gets overriden.

This function calls parent function clone() returning a new instance of object. In case that class doesn’t implement Cloneable interface then we get an exception message.

Next step is to create another class and we will implement all states of the DotPrototype class there. And we will put these states in a Hashtable in order to get one, easily and fast.

We are going to create 3 instances of DotPrototype and each one will have a different color. So, Hashtable is going to keep 3 instances, red dot, yellow and green dot.

Source code of DotSpawner

 

After that we have to initialize DotSpawner class giving the command below.

DotSpawner is static class.

We are almost ready, the last thing that we have to do is to call getDot(“Color that we have instanciate”) to get the desirable instance and call clone().

So, we get a new different object but with the same state.

Output : 

prototype design pattern

Conclusion

In order to use clone method, the class must implement Cloneable interface.

It’s useful to use prototype pattern when there is a need to create new instances of an object during execution time. Also, when a class has many states to implement.

Using clone is faster than using “new” operator.

Last but not least, you can see the resources below that I found very useful.

gameprogrammingpatterns.com

sourcemaking.com

oodesign.com

wikipedia.org

1 Comment

  1. geodoo April 2, 2017 at 9:11 pm

    I am happy to see that you finally write about what you like most: Design patterns and game development!

    Way to go mate! Keep up the good work!

Comments are closed.