Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for generating events? [closed]

I want to design an event generating/consuming for a system, There would be various types of event occurrences in various situations. What I found is I need to write the event generating code everywhere and the event generating code is tightly coupled with the business logic.

For example, for an event of object change, I wrote some code in all the methods that changes that object, and the methods that change the object spread in many place in project.

As another example, for each object, there is a changed event for which I have to write event generating code for all objects.

I believe you guys have some experience in solving the problems, and implement a loosely-coupled and easy-to-maintain system. Do you have any suggestion?

My implementation language is Java, but I think this problem is valid for any language, if any Java specific solution is also welcome :)

Thank you!

like image 386
sevenever Avatar asked Dec 22 '22 00:12

sevenever


1 Answers

You might want to have a look at google guava's EventBus mechanism. It's a nice and easy way to decouple much of the event handler registering boilerplate from your objects.

And if you want to see it language agnostic, event bus is just a pattern describing a publish/subscribe event dispatching mechanism.

like image 101
nansen Avatar answered Dec 28 '22 07:12

nansen