Python Prototype Pattern
Imagine you want to make a batch of identical toy cars. Instead of designing and manufacturing from scratch each time, you first make a perfect prototype and then copy it. This is the core idea of the Prototype pattern!
Prototype patternis a creational design pattern that creates new objects by copying an existing object (the prototype) rather than instantiating through a class. This approach is especially suitable when object creation is costly or when multiple similar objects are needed.
Why is the Prototype pattern needed?
Let us first look at the problems with the traditional way of creating objects:
Example
Traditional approach - recreate every time
class
Car:
def
init
(
self
,
brand
,
model
,
color
,
engine_type
)
:
self
.
brand
=
brand
self
.
model
=
model
self
.
color
=
color
self
.
engine_type
=
engine_type
Assume there is some complex initialization logic here
self
.
initialize_complex_components
(
)
def
initialize_complex_components
(
self
)
:
Simulate a complex initialization process
import
time
time
.
sleep
(
1
)
Assume initialization takes 1 second
(
f
"Initializing complex components of {self.brand} {self.model}..."
)
Create multiple similar objects
car1
=
Car
(
"Toyota"
,
"Camry"
,
"Red"
,
"2.5L"
)
car2
=
Car
(
"Toyota"
,
"Camry"
,
"Blue"
,
"2.5L"
)
Need to re-run complex initialization
Disadvantages of the traditional approach:
- Every object creation requires the full initialization process
- If initialization is complex, it consumes a lot of time and resources
- Code duplication and low efficiency
Implementation principle of the Prototype pattern
The Prototype pattern solves the above problems by letting objects take responsibility for creating their own copies. In Python, we can use the copy module to easily implement the Prototype pattern.
Core Components

Copy mechanisms in Python
Python provides two ways to copy:
| Copy type | Method | Feature | Applicable scenarios |
|---|---|---|---|
| Shallow copy | copy.copy() |
Only copies the object itself, not nested objects | Object structure is simple, with no nested references |
| Deep copy | copy.deepcopy() |
Copies the object and all its nested objects | Object structure is complex, with nested references |
Implementing the Prototype pattern
Let us learn how to implement the Prototype pattern through a complete example.
Basic Implementation
Example
import
copy
from
abc
import
ABC
,
abstractmethod
from
typing
import
Any
class
Prototype
(
ABC
)
:
"""Prototype abstract base class"""
@
abstractmethod
def
clone
(
self
)
Any:
"""Clone method - must be implemented by subclasses"""
pass
class
CarPrototype
(
Prototype
)
:
"""Car prototype class"""
def
init
(
self
,
brand:
str
,
model:
str
,
color:
str
,
engine_type:
str
)
:
self
.
brand
=
brand
self
.
model
=
model
self
.
color
=
color
self
.
engine_type
=
engine_type
self
.
accessories
=
[
]
Accessories list
self
.
initialize_complex_components
(
)
def
initialize_complex_components
(
self
)
:
"""Simulate a complex initialization process"""
(
f
"Initializing complex components of {self.brand} {self.model}..."
)
Here you can simulate some time-consuming initialization operations
def
add_accessory
(
self
,
accessory:
str
)
:
"""Add an accessory"""
self
.
accessories
.
append
(
accessory
)
def
clone
(
self
)
'CarPrototype'
:
"""Implement the clone method - use deep copy"""
return
copy
.
deepcopy
(
self
)
def
display_info
(
self
)
:
"""Display car information"""
info
=
f
"{self.brand} {self.model} - Color: {self.color}, Engine: {self.engine_type}"
if
self
.
accessories
:
info +
=
f
", Accessories: {', '.join(self.accessories)}"
(
info
)
Usage examples
Example
Create the prototype object
(
"=== Create the prototype object ==="
)
original_car
=
CarPrototype
(
"Toyota"
,
"Camry"
,
"White"
,
"2.5L"
)
original_car.
add_accessory
(
"Navigation system"
)
original_car.
add_accessory
(
"Sunroof"
)
original_car.
display_info
(
)
(
"\n=== Create new objects by cloning ==="
)
Create new objects based on the prototype
car1
=
original_car.
clone
(
)
car1.
color
=
"Red"
Only change the color
car1.
display_info
(
)
car2
=
original_car.
clone
(
)
car2.
color
=
"Blue"
car2.
add_accessory
(
"Leather seats"
)
Add a new accessory
car2.
display_info
(
)
Verify the prototype object was not modified
(
"\n=== Verify the prototype object was not modified ==="
)
original_car.
display_info
(
)
Output result:
=== 创建原型对象 ===
正在初始化 Toyota Camry 的复杂组件...
Toyota Camry - 颜色: 白色, 发动机: 2.5L, 配件: 导航系统, 天窗
=== 通过克隆创建新对象 ===
Toyota Camry - 颜色: 红色, 发动机: 2.5L, 配件: 导航系统, 天窗
Toyota Camry - 颜色: 蓝色, 发动机: 2.5L, 配件: 导航系统, 天窗, 真皮座椅
=== 验证原型对象未被修改 ===
Toyota Camry - 颜色: 白色, 发动机: 2.5L, 配件: 导航系统, 天窗
Advanced application scenarios
Scenario 1: Character creation in game development
Example
class
GameCharacter
(
Prototype
)
:
"""Game character prototype"""
def
init
(
self
,
name:
str
,
character_class:
str
,
level:
int
=
1
)
:
self
.
name
=
name
self
.
character_class
=
character_class
self
.
level
=
level
self
.
skills
=
[
]
self
.
equipment
=
{
}
self
.
initialize_character
(
)
def
initialize_character
(
self
)
:
"""Initialize the character - simulate complex data loading"""
(
f
"Loading character data for {self.name}..."
)
Simulate loading data from a database or configuration file
base_skills
=
{
"Warrior"
:
[
"Slash"
,
"Block"
,
"Charge"
]
,
"Mage"
:
[
"Fireball"
,
"Ice arrow"
,
"Teleport"
]
,
"Archer"
:
[
"Precision shot"
,
"Trap placement"
,
"Quick move"
]
}
self
.
skills
=
base_skills.
get
(
self
.
character_class
,
[
]
)
def
add_skill
(
self
,
skill:
str
)
:
"""Add a skill"""
self
.
skills
.
append
(
skill
)
def
equip_item
(
self
,
slot:
str
,
item:
str
)
:
"""Equip an item"""
self
.
equipment
[
slot
]
=
item
def
clone
(
self
)
'GameCharacter'
:
"""Clone the character"""
return
copy
.
deepcopy
(
self
)
def
show_status
(
self
)
:
"""Display character status"""
(
f
"Character: {self.name} ({self.character_class}) - Level: {self.level}"
)
(
f
"Skills: {', '.join(self.skills)}"
)
if
self
.
equipment
:
equipment_str
=
', '
.
join
(
[
f
"{k}: {v}"
for
k
,
v
in
self
.
equipment
.
items
(
)
]
)
(
f
"Equipment: {equipment_str}"
)
Usage examples
(
"=== Game character prototype example ==="
)
warrior_template
=
GameCharacter
(
"Warrior template"
,
"Warrior"
)
warrior_template.
equip_item
(
"Weapon"
,
"Steel Longsword"
)
warrior_template.
equip_item
(
"Armor"
,
"Chain Mail"
)
warrior_template.
show_status
(
)
(
"\n=== Create Player Character ==="
)
player1
=
warrior_template.
clone
(
)
player1.
name
=
"Brave Adventurer"
player1.
level
=
5
player1.
add_skill
(
"Whirlwind Slash"
)
player1.
show_status
(
)
player2
=
warrior_template.
clone
(
)
player2.
name
=
"Fearless Guardian"
player2.
level
=
3
player2.
equip_item
(
"Shield"
,
"Steel Shield"
)
player2.
show_status
(
)
Scenario 2: Document Template System
Example
class
DocumentTemplate
(
Prototype
)
:
"""Document Template Prototype"""
def
init
(
self
,
template_name:
str
)
:
self
.
template_name
=
template_name
self
.
headers
=
{
}
self
.
content_sections
=
[
]
self
.
styles
=
{
}
self
.
load_template_config
(
)
def
load_template_config
(
self
)
:
"""Load template configuration - simulate complex configuration loading"""
(
f
"Loading {self.template_name} template configuration..."
)
Simulate loading configuration from a file or database
self
.
headers
=
{
"title"
: f
"{self.template_name} Document"
,
"author"
:
"System Generated"
,
"date"
:
"2024-01-01"
}
self
.
styles
=
{
"font_family"
:
"Arial"
,
"font_size"
:
"12pt"
,
"line_spacing"
:
"1.5"
}
def
clone
(
self
)
'DocumentTemplate'
:
"""Clone document template"""
return
copy
.
deepcopy
(
self
)
def
customize
(
self
,
title:
str
=
None
,
author:
str
=
None
)
:
"""Custom Document"""
if
title:
self
.
headers
[
"title"
]
=
title
if
author:
self
.
headers
[
"author"
]
=
author
self
.
headers
[
"date"
]
=
"2024-12-19"
Update Date
def
add_section
(
self
,
section_title:
str
,
content:
str
)
:
"""Add content section"""
self
.
content_sections
.
append
(
{
"title"
: section_title
,
"content"
: content
}
)
def
render
(
self
)
:
"""Render document"""
(
f
"\n=== {self.headers['title']} ==="
)
(
f
"Author: {self.headers['author']}"
)
(
f
"Date: {self.headers['date']}"
)
(
f
"Style: {self.styles}"
)
for
section
in
self
.
content_sections
:
(
f
"\n## {section['title']}"
)
(
section
[
'content'
]
)
(
"="
50
)
Usage examples
(
"=== Document Template System ==="
)
report_template
=
DocumentTemplate
(
"Standard Report"
)
report_template.
add_section
(
"Introduction"
,
"This is the introduction section of the report."
)
report_template.
render
(
)
(
"\n=== Create Concrete Reports ==="
)
monthly_report
=
report_template.
clone
(
)
monthly_report.
customize
(
"Monthly Sales Report"
,
"Sales Department"
)
monthly_report.
add_section
(
"Sales Data"
,
"This month's sales reached 1 million yuan."
)
monthly_report.
render
(
)
project_report
=
report_template.
clone
(
)
project_report.
customize
(
"Project Progress Report"
,
"Project Manager"
)
project_report.
add_section
(
"Project Progress"
,
"The project is progressing as planned."
)
project_report.
render
(
)
Advantages and Disadvantages of the Prototype Pattern
Advantages
- Performance optimization: Avoid repeatedly performing expensive initialization operations
- Simplify the creation process: The client does not need to know the details of object creation
- Dynamic configuration: Products can be added or removed dynamically at runtime
- Fewer subclasses: No need to create a corresponding subclass for each product
Disadvantages
- Copy complexity: For complex objects with circular references, copying can be complicated
- Memory Usage: If the prototype object is large, copying may consume a lot of memory
- Deep copy overhead: Deep copying may be more time-consuming than creating a new instance
Best practices and notes
1. Choose the appropriate copying method
Example
class
SmartPrototype
(
Prototype
)
:
def
init
(
self
,
data
)
:
self
.
data
=
data
self
.
reference_data
=
[
]
Data that may need to be shared
def
clone
(
self
)
:
"""Smart copy: choose shallow or deep copy as needed"""
new_obj
=
copy
.
copy
(
self
)
Shallow copy the main body
new_obj.
reference_data
=
self
.
reference_data
Share referenced data
new_obj.
data
=
copy
.
deepcopy
(
self
.
data
)
Deep copy important data
return
new_obj
2. Handle circular references
Example
class
Node
(
Prototype
)
:
def
init
(
self
,
value
)
:
self
.
value
=
value
self
.
children
=
[
]
def
add_child
(
self
,
child
)
:
self
.
children
.
append
(
child
)
def
clone
(
self
)
:
"""Handle possible circular references"""
Using deep copy automatically handles circular references
return
copy
.
deepcopy
(
self
)
3. Prototype registry pattern
Example
class
PrototypeRegistry:
"""Prototype registry - manage multiple prototypes"""
def
init
(
self
)
:
self
._prototypes
=
{
}
def
register_prototype
(
self
,
name:
str
,
prototype: Prototype
)
:
"""Register a prototype"""
self
._prototypes
[
name
]
=
prototype
def
unregister_prototype
(
self
,
name:
str
)
:
"""Unregister a prototype"""
if
name
in
self
._prototypes:
del
self
._prototypes
[
name
]
def
clone_prototype
(
self
,
name:
str
)
Prototype:
"""Clone a prototype by name"""
if
name
not
in
self
._prototypes:
raise
ValueError
(
f
"Prototype {name} is not registered"
)
return
self
._prototypes
[
name
]
.
clone
(
)
def
list_prototypes
(
self
)
:
"""List all available prototypes"""
return
list
(
self
._prototypes.
keys
(
)
)
Using the registry
registry
=
PrototypeRegistry
(
)
registry.
register_prototype
(
"basic_car"
,
CarPrototype
(
"Toyota"
,
"Camry"
,
"White"
,
"2.5L"
)
)
registry.
register_prototype
(
"warrior"
,
GameCharacter
(
"Warrior"
,
"Warrior"
)
)
Quickly create objects
new_car
=
registry.
clone_prototype
(
"basic_car"
)
new_warrior
=
registry.
clone_prototype
(
"warrior"
)
Practical Exercise
Now it's your turn! Try the following exercises to reinforce your understanding of the Prototype Pattern:
Exercise 1: Improve the Car Prototype
Modify CarPrototype class, add the following features:
- Record the car's manufacturing date
- Add Vehicle Identification Number (VIN) generation logic
- Implement a method to determine whether two car objects are the same
Exercise 2: Create a Configuration Manager
Design a configuration manager prototype with the following requirements:
- Able to store application configuration information
- Support cloning and customization of configurations
- Provide configuration validation
Exercise 3: Implement a Prototype Cache
Create a prototype system with caching:
- Cache frequently used prototype objects
- Provide cache clearing and update mechanisms
- Support version management of prototypes
Summary
The Prototype Pattern is a very practical design pattern in Python. It creates new objects by copying existing ones and is especially suitable for the following scenarios:
- High object creation cost: When the initialization process for creating a new object is complex or time-consuming
- Need for similar objects: When you need to create multiple similar but slightly different objects
- Dynamic configuration: When an object's configuration may change at runtime
Key points:
- Use
copyThe copy module implements copying functionality - Choose shallow or deep copy according to your needs
- Consider using a prototype registry to manage multiple prototypes
- Pay attention to circular references and memory usage