Prototype Pattern

Implement a simple Prototype pattern by cloning existing objects.

PythonIntermediate
Python
# Program to implement a simple Prototype pattern

import copy

class Prototype:
    def clone(self):
        return copy.deepcopy(self)


class Document(Prototype):
    def __init__(self, title, content):
        self.title = title
        self.content = content


doc1 = Document("Report", "Content here")
doc2 = doc1.clone()
doc2.title = "Report Copy"

print(doc1.title)
print(doc2.title)

Output

Report
Report Copy

The base Prototype class offers a 'clone' method that subclasses can reuse to duplicate instances.