Design Pattern: Prototype Pattern

Summary

Pattern NamePrototype Pattern
Pattern TypeCreational Pattern
Scope Object
TaglineCreate a new object by copying/cloning an existing one
Use casesWhen we want to optimize the object creation performance
Related Patterns Factory
Abstract Factory
Composite
Decorator
Difficulty LevelEasy
Implementations

Definition

Prototype pattern defines the process of cloning an existing object and creating a new object from that. Prototype is the pattern for creating new objects, keeping performance in mind.

When we want to simplify the creation process of an object and optimize the performance of object creation, we can clone an existing object and create a new one from that.

The main focus of Prototype pattern is to reduce the resource usage of the creational process of new object instances.

Use Cases

  • When we need to create new objects and performance of the object creation is critical for the application.
  • When creating a new object instance from scratch is a resource-hungry process and we want to reduce the resource usage of object creation.
  • When we want the client to create new object instances, without knowing which specific class is being instantiated.
  • When we want to instantiate classes at runtime.
  • When we need to create a new object which is a lot similar to an existing object, only with some small differences in state.
  • When the values or some elements of an object come from some external resources(like file, database, external API, etc.), and we want to limit the external resource access. The object can be cached when it’s created for the first time, and the next time the cached object can be used(with some local changes).

Implementation

In Prototype pattern, the object that we are copying should contain the copying feature(clone) itself. The copying is not done using other classes.

Prototype pattern implementation in different languages will differ, as the implementation depends on some language feature. Here we are discussing the common implementation of Prototype pattern.

Language-specific implementation of Prototype pattern is discussed in separate individual articles. Check the language-specific implementations here at: Prototype Pattern Code Implementations

Cloning objects can be difficult if there are circular references.

Basic Prototype pattern implementation has 2 elements.

  1. Prototype Interface: an interface to have a common interface for all objects that supports the prototyping process. This interface will declare a method for cloning objects.
  2. Prototype Concrete Classes: classes that implement the Prototype Interface, and for the implementation these classes define a cloning method. Usually, that method is named as “clone“.

Here are the steps to follow for implementing Prototype pattern:

  1. Create an interface (or abstract class) and declare a function for cloning.
  2. Create concrete classes and implement the interface.
  3. Declare a constructor that accepts param.
  4. In the clone function implementation create a new object of the same class and return it.

Examples

Example #1: Transport

Let’s take the example of a transport system. Take a look at the implementation.

Prototype Interface

interface Prototype 

   // Return type is of Prototype
   clone(): Prototype

end interface

Prototype Implementations

class Car implements Prototype

    var make: int
    var model: String
    var color: String

    // Empty constuctor
    constructor()
         // Add any required operation
         // or keep it empty
    end constructor

    // Constructor
    constructor(carParam: Car)
        this.make = carParam.make
        this.model = carParam.model
        this.color = carParam.color
    end constructor

    function clone(): Prototype
        return new Car(this)
    end function

    function toString(): String
        return "Make: " + make + " | Model: " + model + " | Color: " + color
    end function

end class


class Plane implements Prototype
    var model: String
    var color: String

    // Empty constructor
    constructor()
         // Add any required operation
         // or keep it empty
    end constructor

    // Consctuctor
    constructor(planeParam: Plane)
        this.model = planeParam.model
        this.color = planeParam.color
    end constructor

    function clone(): Prototype
        return new Plane(this)
    end function

    function toString(): String
        return "Model: " + model + " | Color: " + color
    end function

end class

Demo

var car1: Car = new Car()
car1.make = 2014
car1.model = "ABCD"
car1.color = "Red"

print(car1.toString())

// Clone existing car object and create a new one
Car carClone = (Car) car1.clone()    // Cast the returned object of type Prototype to Car to match behavior. This depends on the programming language.
carClone.model = "Some Different Model"
carClone.color = "White"

print(carClone.toString())

Output

Make: 2014 | Model: ABCD | Color: Red

---------------------------------------

Make: 2014 | Model: Some Different Model | Color: White

Code Implementations

Use the following links to check Prototype pattern implementation in specific programming languages.

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.