Prototype Pattern Implementation in Java

Vector Prototype Implementation

This implementation demonstrates mathematical vector encapsulation in Java with dynamic memory allocation for variable-length vectors. It showcases both shallow and deep cloning techniques for copying vector objects.

Shallow Cloning Approach

In shalow cloning, only the object itself is duplicated, while referanced objects are shared between the original and cloned instances.

// Vector.java
package prototype.shallow;

import java.util.Arrays;

public class Vector implements Cloneable {
    private int[] elements;
    private Metadata metadata = null;

    public Vector() {
        this.metadata = new Metadata();
    }

    @Override
    public Object clone() {
        Vector duplicate = null;
        try {
            duplicate = (Vector) super.clone();
        } catch (CloneNotSupportedException e) {
            System.err.println("Cloning not supported!");
        }
        return duplicate;
    }

    public Metadata getMetadata() {
        return this.metadata;
    }

    public void showElements() {
        System.out.println(Arrays.toString(elements));
    }

    public int[] getElements() {
        return elements;
    }

    public void setElements(int[] elements) {
        this.elements = elements;
    }
}

// Metadata.java
package prototype.shallow;

public class Metadata {
    public void process() {
        System.out.println("Vector duplication");
    }
}

// TestClient.java
package prototype.shallow;

public class TestClient {
    public static void main(String[] args) {
        Vector original, copied;
        original = new Vector();
        int[] data = {2, 8, 3, 6};
        original.setElements(data);
        
        System.out.println("Shallow Clone Example:");
        copied = (Vector) original.clone();
        
        System.out.print("Original: ");
        original.showElements();
        System.out.print("Copied: ");
        copied.showElements();
        
        System.out.println("original == copied? " + (original == copied));
        System.out.println("original.getMetadata() == copied.getMetadata()? " + 
                          (original.getMetadata() == copied.getMetadata()));
    }
}

Deep Cloning Approach

Deep cloning creates a completely independent copy of an object, including all referenced objects within the original.

// Vector.java
package prototype.deep;
import java.io.*;
import java.util.Arrays;

public class Vector implements Serializable {
    private int[] elements;
    private Metadata metadata = null;

    public Vector() {
        this.metadata = new Metadata();
    }

    public Object createDeepCopy() throws IOException, ClassNotFoundException {
        // Serialize object to byte stream
        ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
        ObjectOutputStream objectOutput = new ObjectOutputStream(byteOutput);
        objectOutput.writeObject(this);

        // Deserialize object from byte stream
        ByteArrayInputStream byteInput = new ByteArrayInputStream(byteOutput.toByteArray());
        ObjectInputStream objectInput = new ObjectInputStream(byteInput);
        return objectInput.readObject();
    }

    public Metadata getMetadata() {
        return this.metadata;
    }

    public void showElements() {
        System.out.println(Arrays.toString(elements));
    }

    public int[] getElements() {
        return elements;
    }

    public void setElements(int[] elements) {
        this.elements = elements;
    }
}

// Metadata.java
package prototype.deep;
import java.io.Serializable;

public class Metadata implements Serializable {
    public void process() {
        System.out.println("Vector duplication");
    }
}

// TestClient.java
package prototype.deep;

public class TestClient {
    public static void main(String[] args) {
        Vector original, copied = null;
        original = new Vector();
        int[] data = {2, 8, 3, 6};
        original.setElements(data);
        
        System.out.println("Deep Clone Example:");
        
        try {
            copied = (Vector) original.createDeepCopy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        System.out.print("Original: ");
        original.showElements();
        System.out.print("Copied: ");
        copied.showElements();
        
        System.out.println("original == copied? " + (original == copied));
        System.out.println("original.getMetadata() == copied.getMetadata()? " + 
                          (original.getMetadata() == copied.getMetadata()));
    }
}

Key Differences Between Cloning Methods

  1. Base Cloning Mechanism: All Java classes inherit from java.lang.Object, which provides a clone() method for object duplication. This enables direct use of Object's clone() for creating object copies.
  2. Shallow Cloning Characteristics: Only the object instance is duplicated along with its primitive attributes. When the object contains references to other objects, those referenced objects are not cloned - both original and cloned objects point to the same referenced objects.
  3. Deep Cloning Characteristics: Both the object and all referenced objects within it are completely duplicated. Modifications to the original object's properties do not affect the cloned object. Serialization through object streams achieves deep cloning functionality.

Tags: java Prototype Pattern Object Cloning shallow copy deep copy

Posted on Tue, 21 Jul 2026 16:21:05 +0000 by yarub