Skip to the guide

Events and notifications inside one program

IntermediatePattern7 pages

Many LLD prompts end with "and these other things need to know". This move announces each change as an event, a small object that records what has already happened, through a bus inside one program. It combines Observer for the telling with the idea of Pub/sub, where senders and receivers know only the message, and it adds a small queue for slow work. Interviewers listen for when the event goes out, and for what must not run while the caller waits.

The announcer at a football stadium

Think of a football stadium when the ball hits the net. The referee checks the video first, because the goal may not stand. Only when it stands does the announcer speak, once, over the loudspeakers: "Goal, number 9." He has no idea who is listening.

The scoreboard operator changes the score at once, since everyone is looking up. The highlights editor needs ten minutes to cut the clip, so she writes a note on her pile and goes back to it later. If the scoreboard breaks, the announcer and the match carry on. And if that goal was the player's third, the "hat trick" line waits until the first announcement has finished.

In code, the referee's check is BikeShare.join, this page's sign-up for a city's shared bicycles, and "the goal stands" means the fee is paid and the member is saved. The announcer and his loudspeakers are EventBus. "Goal, number 9" is the event MemberJoined. The scoreboard operator is the handler FreeRideCredits, and the editor is WelcomeMailer, with MailQueue as her pile. The hat trick line is CreditGranted, waiting its turn in the bus.