Prototype Method

Prototype Method

Prototype Method

The Prototype method is a type of creational design pattern that allows cloning of objects(both complex objects too) without coupling to their effect. Also, It is a creational pattern that hides the complexity of instances created by class to which the existing object will differ from that of the new object(duplicated object). The new object(cloned object) may have little changes in its properties, and this makes this approach a time saver.

The prototype pattern aims to reduce the number of classes used for an application by allowing you to copy existing objects independently. When the creation of objects is a tedious task in terms of time and usage of resources, and these objects already exist, the Prototype Method is the best design pattern to use. It further gives you the edge to copy the original object and then allows you to modify it according to your needs. A prototype method is an object that will enable you to clone other objects.

Implementation

I was hoping you could hear me out on this first, you were assigned to build a prototype for a pending project, but you are to use the pre-existing feature and not to build from scratch. The best method for you to use to approach this, is the Prototype method because it helps you to clone the pre-existing feature, and it also saves time.

Python is the programming language used for The implementation of the prototype method:

# import the copy library for cloning
import copy

# the factory prototype
class Prototype:
    _factoryType = None
    _factoryValue = None

    def clone(self):
        pass

    def getType(self):
        return self._factoryType

    def getValue(self):
        return self._factoryValue

class FactoryType1(Prototype):
    def __init__(self, number):
        self._factoryType = 'Factory Type 1  '
        self._factoryValue = number

    def clone(self):
        return copy.copy(self)

class FactoryType2(Prototype):
    def __init__(self, number):
        self._factoryType = 'Factory Type 2  '
        self._factoryValue = number

    def clone(self):
        return copy.copy(self)

class FactoryObject:
    """ It manages the prototypes.
        The static factory ancapsulates the
        prototype and it also allows instantiation
        of the classses.  
    """
    __FactoryType1Value1 = None
    __FactoryType1Value2 = None
    __FactoryType2Value1 = None
    __FactoryType2Value2 = None

    @staticmethod
    def initialize():
        FactoryObject.__FactoryType1Value1 = FactoryType1(1)
        FactoryObject.__FactoryType1Value2 = FactoryType1(2)
        FactoryObject.__FactoryType2Value1 = FactoryType2(1)
        FactoryObject.__FactoryType2Value2 = FactoryType2(2)

    @staticmethod
    def getType1Value1():
        return FactoryObject.__FactoryType1Value1.clone()

    @staticmethod
    def getType1Value2():
        return FactoryObject.__FactoryType1Value2.clone()

    @staticmethod
    def getType2Value1():
        return FactoryObject.__FactoryType2Value1.clone()

    @staticmethod
    def getType2Value2():
        return FactoryObject.__FactoryType2Value1.clone()

def main():
    #  Initializing the Factory Object
    FactoryObject.initialize()

    instance = FactoryObject.getType1Value1()
    print("{0}: {1}".format(instance.getType(), instance.getValue()))

    instance = FactoryObject.getType1Value2()
    print("{0}: {1}".format(instance.getType(), instance.getValue()))

    instance = FactoryObject.getType2Value1()
    print("{0}: {1}".format(instance.getType(),instance.getValue()))

    instance = FactoryObject.getType2Value2()
    print("{0}: {1}".format(instance.getType(), instance.getValue()))

if __name__ == '__main__':

    main()
The above code is how a Factory prototype was explained using the factory method. The following Output was generated:
ProBook:~/InfosecAddicts/python design pattern$ python prototype.py
Factory Type 1 : 1
Factory Type 1 : 2
Factory Type 2 : 1
Factory Type 2 : 1
The Output explains how the code was able to create new objects with the existing ones just by cloning the pre-existing object.

Pros and Cons

Pros
  • A low number of Subclasses.
  • New objects have values that vary.
  • New objects have structures that vary.
  • It gets rid of repeated initialization code in favour of cloning pre-built prototypes.
  • Production of complex objects is more convenient.
  • It is an alternative to inheritance when dealing with configuration presets for complex objects.

 

Cons
  • Abstraction is immersively present
  • ¬†Cloning complex objects that have circular references might be very tricky.