Naive answer
A first reading leads to the simplest possible design:
- An abstract
Motor class,
- A abstract
HWInterface
- an association between the two, implemented using a (smart?) pointer member from
Motor to HWInterface, and perhaps even a pointer member from HWInterface to Motor if the association needs to be bidrectional.
In practice you'd have a scenario where you'd create separately the interface and the motor and connect the two, or you'd create the motor injecting the interface.
More elaborate answer where not any motor could work with any interface
But reading again, I understand that the motor implementation depends on the kind of interface it should use. So not every motor could work with every interface. Moreover, a motor implementation could need specific functionality of a specialised interface, that the abstract interface does not provide.
The more sophisticated approach would then be to use the bridge pattern. The aim of this pattern is to:
Decouple an abstraction from its implementation so that the two can vary independently.
There are several variants of this pattern, and it is not full clear which would be the most promising in your case. But the basic idea would be to have:
- an abstract
Motor,
- a
MotorImplementation which would rely on an abstract Implementor class that defines the basic services (and components) used by a MotorImplementation, and among others the hardware communication interface.
- The
Implementor classes would then be specialised, one for each different type of interface.
This design lets you specialise further the Motor or its general implementation (e.g. electric motor, diesel motor, warp engine ...) whereas the Implementor could be specialised independently to take care of specific interfaces or components.
This flexibility comes at the cost of some complexity, one of it being the construction of a Motor assembling the different implementor services (e.g. a builder pattern could be used to keep it general enough yet assembling different parts). Moreover if you have many other elements like the interface, you might ending ump in deep interleaved hierarchies that are quite difficult to maintain.
Perhaps the simpler Entity Component system could be another alternative, where the motor would be the system, the hardware interface would be one component, and maybe some other component will be the element of liaison between a certain kind of hardware interface and a certain kind of motors. The advantage of this alternative is that the hierarchy remains very flat, and components are easily interchanged.
GoForward(),GoBackward(), but the implementations will be different, because they will be connected to different hardware motors.IHWComand any other low level interfaces behind an adapter that implementsIMoter.