class ITransportProvider
{
public:
virtual ~ITransportProvider() { }
protected:
virtual void SendData() = 0;
// Concrete TransportProvider will call OnReceiveDataEvent
// virtual void RegisterHandlers(std::function<void()> onReceiveDataEvent);
}
class Device
{
public:
Device(shared_ptr<ITransportProvider> transport)
: m_Transport(transport)
{
// transport->RegisterHandlers(boost::bind(&Device::OnReceiveData, this));
}
void SendData()
{
m_Transport->SendData();
}
// Which design pattern to use to get concrete TransportProvider's OnReceiveData event?
//void OnReceiveData()
//{
//}
private:
shared_ptr<ITransportProvider> m_Transport;
};
我总是在我的 ITransportProvider 中添加一个“RegisterHandlers”,并让 Device 在它的 c'tor 中调用它。我想知道它在 DI/IoC 专家眼中是否正确,并且很想听听所有建议。
编辑:
为了澄清,我问除了上述通过 DI 和观察者模式的方式之外,是否有更好的方式将 TransportProvider 与 Device 解耦。