World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Download our e-book of Introduction To Python
4.5 (1,292 Ratings)
559 Learners
Neha Kumawat
a year ago
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 instatiation
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()