Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Prototype Pattern

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

       

print

(

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:


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"""

       

print

(

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)}"

       

print

(

info

)

Usage examples

Example

Create the prototype object

print

(

"=== 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

(

)

print

(

"\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

print

(

"\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"""

       

print

(

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"""

       

print

(

f

"Character: {self.name} ({self.character_class}) - Level: {self.level}"

)

       

print

(

f

"Skills: {', '.join(self.skills)}"

)

       

if

self

.

equipment

:

            equipment_str

=

', '

.

join

(

[

f

"{k}: {v}"

for

k

,

v

in

self

.

equipment

.

items

(

)

]

)

           

print

(

f

"Equipment: {equipment_str}"

)

Usage examples

print

(

"=== 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

(

)

print

(

"\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"""

       

print

(

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"""

       

print

(

f

"\n=== {self.headers['title']} ==="

)

       

print

(

f

"Author: {self.headers['author']}"

)

       

print

(

f

"Date: {self.headers['date']}"

)

       

print

(

f

"Style: {self.styles}"

)

       

for

section

in

self

.

content_sections

:

           

print

(

f

"\n## {section['title']}"

)

           

print

(

section

[

'content'

]

)

       

print

(

"="

50

)

Usage examples

print

(

"=== Document Template System ==="

)

report_template

=

DocumentTemplate

(

"Standard Report"

)

report_template.

add_section

(

"Introduction"

,

"This is the introduction section of the report."

)

report_template.

render

(

)

print

(

"\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

  1. Performance optimization: Avoid repeatedly performing expensive initialization operations
  2. Simplify the creation process: The client does not need to know the details of object creation
  3. Dynamic configuration: Products can be added or removed dynamically at runtime
  4. Fewer subclasses: No need to create a corresponding subclass for each product

Disadvantages

  1. Copy complexity: For complex objects with circular references, copying can be complicated
  2. Memory Usage: If the prototype object is large, copying may consume a lot of memory
  3. 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:

Exercise 2: Create a Configuration Manager

Design a configuration manager prototype with the following requirements:

Exercise 3: Implement a Prototype Cache

Create a prototype system with caching:


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:

Key points: