J
Jef Driesen
Hi,
I have the following problem. I have an interface (abstract base class)
to represent a "device":
class IDevice {
virtual Read () = 0;
}
I have a number of concrete devices, that implement this interface. The
typical implementation of a device consist of a protocol part (defines
how to read the data) and a layout part (defines the structure of the
data and thus where to read the data):
class Device : IDevice {
m_protocol;
m_layout;
...
}
Device:evice ()
: m_protocol (...), m_layout (...)
{
...
}
Device::Read ()
{
return m_protocol->Read (m_layout->offset);
}
But now I want to implement some devices which are very similar. For
instance two devices sharing the same layout, but using a different
protocol. What is the preferred way to implement this, without
duplicating all code?
Basically, only the constructor is different. All other member functions
(such as Read) have exactly the same implementation.
ADevice::ADevice ()
: m_protocol (new AProtocol (...)), m_layout (new Layout (...))
{
...
}
BDevice::BDevice ()
: m_protocol (new BProtocol (...)), m_layout (new Layout (...))
{
...
}
Thanks in advance,
Jef
I have the following problem. I have an interface (abstract base class)
to represent a "device":
class IDevice {
virtual Read () = 0;
}
I have a number of concrete devices, that implement this interface. The
typical implementation of a device consist of a protocol part (defines
how to read the data) and a layout part (defines the structure of the
data and thus where to read the data):
class Device : IDevice {
m_protocol;
m_layout;
...
}
Device:evice ()
: m_protocol (...), m_layout (...)
{
...
}
Device::Read ()
{
return m_protocol->Read (m_layout->offset);
}
But now I want to implement some devices which are very similar. For
instance two devices sharing the same layout, but using a different
protocol. What is the preferred way to implement this, without
duplicating all code?
Basically, only the constructor is different. All other member functions
(such as Read) have exactly the same implementation.
ADevice::ADevice ()
: m_protocol (new AProtocol (...)), m_layout (new Layout (...))
{
...
}
BDevice::BDevice ()
: m_protocol (new BProtocol (...)), m_layout (new Layout (...))
{
...
}
Thanks in advance,
Jef