All Courses

Python Design Patterns - Prototype

Neha Kumawat

2 years ago

Python Design Patterns - Prototype | insideAIML
Table of Contents
  • Introduction
  • Implementation of a prototype pattern
  • Advantages
  • Disadvantages


          The prototype design pattern is a creational design pattern that helps to hide the complexity of the instances created by the class. It allows to copy an 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 .

Implementation of a prototype pattern

          Implementation of a prototype pattern.
import copy

class Cloneclass:

   type = none
   value = none

   def clone(self):

   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 instatiation
   of the classes from these prototypes.

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

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

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

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

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

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

def main():
   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__":


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.


  • 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.


  • If the number of objects is less then it is not a suitable pattern i.e. it is wasting the resources .
Liked what you read? Then don’t break the spree. Visit our insideAIML blog page to read more awesome articles. 
Or if you are into videos, then we have an amazing Youtube channel as well. Visit our InsideAIML Youtube Page to learn all about Artificial Intelligence, Deep Learning, Data Science and Machine Learning. 
Keep Learning. Keep Growing. 
Recommended Course For you :
Recommended blogs For you :

Submit Review