Answer the question
In order to leave comments, you need to log in
Designing two similar classes that share many (but not all) properties and methods?
The situation is this.
There are two printers from different manufacturers. We want the top level to know nothing about them and just use the class with a single set of methods and properties.
I start highlighting the interface.
public interface IPrinterProvider {
bool Connect(string comPort);
bool IsConnected();
}
public class CommonPrinterProvider {
private IPrinterProvider printerProvider;
public CommonPrinterProvider(IPrinterProvider printerProvider) {
this.printerProvider= printerProvider;
}
}
Answer the question
In order to leave comments, you need to log in
An abstract class is not considered as a parent? As far as I understand, a lot of code will be common, the interface is perfectly replaced by an abstract class + how many heirs are needed.
Could it be something like this? the interface is common, it's just that one printer will have to override methods to call a new one with the password passed. I ideally it will be able to use the methods of the base abstract class.
Code on pastebin
The question is, where will this password come from? It can be passed by the same “top level” that will send commands to the printer or some other object that will configure the printer object; as an option - a factory object. This object can be hardcoded for configuration for different implementations of the interface; an alternative is to take as a generalization that there may or may not be a password, in which case we pass an empty string, e.g. A particular implementation may or may not use this password. Total we have:
A factory for receiving settings should be injected into a specific printer, or the settings should be directly injected right away. It is not advisable to allocate any passwords to the interface of the printer itself. To use a password or not to use it is a matter of connection with the entity "printer settings" or something like that, but not the "printer" itself. The printer itself should only be able to print directly, and even then, probably, delegate to the "printing part of the printer" or the like.
The basis for identifying a common ancestor is a common responsibility, a common behavior. In this case, "password present/password not present" is neither a shared responsibility nor a shared behavior, but just one of the dependencies needed by only one of the printer types.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question