7.6. Prototype
EN: Prototype
PL: Prototyp
Type: object
The Prototype design pattern is a creational design pattern that allows cloning objects, even complex ones, without coupling to their specific classes. All prototype classes have a common interface that makes it possible to clone objects.
Here's a simple example of the Prototype pattern in Python:
>>> import copy
...
>>> class Prototype:
... def clone(self):
... return copy.deepcopy(self)
...
>>> class ConcretePrototype1(Prototype):
... pass
...
>>> class ConcretePrototype2(Prototype):
... pass
...
>>> prototype1 = ConcretePrototype1()
>>> prototype2 = ConcretePrototype2()
>>> clone1 = prototype1.clone()
>>> clone2 = prototype2.clone()
In this example, Prototype is an abstract class that declares the clone method that returns a copy of the current object. ConcretePrototype1 and ConcretePrototype2 are concrete classes that implement the Prototype interface. The clone method uses the copy.deepcopy function to create a deep copy of the current object.
7.6.1. Pattern
Create new object by copying an existing object
7.6.2. Problem
Violates Open/Close Principle
class User:
def __init__(self, firstname, lastname, email):
self.firstname = firstname
self.lastname = lastname
self.email = email
if __name__ == '__main__':
a = User('Mark', 'Watney', 'mwatney@nasa.gov')
b = User( a.firstname, a.lastname, a.email)
7.6.3. Solution
class User:
def __init__(self, firstname, lastname, email):
self.firstname = firstname
self.lastname = lastname
self.email = email
def copy(self):
return User(
self.firstname,
self.lastname,
self.email
)
if __name__ == '__main__':
a = User('Mark', 'Watney', email='mwatney@nasa.gov')
b = a.copy()
7.6.4. Use Case - 0x00
>>> data = [1, 2, 3]
>>> a = data
>>> b = data.copy()
>>>
>>>
>>> data
[1, 2, 3]
>>>
>>> a
[1, 2, 3]
>>>
>>> b
[1, 2, 3]
>>> data.append(4)
>>>
>>>
>>> data
[1, 2, 3, 4]
>>>
>>> a
[1, 2, 3, 4]
>>>
>>> b
[1, 2, 3]
7.6.5. Use Case - 1
from dataclasses import dataclass, field
from datetime import datetime
from typing import Literal
@dataclass
class User:
firstname: str
lastname: str
username: str
password: str
email: str
last_login: datetime | None
role: Literal['admin', 'user', 'guest']
groups: list[str] = field(default_factory=list)
def clone(self):
return User(
firstname = self.firstname,
lastname = self.lastname,
username = self.username,
password = self.password,
email = self.email,
last_login = self.last_login,
role = self.role,
groups = self.groups)
mark = User(
firstname='Mark',
lastname='Watney',
username='mwatney',
password='Ares3',
email='mwatney@nasa.gov',
last_login=None,
role='admin',
groups=['admins', 'users'],
)
melissa = mark.clone()
print(melissa)
# User(firstname='Mark', lastname='Watney', username='mwatney', password='Ares3', email='mwatney@nasa.gov', last_login=None, role='admin', groups=['admins', 'users'])
melissa.firstname = 'Melissa'
melissa.lastname = 'Lewis'
melissa.username = 'mlewis'
melissa.email = 'mlewis@nasa.gov'
print(melissa)
# User(firstname='Melissa', lastname='Lewis', username='mlewis', password='Ares3', email='mlewis@nasa.gov', last_login=None, role='admin', groups=['admins', 'users'])
7.6.6. Use Case - 2
from dataclasses import dataclass, field
from datetime import datetime
from typing import Literal
@dataclass
class User:
firstname: str
lastname: str
username: str
password: str
email: str
last_login: datetime | None
role: Literal['admin', 'user', 'guest']
groups: list[str] = field(default_factory=list)
def clone(self):
return User(**vars(self))
mark = User(
firstname='Mark',
lastname='Watney',
username='mwatney',
password='Ares3',
email='mwatney@nasa.gov',
last_login=None,
role='admin',
groups=['admins', 'users'],
)
melissa = mark.clone()
print(melissa)
# User(firstname='Mark', lastname='Watney', username='mwatney', password='Ares3', email='mwatney@nasa.gov', last_login=None, role='admin', groups=['admins', 'users'])
melissa.firstname = 'Melissa'
melissa.lastname = 'Lewis'
melissa.username = 'mlewis'
melissa.email = 'mlewis@nasa.gov'
print(melissa)
# User(firstname='Melissa', lastname='Lewis', username='mlewis', password='Ares3', email='mlewis@nasa.gov', last_login=None, role='admin', groups=['admins', 'users'])
7.6.7. Use Case - 3
from dataclasses import dataclass, field
from datetime import datetime
from typing import Literal
@dataclass
class User:
firstname: str
lastname: str
username: str
password: str
email: str
last_login: datetime | None
role: Literal['admin', 'user', 'guest']
groups: list[str] = field(default_factory=list)
def clone(self, **kwargs):
values = vars(self) | kwargs
return User(**values)
mark = User(
firstname='Mark',
lastname='Watney',
username='mwatney',
password='Ares3',
email='mwatney@nasa.gov',
last_login=None,
role='admin',
groups=['admins', 'users'],
)
melissa = mark.clone(
firstname='Melissa',
lastname='Lewis',
username='mlewis',
email='mlewis@nasa.gov',
)
print(melissa)
# User(firstname='Melissa', lastname='Lewis', username='mwatney', password='Ares3', email='mwatney@nasa.gov', last_login=None, role='admin', groups=['admins', 'users'])
7.6.8. Use Case - 4
from dataclasses import dataclass, field
from datetime import datetime
from typing import Literal
@dataclass
class User:
firstname: str
lastname: str
username: str
password: str
email: str
last_login: datetime | None
role: Literal['admin', 'user', 'guest']
groups: list[str] = field(default_factory=list)
def clone(self, **kwargs):
values = vars(self) | kwargs
cls = self.__class__
return cls(**values)
@dataclass
class Admin(User):
pass
mark = Admin(
firstname='Mark',
lastname='Watney',
username='mwatney',
password='Ares3',
email='mwatney@nasa.gov',
last_login=None,
role='admin',
groups=['admins', 'users'],
)
melissa = mark.clone(
firstname='Melissa',
lastname='Lewis',
username='mlewis',
email='mlewis@nasa.gov',
)
print(melissa)
# Admin(firstname='Melissa', lastname='Lewis', username='mwatney', password='Ares3', email='mwatney@nasa.gov', last_login=None, role='admin', groups=['admins', 'users'])
7.6.9. Assignments
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: DesignPatterns Creational PrototypeDate
# - Difficulty: easy
# - Lines: 5
# - Minutes: 3
# %% English
# 1. Create class `Date` with:
# - `year: int`
# - `month: int`
# - `day: int`
# - method `.clone()`
# 2. Method `.clone()` returns another `Date` with the same values
# 3. Do not use `vars(self)`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Stwórz klasę `Date` z:
# - `year: int`
# - `month: int`
# - `day: int`
# - metodą `.clone()`
# 2. Metoda `.clone()` zwraca kolejny `Date` z tymi samymi wartościami
# 3. Nie używaj `vars(self)`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> date = Date(1969, 7, 21)
>>> result = date.clone()
>>> result.year
1969
>>> result.month
7
>>> result.day
21
"""
from dataclasses import dataclass
@dataclass
class Date:
year: int
month: int
day: int
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: DesignPatterns Creational PrototypeTime
# - Difficulty: easy
# - Lines: 2
# - Minutes: 3
# %% English
# 1. Create class `Time` with:
# - `hour: int`
# - `minute: int`
# - `second: int`
# - `microsecond: int`
# - method `.clone()`
# 2. Method `.clone()` returns another `Time` with the same values
# 3. Use `vars(self)`
# 4. Run doctests - all must succeed
# %% Polish
# 1. Stwórz klasę `Time` z:
# - `hour: int`
# - `minute: int`
# - `second: int`
# - `microsecond: int`
# - metodą `.clone()`
# 2. Metoda `.clone()` zwraca kolejny `Time` z tymi samymi wartościami
# 3. Użyj `vars(self)`
# 4. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> time = Time(2, 56, 15)
>>> result = time.clone()
>>> result.hour
2
>>> result.minute
56
>>> result.second
15
>>> result.microsecond
0
"""
from dataclasses import dataclass
@dataclass
class Time:
hour: int = 0
minute: int = 0
second: int = 0
microsecond: int = 0
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% About
# - Name: DesignPatterns Creational PrototypeDragon
# - Difficulty: easy
# - Lines: 6
# - Minutes: 8
# %% English
# 1. Create class `Dragon`
# 2. Dragon has attributes:
# - `name: str`
# - `position: tuple[int,int]` default `(0, 0)`
# - `health: int` random from 50 to 100
# - `gold: int` random from 1 to 100
# - method `.clone()`
# 3. Method `.clone()` returns another `Dragon` with the same values
# 4. Use `random.randint()` to generate pseudorandom numbers
# 5. Run doctests - all must succeed
# %% Polish
# 1. Stwórz klasę `Dragon`
# 2. Dragon ma atrybuty:
# - `name: str`
# - `position: tuple[int,int]` domyślnie `(0, 0)`
# - `health: int` losowe od 50 do 100
# - `gold: int` losowe od 1 do 100
# - metodę `.clone()`
# 3. Metoda `.clone()` zwraca kolejnego `Dragon` z tymi samymi wartościami
# 4. Użyj `random.randint()` do generowania pseudolosowych liczb
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from pprint import pprint
>>> from random import seed
>>> seed(0)
>>> dragon = Dragon('Wawelski')
>>> result = dragon.clone()
>>> result.name
'Wawelski'
>>> result.health
74
>>> result.gold
98
>>> result.position
(0, 0)
"""
from dataclasses import dataclass, field
from random import randint, seed
seed(0)
@dataclass
class Dragon:
...