3

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?

Connor
  • 867
  • 7
  • 18
  • 4
    Sounds like a very complex idea. Be sure that your implementation needs it before you take this route. SOLID would suggest that a class should do one thing. Why do you think it's necessary to combine all this into one thing? How do the parts interact? – duffymo Mar 10 '23 at 21:36
  • The alternative feels messier to me. A series of functions and objects that are strongly logically connected, but only loosely tied together. It feels like they need a centralised container. – Connor Mar 10 '23 at 21:38
  • 1
    You might be right. Your description does not make the interaction clear in any way. I think the factory can easily stand on its own. Is it necessary for clients to know about all those classes and their interactions? How much does the public interface expose? The less, the better. – duffymo Mar 10 '23 at 21:42
  • Apologies, I was trying to keep it vague to make it general. I'll update the question. – Connor Mar 10 '23 at 21:44
  • 4
    Patterns are targets for refactoring. Get something working and then improve it. Also, sounds like [Strategy](https://en.wikipedia.org/wiki/Strategy_pattern) or [Builder](https://en.wikipedia.org/wiki/Builder_pattern). – Peter Wood Mar 10 '23 at 21:46

1 Answers1

1

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()}");
StepUp
  • 36,391
  • 15
  • 88
  • 148