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 >>

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
What is OOPs?
Object oriented programming organizes a program around its data, i.e. , objects and a set of well defined interfaces to that data. An object-oriented program can be characterized as data controlling access to code.
Famous Quotations
Millions long for immortality who do not know what to do with themselves on a rainy Sunday afternoon.
-- Susan Erz

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

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