mdk.fr/content/blog/the-art-of-events.md

15 lines
819 B
Markdown

Title: The art of Events
The art of using events to build more independent classes. /\* Found a
better example \*/ Imagine you have a class A and a class B. A builds B,
and B have to communicate with A (call methods ... ?) Some developers
(Boooo (I've done it ... (Booo >> me))) will pass a reference of A
to B "A(){B my\_B = new B(this);}" /\* Please note the newB private joke
\*/ But what happens when you want an other class, C, who don't know A
to build B ? So we have to remove the A's reference in the B ctor. How B
should communicate now ? - Using Events ! B exposes an event, the
builder of B can register on it. - B can speak to A throwing this event.
- Every class can build a B, and can, if it needs, register on its
events, doing whatever he wants when the event is thrown. B is now fully
reusable!