While teaching C++ for novice software architects, I’m happy to see their willing to employ various OO design patterns to streamline and strengthen class-level architecture of academic projects they implement. Unsurprisingly, the first and foremost area of their interest is all around «Gang-of-Four» (GoF) patterns described as early as 1994 by Erich Gamma et al. in their world-known book.
Everyone who’s new to Gamma gets almost always confused about quantity and diversity of GoF patterns. Where to start? Besides recommendations kindly given by the book authors, it seems promising to take a look at projects made by, say, unseasoned practitioners. What’s used often and why?
As I can judge from my own experience and my students’ code base, top 5 GoF patterns could look as follows (well, three of five items go in pairs as long as they can handle slightly different OO design problems):
- State / Strategy: Allows to abstract and dynamically substitute different concrete algorithms to solve the same domain problem.
- Composite / Decorator: Provides a unified ADT interface for aggregates and leaf entities with or without some additional features.
- Bridge: Separates interface classes from implementation classes and makes them both interchangeable.
- Abstract factory / Builder: Encapsulates logic for creating complex problem-oriented entities.
- Observer: A good choice for model-view architectures.
Another group of remarkable GoF patterns in use among novice C++ architects are, alphabetically, Adapter, Facade, Mediator, and Proxy.
But to be honest, both groups of GoF patterns are based upon non-GoF fundamental patterns such as Class Inheritance / Interface Implementation, Composition / Aggregation / Acquaintance, and Delegation.