mirror of
https://github.com/rjNemo/design-patterns
synced 2026-06-06 02:26:40 +00:00
* doc: create package & update general TOCs * doc: add documentation * add code example * reformat using black
19 lines
654 B
Python
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="")
|