design-patterns/structural/flyweight/flyweight.py
Ruidy 664b39c32e
Flyweight Pattern (#12)
* edit documentation

* add code example

Co-authored-by: Ruidy <r.nemausat@empfohlen.de>
2020-09-27 21:25:55 +02:00

20 lines
668 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="")