Skip to content

Repository files navigation

Implementing the Prototype Design Pattern

  • Prototype is a creational design pattern.

This pattern focuses on simplifying object cloning, reducing the overhead of creating new instances. The project uses cloned objects while preserving the state and data of the original object.

Examples of Dunder Methods Used in the Prototype Design Pattern in Python

1. __class__ – Returns the object’s class type

class Mage:
    def __init__(self, name: str, magic_type: str) -> None:
        self.name = name
        self.magic_type = magic_type

mage = Mage(
    name="Robertão, The Mage",
    magic_type="Fire"
)

print(object.__class__)
print(mage.__class__)

# Output:
# <class 'type'>
# <class '__main__.Mage'>

2. __new__ – Object constructor method

class Mage:
    def __init__(self, name: str, magic_type: str) -> None:
        self.name = name
        self.magic_type = magic_type

mage = Mage(
    name="Robertão, The Mage",
    magic_type="Fire"
)

mage_copy = mage.__new__(mage.__class__)
print(mage_copy)

# Output:
# <__main__.Mage object at 0x706ff6a5d590>

3. __dict__ – Returns a dictionary of an object’s attributes

class Mage:
    def __init__(self, name: str, magic_type: str) -> None:
        self.name = name
        self.magic_type = magic_type

mage = Mage(
    name="Robertão, The Mage",
    magic_type="Fire"
)

print(Mage.__dict__)
print(mage.__dict__)  # or print(vars(mage))

# Output:
# {'__module__': '__main__', '__init__': <function Mage.__init__ at 0x733dde3f42c0>, ...}
# {'name': 'Robertão, The Mage', 'magic_type': 'Fire'}

4. __dir__ – Lists all attributes and methods of an object

print(mage.__dir__())

# Output:
# ['name', 'magic_type', '__module__', '__init__', '__dict__', '__weakref__', ...]

5. __reduce__ and __reduce_ex__ – Used for object serialization

import pickle

class Mage:
    def __init__(self, name: str, magic_type: str) -> None:
        self.name = name
        self.magic_type = magic_type

mage = Mage(
    name="Robertão, The Mage",
    magic_type="Fire"
)

print(mage.__reduce__()) # Returns values like the object's constructor, its value as a dict(), similar to __dict__, and the values of __class__.

print(mage.__reduce_ex__(4)) # Similar to __reduce__, but with versioning, allowing it to return more values and existing ones with different names depending on the chosen version passed as an argument.

# Output:
# (<function _reconstructor at 0x769819a1b920>, (<class '__main__.Mage'>, <class 'object'>, None), {'name': 'Robertão, The Mage', 'magic_type': 'Fire'})
# (<function __newobj__ at 0x769819a1ba60>, (<class '__main__.Mage'>,), {'name': 'Robertão, The Mage', 'magic_type': 'Fire'}, None, None)

Dunder Methods Used in Prototype Design Pattern

6. __copy__ – Used when calling copy() from the copy module

from copy import copy

class Mage:
    def __init__(self, name: str, magic_type: str) -> None:
        self.name = name
        self.magic_type = magic_type

    def __copy__(self):
        pass  # This method is called when using `copy()`

mage = Mage(name="Robertão, The Mage", magic_type="Fire")
mage_copy = copy(mage)

7. __deepcopy__ – Used when calling deepcopy() from the copy module

from copy import deepcopy

class Mage:
    def __init__(self, name: str, magic_type: str) -> None:
        self.name = name
        self.magic_type = magic_type

    def __deepcopy__(self, memo=None):
        pass  # This method is called when using `deepcopy()`

mage = Mage(name="Robertão, The Mage", magic_type="Fire")
mage_copy = deepcopy(mage)

Reference Material

For further learning, refer to:


About

Repository created for notes on the Prototype Design Pattern so that I can refer to my notes and implementation models here when I use them in the future.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages