Context:
class MyClass:
def __init__(self, ...):
self.algo1: Algo1 = ... # receives `self`
self.algo2: Algo2 = ... # receives `self` too
self.algo1_result: Algo1ReturnType
self.algo2_result: Algo2ReturnType
class Algo1A(Algo1):
...
def execute(self):
...
intermediate_value = ...
...
class Algo2A(Algo2):
...
def execute(self):
...
# needs intermediate_value from Algo1A
...
I have a class that depends on two strategy patterns. The second one is optional, in the sense that the user decides whether they want its output.
However, both algorithms are interlinked in such a way that some implementations of the first necessitate a specific implementation of the second, which in turn depends on some intermediate values of the first.
What I need is a solution to seamlessly provide Algo2A
with this intermediate value.
The most obvious way to me to achieve this is to have a self.intermediate_value
on MyClass
which is set when Algo1A
is called and consumed when Algo2A
is executed. However, this doesn’t feel clean at all, since now MyClass
“knows” about the strategy implementations.
Another way is to have Algo2
be a part of Algo1
, and pass a flag to the latter that would tell it whether to execute the former when the client requires its result. However, these algorithms are conceptually distinct that this approach also doesn’t feel right to me.
Would love to hear your ideas.