I have the following classes in my system (python):
class Metric(ABC): def compute(self): -> float class Visualization(ABC) def visualize(self) class FirstMetric(Metric) def __init__(self, data1): def compute(self): -> float class FirstVisualization(Visualization) def __init__(self, plot_param1, data1) def visualize(self) class SecondMetric(Metric) def __init__(self, data1, data2): def compute(self): -> float class SecondVisualization(Visualization) def __init__(self, plot_param1, plot_param2, data1, data2) def visualize(self) I have one Metric and one Visualization subclass for each different implementation. As you can see in the code, each of those two classes require the same data, only visualization requires extra plotting parameters. When I instantiate them, I never instantiate objects from different catalogs, i.e, I would never create a FirstMetric and a SecondVisualization together.
Since I always generate pairs Metric-Visualization, and they have similar inputs, I was wondering if it would make sense to use the Abstract Factory design pattern. It would look like this:
class AbstractFactory(ABC): def create_metric(self) -> Metric def create_visualization(self) -> Visualization class FirstFactory(AbstractFactory): def __init__(self, data1, plot_param1): self.data1 = data1 self.plot_param1 = plot_param1 def create_metric(self): return FirstMetric(self.data1) def create_visualization(self): return FirstVisualization(self.data1, self.plot_param1) class SecondFactory(AbstractFactory): def __init__(self, data1, data2, plot_param1, plot_param2): self.data1 = data1 self.data2 = data2 self.plot_param1 = plot_param1 self.plot_param2 = plot_param2 def create_metric(self): return SecondMetric(self.data1, self.data2) def create_visualization(self): return SecondVisualization(self.data1, self, data2, self.plot_param1, self.plot_param2) I also wonder if this is "correct", since each Metric and Visualization subclass have different parameters in the constructor. Here, I am using the constructor of the Factory to solve this problem. All examples I have seen of AbstractFactory receive the parameters as arguments on the create_... methods, so maybe I am doing it wrong. Would you use this pattern here? If so, what would be the best implementation? If not, what would be a better solution?
AbstractFactory. I understand that this is normalAbstractFactorypublic interface, vs what to pass through concrete factory constructors or obtain in some other way.