Let's say I have an entity class ListOfItems
with many related entities Item
. These items are processed in a ItemsProcessor
class that encapsulates the behavior. This class has several control lists and variable, for example, a processed_items
list. Every time an item is done processing, it is added to that list:
class ItemsProcessor:
def __init__(self, list_of_items):
self.items = list_of_items.items
self.processed_items = []
def process(self):
for item in self.items:
if self._process_item(item):
self.processed_items.append(item)
But now let's say we have a secondary class ItemsProcessorAlt
that encapsulates additional processing. It needs a reference to the ItemsProcessor
because it holds the state of the process, among other data that it uses:
class ItemsProcessorAlt:
def __init__(self, processor):
self.parent_processor = processor
def process(self):
for item in self.parent_processor.items:
self._process_item_alt(item)
And this is how the main processor would use the alternative one:
class ItemProcessor:
def process(self):
# process all items as shown above
if self._corrupt_items():
subprocessor = ItemsProcessorAlt(self)
subprocessor.process()
Now, when an item is processed by this last class, the state of the process needs to be updated, i.e. the processed_items
list. Whose responsibility is it to update this state?:
- Should
ItemsProcessorAlt.process()
update those lists directly? - Should ItemsProcessor
define a method to update the state and then
ItemsProcessorAlt.process()` call it? - Should
ItemsProcessorAlt.process()
have a return value that hints the main processor how the state should be updated? - Should
ItemsProcessorAlt
set up a state on its own so that when the subprocess is finished the main processor updates its state based on that?
Note that the subprocessor is using variables that lie in the main processor and are changing as the process/subprocess is executed.