jeudi 20 septembre 2018

What is a good design pattern (if at all existing) for the following situation

Been breaking my head over trying to find an elegant solution for the following (and I'm obscuring and changing some details here, but the situation I'll describe is equivalent):

I have a list of computers and a list of printers in an object in memory (actually it's a big xml document, with among others computers and printers in it), so basically a class Peripherals with a list and list as properties. The properties inside Computer and Printer are quite different, and no inheritance there is possible. Now, I'm writing a selector whose job it is to take an incoming argument of type XDocument (that big list), and selecting one computer and one printer to remain in that XDocument, and return the updated xml.

To make the choice, I can send in some helper arguments. Either some Options (but options for Computer and Printer are different, so I have ComputerOptions and PrinterOptions types), or Preselection (which holds the preferences of a customer, but I can have ComputerPreselection as well as PrinterPreselection).

Now the thing is, the internal flow of this selector is such that IF a caller has a Preselection for a computer and/or a printer, I don't need the options, and the other way around.

So my current idea is this:

public interface ISelector
{
    XDocument Select(XDocument peripherals, ComputerSelectionOptions computerOptions, PrinterSelectionOptions printerOptions);
    XDocument Select(XDocument peripherals, ComputerSelectionOptions computerOptions, PrinterPreselection printerPreselection);
    XDocument Select(XDocument peripherals, ComputerPreselection computerPreselection, PrinterSelectionOptions printerOptions);
    XDocument Select(XDocument peripherals, ComputerPreselection computerPreselection, PrinterPreselection printerPreselection);
}

Now, I think you can agree, that if we have more peripherals in the future, this list of functions will exploded. Let's say the xml doesn't only contain computers and printers, but also smartphones and all that stuff. The api will explode. At the same time, the fact is that the ComputerPreselection and PrinterPreselection types are completely different, and the same goes for the ComputerOptions and PrinterOptions.

Inside the Selector component, I already have classes that are only responsible for the Preselection process of a Computer/Printer, or the Selection of Computer/Printer using the Options, depending on which overload you select, so that part is already delegated to custom handlers doing preselection or selection based on options.

I'm quite familiar with several design patterns, but I'm a bit stuck since those Preselection and Option models are so vastly different (basically don't share anything), so what is an elegant way of solving this, so that I'm safeguarded to some extent by a possible explosion of overloads. If delegation/design patterns can't help me, are overloads really my only option?

Aucun commentaire:

Enregistrer un commentaire