Patterns / Creational patterns
image

from abc import ABCabstractmethod
import copy


# Prototype
class Shape(ABC):
    def __init__(self, color):
        self.color = color

    @abstractmethod
    def clone(self):
        pass


# ConcretePrototype
class Square(Shape):
    def clone(self):
        return copy.deepcopy(self)


# Client
class ShapeMaker:
    def __init__(self, shape):
        self.shape = shape

    def make_shape(self):
        return self.shape.clone()


square = Square("Red")
maker = ShapeMaker(square)

square1 = maker.make_shape()
square2 = maker.make_shape()

print(square1 == square2)
print(square1.color)


Specify the kinds of objects to create using a prototypical instance, and create new objectsby copying this prototype.