there is a large c++ project that receives data from outside and after small processing, new data are sent to client code (within the app). now all the client code implements lots of interfaces (e.g. IOnConcreteDataReceived) and pointers to these interfaces are set to the part that receives data from outside.
Event dispatcher looks like a good choice for code decoupling. I would like to get the following code:
// dispatcher global access
IEventDispatcher & getEventDispatcher();
//producer:
ConcreteDataType data{values};
getEventDispatcher().fireEvent<ConcreteDataTypeEvent>(data);
//consumer:
getEventDispatcher().subscribe<ConcreteDataTypeEvent>([](const ConcreteDataType & data){ processData(data);});
As far as in most cases the data types are different the simplest solution that comes to mind is to use data types as event types:
//producer:
ConcreteDataType data{values};
getEventDispatcher().fireEvent<ConcreteDataType>(data);
//consumer:
getEventDispatcher().subscribe<ConcreteDataType>([](const ConcreteDataType & data){ processData(data);});
The only thing that i do not like about this variant is the usage of data types as event types. It does not look intuitive
Separation of event types from data types leads to more complicated code. there should be an association between event types and data types, possibly in a central place.
The question: Is the purity of design (separated event types and data types) worth additional efforts or usage data types as event types is not as bad as i think of it? How to implement compile-time checks for event type - data type association?
Aucun commentaire:
Enregistrer un commentaire