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
42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
from structural.flyweight.flyweight_factory import FlyweightFactory
|
|
|
|
|
|
def add_car_to_police_database(
|
|
factory: FlyweightFactory,
|
|
plates: str,
|
|
owner: str,
|
|
brand: str,
|
|
model: str,
|
|
color: str,
|
|
) -> None:
|
|
|
|
print("\n\nClient: Adding a car to database.")
|
|
flyweight = factory.get_flyweight([brand, model, color])
|
|
# The client code either stores or calculates extrinsic state and passes it
|
|
# to the flyweight's methods.
|
|
flyweight.operation([plates, owner])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
"""
|
|
The client code usually creates a bunch of pre-populated flyweights in the
|
|
initialization stage of the application.
|
|
"""
|
|
factory = FlyweightFactory(
|
|
[
|
|
["Chevrolet", "Camaro2018", "pink"],
|
|
["Mercedes Benz", "C300", "black"],
|
|
["Mercedes Benz", "C500", "red"],
|
|
["BMW", "M5", "red"],
|
|
["BMW", "X6", "white"],
|
|
]
|
|
)
|
|
|
|
factory.list_flyweights()
|
|
|
|
add_car_to_police_database(factory, "CL234IR", "James Doe", "BMW", "M5", "red")
|
|
add_car_to_police_database(factory, "CL234IR", "James Doe", "BMW", "X1", "red")
|
|
|
|
print("\n")
|
|
|
|
factory.list_flyweights()
|