All Courses

Python Design Patterns - Prototype

Neha Kumawat

3 years ago

Python Design Patterns | Insideaiml
Prototype design pattern is a creational design pattern which helps to hide the complexity of the instances created by the class. It allows to copy existing object or we can say cloning  the existing object which is independent of the concrete implementation of their classes.
An object that supports cloning is called as Prototype.
The prototype design pattern saves time and resources .
             
Recommended blogs For you :  Design Patterns Template in Python

Implementation of a prototype pattern

Implementation of prototype design pattern.
import copy

class Cloneclass:

   type = none
   value = none

   def clone(self):
      pass

   def getType(self):
      return self.type

   def getValue(self):
      return self.value

class Type1(Cloneclass):

   def __init__(self, no):
      self.type = "Type1"
      self.value = no

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

class Type2(Cloneclass):

   """ Concrete Cloneclass. """

   def __init__(self, no):
      self.type = "Type2"
      self.value = no

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

class ObjectGroup:

   """ Manages prototypes.
   It encapsulates prototype
   initialization and then allows instantiation
   of the classes from these prototypes.
   """

   object1 = none
   object2 = none
   object3 = none
   object4 = none

   @staticmethod
   def initialize():
      ObjectGroup.object1 = Type1(1)
      ObjectGroup.object2 = Type1(2)
      ObjectGroup.object3 = Type2(1)
      ObjectGroup.object4 = Type2(2)

   @staticmethod
   def getObject1():
      return ObjectGroup.object1.clone()

   @staticmethod
   def getObject2():
      return ObjectGroup.object2.clone()

   @staticmethod
   def getObject3():
      return ObjectGroup.object3.clone()

   @staticmethod
   def getObject4():
      return ObjectGroup.object4.clone()

def main():
   ObjectGroup.initialize()
   
   obj = ObjectGroup.getObject1()
   print("%s: %s" % (obj.getType(), obj.getValue()))
   
   obj = ObjectGroup.getObject2()
   print("%s: %s" % (obj.getType(), obj.getValue()))
   
   obj = ObjectGroup.getObject3()
   print("%s: %s" % (obj.getType(), obj.getValue()))
   
   obj = ObjectGroup.getObject4()
   print("%s: %s" % (obj.getType(), obj.getValue()))

if __name__ == "__main__":
   main()

Output

Output of implement a prototype pattern | insideaiml
The prototype method provides a way to copy the existing object and then modify it according to our needs.

Advantages
  • There is no need to create new sub classes.
  • Create complex objects more conveniently.
  • Creating a prototype of an object without pairing to their concrete classes
  • Saves time and resources.
            
Disadvantages
  • If the number of objects is less, then it is not a suitable pattern, i.e., it is wasting resources.
             
I hope you enjoyed reading this article and finally, you came to know about Python Design Patterns - Prototype.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…
  
Recommended Course For you :
   
Recommended blogs For you :

Submit Review