
from abc import ABC, abstractmethod
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)
| Описывает виды создаваемых объектов с помощью прототипа и создает новые объекты путем его копирования. |