`Mother’ earned its place as one of TV’s better ensemble comedies

Nine years of laughter and occasional fits of exasperation come to an end starting at 8 p.m. Monday with the two-part finale of “How I Met Your Mother,” the mainstay of the formidable CBS Monday night sitcom lineup and one of the great ensemble shows of contemporary television.

On the surface, ensemble shows seem to make perfect sense: Create five or six characters (preferably single) who share space and oxygen, write some jokes for them to say, and if the audience doesn’t like one character, chances are they’ll like another.

Some stars inevitably shine brighter than others – Rachel Green in “Friends,” Sam Malone in “Cheers,” Sheldon Cooper in “Big Bang Theory”– but it’s a matter of relative luminescence for the most part. It’s hedging your bets, as opposed to banking on one star to carry a show.

Beneath the surface, however, ensemble shows have their own particular challenge: Not only do you have to have the right chemistry between two leading actors, but among other ensemble members as well. If they work together well enough as a group, maybe viewers won’t care that much about the weaker links.

No doubt, creators look to the immortals like “Friends,” “Cheers” and “How I Met Your Mother” to decipher the secret to making them work.

“HIMYM” started out with an artificial framework to nudge us forward week to week: “Kids,” each episode would begin (voiceover by Bob Saget), as future Ted Mosby (Josh Radnor) supposedly told his kids how he met their mother years before.

Over the show’s nine-year run, creators Craig Thomas and Carter Bays occasionally renewed the extended tease, with Ted falling for one guest-starring actress or another.

For a while, Sarah Chalke seemed a sure thing, but wasn’t. That’s where the exasperation came in. The rest of the time, millions of us were perfectly happy with Ted’s indefatigable romanticism, Robin’s (Cobie Smulders) Canadian pride, Barney’s (Neil Patrick Harris) “legen-dary” womanizing, and Marshall and Lily’s (Jason Segal, Alyson Hannigan) attempts at grown-up marriage.

Finally rounding the clubhouse turn, the show introduced us to the girl with the umbrella, played by Cristin Milioti, a charming young actress who nonetheless may not seem right for the role of the mother to you.

That’s probably because, over nine years, we’ve all come up with our ideal mate for Ted. During a visit to the set in January, Milioti was tellingly deferential to her cast-mates and showed her age when she revealed, “I watched all eight seasons in three months. I did binge on it, and it is amazing to have it, like, at the surface, because I cried hysterically, laughed. … I hadn’t seen it, which means nothing because I just saw ‘Pretty Woman’ over the weekend.”

So what is the secret of “HIMYM’s” success? A perfect cast, consistently smart direction by Pamela Fryman, and great writing.

The catch phrases (mostly Barney’s), the running jokes (Robin’s early career as Canadian pop star Robin Sparkles) the central presence of the couch in the living room and that one red-leather booth at MacLaren’s Pub they all share – made the show memorable, but also gave it grounding to strengthen the bond with viewers.

You could assemble many of the elements that made “HIMYM” work for nine years and still come up with junk without the two most important ingredients: Excellent writing and a talented, appealing cast.