0

I have device interface classes written in c# for electronic variable loads. They are mostly for DC devices. We got a new one that does loading for both AC and DC devices. I also have an interface named IVaraibleLoad that lets me interact with them generically and group them if we are using several in parallel. From the perspective of the caller, interacting with an AC variable load is the same as interacting with a DC variable load. I would like to make two new interfaces IAcVariableLoad and IDcVaraibleLoad that each inherit from IVaraibleLoad.

I do not think I should make the AC/DC mode part of the interface because most devices do not support the AC mode. I am stuck trying to understand how to make this happen.

If the above does not make sense, I will try to give a more generic example below. A similar situation might be explained using the standard printer setup. Assume I have several classes that each provide the low level functionality for an individual printer model, e.g., PrinterModelA, PrinterModelB, and PrinterModelC. I have an interface called ICanPrint which just exposes the bare minimum an external caller would need to print a text document. (This is where the analogy starts to break down.) Imagine that all these printer models implement ICanPrint. Now we introduce an update and PrinterModelC has the functionality to print a picture or a text document. To mirror my situation I make two new interfaces ICanPrintText and ICanPrintPhotos which both implement ICanPrint. Now I have PrinterModelA : ICanPrintText | PrinterModelB : ICanPrintText | PrinterModelC : ICanPrintText, ICanPrintPhotos.

Is there a better way to structure this?

4
  • For the IVariableLoad, I now don't understand why you've introduced a separate IVariableDcLoad and IVariableAcLoad. Do those interfaces provide any AC or DC-specific behaviors? Is it better instead to have an abstract base class implementing most of the behaviors of an IVariableLoad, and then the AC/DC part specifically is implemented by subclasses? Nov 13, 2020 at 22:45
  • @BryanBoettcher The interface for the caller is the same for AC and DC. However, the actions taken under the hood (by the class that implements the interface) are different. There is nothing common about what happens in the implementation. However, from the perspective of the caller the outcome is the same. Nov 13, 2020 at 22:52
  • 1
    "The interface for the caller is the same for AC and DC" - what is your motivation for introducing these new interfaces then? If the callers don't need to treat these differently, then there's no reason to have them. Are you anticipating future callers that will break this pattern? "I am stuck trying to understand how to make this happen" - you didn't say what you're trying to do, though. How to make what happen? I'm guessing you're not asking how to declare that one interface inherits from another, but how to design that interface for some usage scenario you have in mind. Nov 13, 2020 at 23:27
  • I think this could be answered if you focus on the AC/DC aspect or the printer aspect. Otherwise it is too broad. Nov 16, 2020 at 22:15

1 Answer 1

3

Interfaces are for callers, not for categorization. If the perspective is truly the same from the caller point of view, then you should just have the IVariableLoad.

If there are callers who do care whether it's AC or DC, then most likely you want to add a second, non-inherited interface with just the AC-specific stuff. The only reason to make it inherited would be if a caller ever needs to do both non-specific stuff and AC-specific stuff. It would be preferable to do that with intersection types, but I don't think C# has those yet.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.