-
Notifications
You must be signed in to change notification settings - Fork 0
/
singleton-design-P.py
45 lines (34 loc) · 1.08 KB
/
singleton-design-P.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
42
43
44
45
# Singleton Design Pattern
from abc import ABCMeta, abstractmethod
class IPerson(metaclass=ABCMeta):
@abstractmethod
def print_data():
""" Implement in child class"""
class PersonSingleton(IPerson):
__instance = None
@staticmethod
def get_instance():
if PersonSingleton.__instance == None:
print('Error!')
PersonSingleton('Default Name, 0')
return PersonSingleton.__instance
def __init__(self, name, age):
if PersonSingleton.__instance != None:
print(f'Instance: {PersonSingleton.__instance}')
raise Exception('Singleton cannot be instantiated more than once')
else:
self.name = name
self.age = age
PersonSingleton.__instance = self
@staticmethod
def print_data():
print(f'Name: {PersonSingleton.__instance.name}, Age: '
f'{PersonSingleton.__instance.age}')
p = PersonSingleton('Mike', 30)
print(p)
p.print_data()
#exception
# p2 = PersonSingleton('Bob', 30)
p2 = PersonSingleton.get_instance()
print(p2)
p2.print_data()