Search code examples
pythondesign-patternsarchitecturesoftware-design

Which Design Pattern combines an Interface, a Factory, and Data?


Problem

I've tried reading this resource on the naming conventions for common Python design patterns, but I can't find a pattern that matches the class I'm creating.

The closest I could find was the Composite Pattern, although I don't feel that matches my use case.

About the Class

I'm working on a Machine Learning Explainability package. One way of explaining a model is to build a simpler surrogate model that mimics the behaviour of the complex model.

A common way to implement this is the SHAP algorithm, which requires you to sample the dataset that you used to create the original complex model.

How you sample is dependent on a single row of features, which you build the surrogate model around. This makes it useful to have a Sampler object that stores the background dataset and takes a single row as input.

This gives me three strongly related complex objects and one dataset. It makes sense to me to store these in the same place and obfuscate their connections with a simple interface.

However, I don't think this is an interface because the methods won't change to match the objects. The class will not be abstract, and it will hold all the important data.

I'd like to contain the functionality for building the surrogate model, and Sampler inside this class too. So it's partially a Factory design pattern.

Question

Is there a pre-existing name for a class like this? Maybe the BORG design pattern? Or should I have rethink before I create something like this?


Solution

  • It is difficult to advice what patterns should be used without being given code snippets.

    However, let me give a quick overview of what pattern can be applied:

    • Abstract Factory and Builder patterns can be used if you know how objects should be constructed before. I mean, if you want to build a computer, then you know that there should be CPU, HDD, video card, ram. There is a slight difference between them that described perfectly here. Difference between Abstract factory and builder?

    This gives me three strongly related complex objects and one dataset

    • This phrase gave me an idea that a side effect of Decorator pattern can be used here. I mean, if you want to build strongly related objects and add them some behavior, then you can try to apply this pattern

    I mean you can compose objects like you want. Let me show an example via C#:

    var basicBike = new AluminiumBike();
    BikeAccessories upgraded = new SportPackage(basicBike);
    upgraded = new SecurityPackage(upgraded);
    
    Console.WriteLine($"Bike: '{upgraded.GetDetails()}' 
        Cost: {upgraded.GetPrice()}");