-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton.py
More file actions
31 lines (22 loc) · 914 Bytes
/
Copy pathsingleton.py
File metadata and controls
31 lines (22 loc) · 914 Bytes
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
class Borg:
"""The Borg design pattern"""
"""Borg class making class attributes global"""
_shared_state = {} # Attribute dictionary
def __init__(self):
# Make it an attribute dictionary
self.__dict__ = self._shared_state
class Singleton(Borg):
"""The singleton class"""
"""This class now shares all its attributes among its various instances"""
# This essentially makes the singleton objects an object-oriented global variable
def __init__(self, **kwargs):
Borg.__init__(self)
#Update the attribute dictionary by inserting a new key-value pair
self._shared_state.update(kwargs)
def __str__(self):
# Returns the attribute dictionary for printing
return str(self._shared_state)
x = Singleton(HTTP="Hyper Text Transfer Protocol")
print(x)
y = Singleton(SNMP = "Simplle Network Management Protocol")
print(y)