Using the Prototype Design Pattern In Java

Using the Prototype Design Pattern In Java

Learn about and how to apply the Prototype Pattern in Java. In this tutorial, we're going to learn about one of the Creational Design Patterns – the Prototype Design Pattern. Prototype design pattern is used when the Object creation is a costly affair and requires a lot of time and resources and you have a similar object already existing. Prototype pattern provides a mechanism to copy the original object to a new object and then modify it according to our needs.

Let's discuss today another creational design pattern named - Prototype Design Pattern.

Prototype Design Pattern

  • The Prototype Design Pattern is one of the twenty-three well-known GoF design patterns which helps us copying an existing object rather than creating a new instance from scratch.
  • The Prototype pattern is used when we need to create a number of instances of a class and each instance has almost same state or has very little difference.
  • The Prototype pattern is used when creation of actual object directly is costly as per resource and data it holds and also is a time taking.
  • The existing original object acts as a prototype and contains the state of the object. The new object is copy of the original object.
  • The Prototype pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects.
  • We are free to change values in the newly copied object whenever we required.
  • The Prototype pattern helps us to save costly resources and time, especially when the object creation is a heavy and time taking process.
  • In Java, One of the best available way to create object from existing objects are by using clone() *method of Cloneable interface. *Clone is the simplest approach to implement prototype pattern.
  • We can also implement Prototype interface by using *Deep Copy *method.
  • Deep Copy - By Deep copy we create an object by copying all the fields of the original object. And also if the original object contains other objects as fields, we copy them as well.
  • Shallow Copy - By Shallow copy we create an object by copying all the fields of the original object. But if the original object contains other objects as fields, we copy only the reference of those objects instead of copying complete object itself.
  • There are two ways of implementing the Prototype pattern
  • Basic Implementation
  • Prototype Registry Implementation

Basic ImplementationBasic Implementation

Prototype Registry Implementation

Prototype Registry Implementation

  • In the Prototype Registry implementation of the pattern, we create a registry class to cache the objects for prototyping. In other worlds we use a prototype collection and management service or class which makes all the prototype accessible and available for prototyping.
  • The purpose of the cache-registry is to maintain a pool of prototype ready objects in-memory.

To better understand this, let's look at an example of geometric shapes. I am keeping the example relatively simple to keep the focus on the pattern.

Let's first define few common enums which we will use in each of different methods on implementation of Prototype pattern.

Here, the code for Color enum:

package org.trishinfotech.prototype;
public enum Color {
    RED, GREEN, BLUE, YELLOW, WHITE, BLACK, ORANGE, MAROON
}

Here, the code for LinePattern enum. We will use these line patterns for drawing different geometric shapes.

package org.trishinfotech.prototype;
public enum LinePattern {
    SOLID, DASH, DOT, DOUBLE_DASH, DASH_SPACE
}

Here, the code for FillPattern enum. We will use these fill patterns for painting different geometric shapes.

package org.trishinfotech.prototype;
public enum FillPattern {
    SOLID, DASH, DOT, CHECKS, HEART;
}

java design-pattern programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Proxy Design Pattern in JAVA

In this blog, we will discuss Proxy Design Pattern, its example, and how it is different from the other design patterns. But, it’s important to have an understanding of the design patterns first. You can refer here.

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java Design Patterns for Beginners - Design Patterns in Java

"Java Design Patterns for Beginners - Design Patterns in Java - Design Patterns Tutorial" will provide you with detailed knowledge about Java Design Patterns and along with it. Why do we need Design Patterns? Structure of Design Patterns. Types of Design Patterns. Creational Design Patterns. Factory Design Pattern. Overview of Design Patterns

Design Patterns — Decorator Design Pattern

The decorator design pattern is a software design pattern that helps you add responsibilities to an object at runtime. This pattern is considered to be a structural design pattern. This type of pattern focuses on the relationships between classes and how they can help solve specific problems.

State Design Pattern In Java

The state pattern in Java is a behavioural software design pattern that allows an object to alter its behaviour when its internal state changes. State is a behavioral design pattern that allows an object to change the behavior when its internal state changes. The pattern extracts state-related behaviors into separate state classes and forces the original object to delegate the work to an instance of these classes, instead of acting on its own.