-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototype.py
41 lines (30 loc) · 998 Bytes
/
prototype.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# --------------------------------------------------------
# Licensed under the terms of the BSD 3-Clause License
# (see LICENSE for details).
# Copyright © 2018-2024, A.A Suvorov
# All rights reserved.
# --------------------------------------------------------
# https://github.com/smartlegionlab/
# --------------------------------------------------------
"""Prototype"""
import copy
class Prototype:
def __init__(self):
self._objects = {}
def register(self, name, obj):
self._objects[name] = obj
def unregister(self, name):
del self._objects[name]
def clone(self, name, attrs):
obj = copy.deepcopy(self._objects[name])
obj.__dict__.update(attrs)
return obj
class Bird:
"""Bird"""
def main():
prototype = Prototype()
prototype.register('Bird', Bird())
duck = prototype.clone('Bird', {'name': 'Duck'})
print(type(duck), duck.name) # <class '__main__.Bird'> Duck
if __name__ == '__main__':
main()