Вот упрощенная версия моего кода:
Код: Выделить всё
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .department_store import DepartmentStore
class BaseStore:
def navigate_to_department(self, dept_id: str) -> "DepartmentStore":
# Import inside method to avoid circular import
from .department_store import DepartmentStore
return DepartmentStore(dept_id, self.inventory_system)
Код: Выделить всё
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .section_store import SectionStore
class DepartmentStore:
def __init__(self, dept_id: str, inventory_system):
self.dept_id = dept_id
self.inventory_system = inventory_system
def navigate_to_section(self, section_id: str) -> "SectionStore":
# Import inside method to avoid circular import
from .section_store import SectionStore
return SectionStore(self.dept_id, section_id, self.inventory_system)
Код: Выделить всё
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .department_store import DepartmentStore
class SectionStore:
def __init__(self, dept_id: str, section_id: str, inventory_system):
self.dept_id = dept_id
self.section_id = section_id
self.inventory_system = inventory_system
def navigate_to_parent_department(self) -> "DepartmentStore":
# Import inside method to avoid circular import
from .department_store import DepartmentStore
return DepartmentStore(self.dept_id, self.inventory_system)
- может перейти в универмаг (например, электроника, одежда)
Код: Выделить всё
BaseStore - может перейти в магазин разделов (например, телевизоры в разделе «Электроника»)
Код: Выделить всё
DepartmentStore - может вернуться в родительский магазин DepartmentStore
Код: Выделить всё
SectionStore
- Использование TYPE_CHECKING для подсказок типов
- Импорт необходимых классов внутри методов
Некоторые идеи, которые я рассмотрел:
- Перемещение всего хранилища классы в один файл (неудобно с точки зрения удобства обслуживания)
- Создание отдельного класса фабрики хранилища (кажется, перепроектирование)
- Какое-то использование внедрения зависимостей
- Какое-то использование внедрения зависимостей
- li>
Создание абстрактного интерфейса навигации
Подробнее здесь: https://stackoverflow.com/questions/792 ... ent-system
Мобильная версия