Exercises in Programming Style–Bulletin Board

NOTE : read the rest of the series, or check out the source code.

If you enjoy read­ing these exer­cises then please buy Crista’s book to sup­port her work.

exercises-prog-styles-cover

Fol­low­ing on from the last post, we will look at the Bul­letin Board style today.

 

Style 15 – Bulletin Board

Constraints

  • Larg­er prob­lem is decom­posed into enti­ties using some form of abstrac­tion
  • The enti­ties are nev­er called on direct­ly for actions
  • Exis­tence of an infra­struc­ture for pub­lish­ing and sub­scrib­ing to events, aka the bul­letin board
  • Enti­ties post events sub­scrip­tions to the bul­letin board, and pub­lish events to the bul­letin board. The bul­letin board infra­struc­ture does all the event man­age­ment and dis­tri­b­u­tion

 

If you’ve done any WPF work in the past (or in the present) then you might rec­og­nize this style as the Weak Event pat­tern.

Whilst sim­i­lar in design to the Hol­ly­wood style, this style fur­ther decou­ples the enti­ties with an Event­Man­ag­er that sits between them all.

So let’s start by defin­ing what a han­dler and event man­ag­er looks like:

Style15_01

Unfor­tu­nate­ly we have to for­feit some type safe­ty here as we uni­fy dif­fer­ent events under the same umbrel­la. Here’s the imple­men­ta­tion for the IEvent­Man­ag­er inter­face:

Style15_02

The 3 enti­ties — DataS­tor­age, Stop­Words­Fil­ter and Word­Fre­quen­cy­Counter — are sim­i­lar to those in the Hol­ly­wood style, except they now sub­scribe and pub­lish events through the afore­men­tioned IEvent­Man­ag­er abstrac­tion.

Oh, and we’ll stick with the RunArgs type we cre­at­ed last time:

Style15_03

Style15_04

Style15_05

Final­ly, to put every­thing togeth­er:

Style15_06

 

You can find the source code for this exer­cise here.