Alexey Zvolinskiy

About Alexey Zvolinskiy

Alexey is a test developer with solid experience in automation of web-applications using Java, TestNG and Selenium. He is so much into QA that even after work he provides training courses for junior QA engineers.

Design Patterns: Prototype

The one of creational design patterns is a Prototype design pattern. Despite that the Prototype is the creational pattern it distinguishes from other patterns by a concept. I mean that the Prototype in some sense creates itself. I’m going to explain it bellow.java-design-patterns

All magic of the Prototype pattern is based on a clone() method of a java Object. So let’s to consider an example of usage and then I will try to figure out which cons and pros this pattern has.
 
 
prototype-design-pattern-java

The class diagram above shows to us an basic sense of the pattern. An abstract class or an interface can play role of a prototype. Notice that the prototype have to extend Cloneable interface. That’s because concrete implementations of the prototype will invoke the clone() method. The particular class which implements interface (extends the abstract class) has to contain method which will return a copy of itself with the help of clone operation.

In my example I declared the Unicellular interface as the prototype and the Amoeba class as its realisation:

public interface Unicellular extends Cloneable {

	public Unicellular reproduce();

}
public class Amoeba implements Unicellular {

	public Unicellular reproduce() {
		Unicellular amoeba = null;
		try {
			amoeba = (Unicellular) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return amoeba;
	}

	public String toString() {
		return "Bla bla bla it's a new amoeba...";
	}

}

Demonstration:

...
	public static void main(String[] args) {

		Unicellular amoeba = new Amoeba();

		List< Unicellular > amoebaList = new ArrayList< Unicellular >();

		amoebaList.add(amoeba.reproduce());
		amoebaList.add(amoeba.reproduce());
		amoebaList.add(amoeba.reproduce());

		for (Unicellular a : amoebaList)
			System.out.println(a);

	}
...

The result:

Bla bla bla it’s a new amoeba…
Bla bla bla it’s a new amoeba…
Bla bla bla it’s a new amoeba…

What about the cons and pros? Actually I don’t know what to say regarding this, because I have never encountered such situations where the Prototype pattern will be applied appropriately. Maybe in some cases when you don’t need to call a constructor explicitly or when a system don’t need to depend on way of objects creation.
 

Reference: Design Patterns: Prototype from our JCG partner Alexey Zvolinskiy at the Fruzenshtein’s notes blog.
Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


2 + one =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books