I have a lot of
Command objects. Each command has lots of
Task objects. Tasks and commands are related. Each command wants to know which task belongs to it. Where each task wants to know which command it belongs to.
class Command: def __init__(self, date): self.deadline = date self.tasks =  def add_task(self, task:Task): """ add a task to this command """ self.tasks.append(task) return task def has_task(self, task): return task in self.tasks def remove_task(self, task): """ Remove the task from this command. After calling this, the task is orphaned and the reference to the task should be discarded. """ self.tasks.remove(task) class Task: def __init__(self, command:Command, duration:int): self.duration = duration self._command = command @property def command(self): """ Getter for self._command with sanity checking that the task is not orphaned. """ if not self._command.has_task(self): raise ValueError("orphaned task") return self._command class Problem: def __init__(self, commands:Dict[str,Command], tasks:Dict[str,Task]): self._commands = commands self._tasks = tasks
Problem object I feel there is some redundancy in the data between
I fix starting times in two ways:
- Fix the start time of tasks command by command.
This is why is a
- Merge them and then fix the starting times.
This is why there is
I need both ways to fix starting times. However, fixing the starting time of a task always depends on the deadline of the command to which it belongs.
I'm unsure if my design is good. Sometimes I need to go through the tasks command by command. Sometimes I need to consider all the tasks for the command they belong.
Is there a design pattern I can use?