FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Fix Typing module and format] (ID тикета: 3672) by isaevmik · Pull Request #12 · RefactoringGuru/design-patterns-python · GitHub

Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
51 changes: 27 additions & 24 deletions src/Flyweight/Conceptual/main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
между собой, вместо хранения одинаковых данных в каждом объекте.
"""


import json
from typing import Dict
from typing import Dict, List


class Flyweight():
class Flyweight:
"""
EN: The Flyweight stores a common portion of the state (also called
intrinsic state) that belongs to multiple real business entities. The
Expand All @@ -30,16 +29,16 @@ class Flyweight():
для каждого объекта) через его параметры метода.
"""

def __init__(self, shared_state: str) -> None:
def __init__(self, shared_state: List) -> None:
self._shared_state = shared_state

def operation(self, unique_state: str) -> None:
def operation(self, unique_state: List) -> None:
s = json.dumps(self._shared_state)
u = json.dumps(unique_state)
print(f"Flyweight: Displaying shared ({s}) and unique ({u}) state.", end="")


class FlyweightFactory():
class FlyweightFactory:
"""
EN: The Flyweight Factory creates and manages the Flyweight objects. It
ensures that flyweights are shared correctly. When the client requests a
Expand All @@ -54,11 +53,11 @@ class FlyweightFactory():

_flyweights: Dict[str, Flyweight] = {}

def __init__(self, initial_flyweights: Dict) -> None:
def __init__(self, initial_flyweights: List) -> None:
for state in initial_flyweights:
self._flyweights[self.get_key(state)] = Flyweight(state)

def get_key(self, state: Dict) -> str:
def get_key(self, state: List) -> str:
"""
EN: Returns a Flyweight's string hash for a given state.

Expand All @@ -67,7 +66,7 @@ def get_key(self, state: Dict) -> str:

return "_".join(sorted(state))

def get_flyweight(self, shared_state: Dict) -> Flyweight:
def get_flyweight(self, shared_state: List) -> Flyweight:
"""
EN: Returns an existing Flyweight with a given state or creates a new
one.
Expand All @@ -93,8 +92,12 @@ def list_flyweights(self) -> None:


def add_car_to_police_database(
factory: FlyweightFactory, plates: str, owner: str,
brand: str, model: str, color: str
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])
Expand All @@ -115,22 +118,22 @@ def add_car_to_police_database(
на этапе инициализации приложения.
"""

factory = FlyweightFactory([
["Chevrolet", "Camaro2018", "pink"],
["Mercedes Benz", "C300", "black"],
["Mercedes Benz", "C500", "red"],
["BMW", "M5", "red"],
["BMW", "X6", "white"],
])
car_factory = FlyweightFactory(
[
["Chevrolet", "Camaro2018", "pink"],
["Mercedes Benz", "C300", "black"],
["Mercedes Benz", "C500", "red"],
["BMW", "M5", "red"],
["BMW", "X6", "white"],
]
)

factory.list_flyweights()
car_factory.list_flyweights()

add_car_to_police_database(
factory, "CL234IR", "James Doe", "BMW", "M5", "red")
add_car_to_police_database(car_factory, "CL234IR", "James Doe", "BMW", "M5", "red")

add_car_to_police_database(
factory, "CL234IR", "James Doe", "BMW", "X1", "red")
add_car_to_police_database(car_factory, "CL234IR", "James Doe", "BMW", "X1", "red")

print("\n")

factory.list_flyweights()
car_factory.list_flyweights()

Back | FazBrowse Home | New Git URL