JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Prototype Pattern in java


The prototype pattern is a creational design pattern. Prototype patterns is required, when object creation is time consuming, and costly operation, so we create object with existing object itself. One of the best available way to create object from existing objects are clone() method. Clone is the simplest approach to implement prototype pattern. However, it is your call to decide how to copy existing object based on your business model.

If you are using clone method, then it is upto you to decide whether go for shallow copy or deep copy based on your business need. Here is the UML structure of the Prototype design pattern:

Here is a simple example to demonstrate the pattern to make you understand. Code is slightly modifed when compared with UML structure. We added ColorStore class to store existing objects.

Abstract class implementing Clonable interface:

package com.java2novice.prototype;

public abstract class Color implements Cloneable {

	protected String colorName;
	
	abstract void fillColor();
	
	public Object clone(){
		
		Object clone = null;
		try {
			clone = super.clone();
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return clone;
	}
}

Concrete class extending above abstract class:

package com.java2novice.prototype;

public class RedColor extends Color{

	public RedColor() {
		this.colorName = "RED";
	}

	@Override
	void fillColor() {
		System.out.println("filling red color...");
	}
}

Concrete class extending above abstract class:

package com.java2novice.prototype;

public class GreenColor extends Color{

	public GreenColor() {
		this.colorName = "Green";
	}

	@Override
	void fillColor() {
		System.out.println("filling green color...");
	}
}

Below code creates all objects and maintain object store. All object requests will be cloned from this store:

package com.java2novice.prototype;

import java.util.HashMap;
import java.util.Map;

public class ColorStore {

	private static Map<String, Color> colorMap = new HashMap<String, Color>(); 
	
	static {
		colorMap.put("red", new RedColor());
		colorMap.put("green", new GreenColor());
	}
	
	public static Color getColor(String colorName){
		
		return (Color) colorMap.get(colorName).clone();
	}
}

Prototype pattern demo class:

package com.java2novice.prototype;

public class PrototypeTest {

	public static void main(String a[]){
		ColorStore.getColor("red").fillColor();
		ColorStore.getColor("green").fillColor();
		ColorStore.getColor("green").fillColor();
		ColorStore.getColor("red").fillColor();
	}
}

Output:
filling red color...
filling green color...
filling green color...
filling red color...
<< Previous Program | Next Program >>
blog comments powered by Disqus

Java design pattern examples

  1. Factory Pattern
  2. Abstract Factory Pattern
  3. Builder Design Pattern
  4. Prototype Pattern
  5. Adapter Pattern
  6. Composite Pattern
  7. Proxy Pattern
Knowledge Centre
Exception Vs Error
An error is an irrecoverable condition occurring at runtime like out of memory error. These kind of jvm errors cannot be handled at runtime. Exceptions are because of condition failures, which can be handled easily at runtime.
Famous Quotations
Success consists of going from failure to failure without loss of enthusiasm.
-- Winston Churchill

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.