design-patterns/structural/flyweight/flyweight.py
2020-10-05 19:34:59 +02:00

19 lines
654 B
Python

import json
from typing import List
class Flyweight:
"""
The Flyweight stores a common portion of the state (also called intrinsic
state) that belongs to multiple real business entities. The Flyweight
accepts the rest of the state (extrinsic state, unique for each entity) via
its method parameters.
"""
def __init__(self, shared_state: List[str]) -> None:
self._shared_state = shared_state
def operation(self, unique_state: List[str]) -> None:
s = json.dumps(self._shared_state)
u = json.dumps(unique_state)
print(f"Flyweight: Displaying shared ({s}) and unique ({u}) state.", end="")