-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathflyweight_factory.py
More file actions
24 lines (18 loc) · 706 Bytes
/
flyweight_factory.py
File metadata and controls
24 lines (18 loc) · 706 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
"Creating the FlyweightFactory as a singleton"
from flyweight import Flyweight
class FlyweightFactory():
"Creating the FlyweightFactory as a singleton"
_flyweights: dict[str, Flyweight] = {} # Python 3.9
# _flyweights = {} # Python 3.8 or earlier
def __new__(cls):
return cls
@classmethod
def get_flyweight(cls, code: str) -> Flyweight:
"A static method to get a flyweight based on a code"
if not code in cls._flyweights:
cls._flyweights[code] = Flyweight(code)
return cls._flyweights[code]
@classmethod
def get_count(cls) -> int:
"Return the number of flyweights in the cache"
return len(cls._flyweights)