noscript

The Prototype design pattern is a creational pattern that allows you to copy existing objects without making your code dependent on their classes. This pattern is useful when the cost of creating a new instance of a class is more expensive than copying an existing instance.

A step-by-step guide to implementing the Prototype pattern in Java:

1. Define a Prototype Interface:
The interface will declare a `clone` method.

2. Implement Concrete Prototypes:
These classes will implement the `clone` method to return a copy of themselves.

3. Use the Prototype:
You can use the `clone` method to create copies of objects.

Step 1: Define a Prototype Interface

public interface Prototype {
Prototype clone();
}

Step 2: Implement Concrete Prototypes

Here, we’ll create a `Shape` class that implements the `Prototype` interface and two concrete classes: `Circle` and `Rectangle`.

abstract class Shape implements Prototype {
private String id;
protected String type;

abstract void draw();

public String getType(){
return type;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@Override
public abstract Shape clone();
}

class Circle extends Shape {
public Circle(){
type = “Circle”;
}

@Override
public void draw() {
System.out.println(“Inside Circle::draw() method.”);
}

@Override
public Shape clone() {
Circle clone = null;
try {
clone = (Circle) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
}

class Rectangle extends Shape {
public Rectangle(){
type = “Rectangle”;
}

@Override
public void draw() {
System.out.println(“Inside Rectangle::draw() method.”);
}

@Override
public Shape clone() {
Rectangle clone = null;
try {
clone = (Rectangle) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
}

Step 3: Use the Prototype

We can create a `ShapeCache` class to store shape objects and return their clones.

import java.util.Hashtable;
public class ShapeCache {
private static Hashtable<String, Shape> shapeMap = new Hashtable<String, Shape>();

public static Shape getShape(String shapeId) {
Shape cachedShape = shapeMap.get(shapeId);
return (Shape) cachedShape.clone();
}

public static void loadCache() {
Circle circle = new Circle();
circle.setId(“1”);
shapeMap.put(circle.getId(), circle);

Rectangle rectangle = new Rectangle();
rectangle.setId(“2”);
shapeMap.put(rectangle.getId(), rectangle);
}
}

Client Code

Here’s how you can use the `ShapeCache` to get clones of shapes.

public class PrototypePatternDemo {
public static void main(String[] args) {
ShapeCache.loadCache();

Shape clonedShape1 = ShapeCache.getShape(“1”);
System.out.println(“Shape : ” + clonedShape1.getType());
clonedShape1.draw();

Shape clonedShape2 = ShapeCache.getShape(“2”);
System.out.println(“Shape : ” + clonedShape2.getType());
clonedShape2.draw();
}
}

Explanation

1. Prototype Interface: The `Prototype` interface declares the `clone` method.
2. Concrete Prototypes: The `Shape` class and its subclasses (`Circle` and `Rectangle`) implement the `clone` method. The `clone` method is responsible for creating and returning a copy of the object.
3. ShapeCache: This class is responsible for storing the original instances of `Shape` objects. The `loadCache` method initializes the cache with some shapes, and the `getShape` method returns a clone of the shape from the cache.
4. PrototypePatternDemo: The client code demonstrates how to use the `ShapeCache` to get clones of shapes and work with them.

This approach ensures that creating new instances of objects is efficient and that the objects can be extended without changing the code that uses them.

                                                          For more information & classes Call2048553004
                                                                         Registration Link: Click Here!

Author: Ambarish Durani

JAVA Trainer

IT Education Centre Placement & Training Institute

© Copyright 2024 | IT Education Centre.