This course provides a comprehensive overview of Design Patterns in Modern C++ from a practical perspective. This course in particular covers patterns with the use of:
This course provides an overview of all the Gang of Four (GoF) design patterns as outlined in their seminal book, together with modern-day variations, adjustments, discussions of intrinsic use of patterns in the language.
What are Design Patterns?
Design Patterns are reusable solutions to common programming problems. They were popularized with the 1994 book Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, John Vlissides, Ralph Johnson and Richard Helm (who are commonly known as a Gang of Four, hence the GoF acronym).
The appeal of design patterns is immortal: we see them in libraries, some of them are intrinsic in programming languages, and you probably use them on a daily basis even if you don't realize they are there.
What Patterns Does This Course Cover?
This course covers all the GoF design patterns. In fact, here's the full list of what is covered:
Who Is the Course For?
This course is for C++ developers who want to see not just textbook examples of design patterns, but also the different variations and tricks that can be applied to implement design patterns in a modern way.
This course is presented as a (very large) series of live demonstrations being done in JetBrains CLion. Most demos are single-file, so you can download the file attached to the lesson and run it in CLion, XCode or another IDE of your choice (or just on the command line).
This course does not use UML class diagrams; all of demos are live coding.
What are SOLID principles, where do they come from and why do we care?
A look at the Single Responsibility Principle, which states that a class should only have one reason to change. Also tied to the concept of Separation of Concerns which is basically stating the same thing.
A discussion of the Open-Closed Principle, which states that classes should be open for extension, but closed for modification. In other words, you should extend functionality using interfaces and inheritance rather than jumping back into already-written/tested code and adding to it or changing it.
This lesson also demonstrates the Specification pattern.
The Liskov Substitution Principle states that subtypes should be substitutable for their base types.
The Interface Segregation Principle is simple: don't throw everything in the kitchen sink into an interface because then all its users will have to implement things they do not need. Instead, split the interface into several smaller ones.
Not to be confused with dependency injection, dependency inversion specifies that high-level modules should not depend on low-level ones; both should depend on abstractions. Confusing, huh?
A summary of the things we've learned in this section of the course.
A discussion of the Builder pattern and what it's used for.
A look at why you'd want to have a builder in the first place.
We implement a simple builder for constructing trees of HTML elements.
We make the builder fluent by returning this from builder methods.
Not so much a Builder pattern, but a clever way of using uniform initializer syntax to create a DSL for easily defining HTML constructs in a familiar manner.
We look at a more complicated builder facade that exposes several sub-builders (builder facets) for building up parts of an object in a fluent manner.
A summary of the things we've learned about the Builder pattern.
A discussion of the general concept of factories and the two design patterns: Factory Methods and Abstract Factory.
A scenario where having a factory interface actually makes sense.
Implementing a factory method, as an alternative to a constructor, is easy.
When you want all the factory methods in a separate class.
An external factory needs the created object's constructor to be public. But what if you want it to be private? There are two solutions here: you either make a friend class or, alternatively, stick a factory into the class whose instance it creates!
Sometimes, you want abstract factories with abstract objects; we support DIP but break OCP in the process.
Thanks to constructs such as std::function, we can express factories in a purely functional way.
A summary of the things we've learned about factories.
A discussion of the Prototype factory (not to be confused with a rather good game of the same name) and what it's used for.
A sample scenario where the Prototype pattern is relevant.
We implement the Prototype design pattern by making copy constructors.
If you find using prototypes a lot, and you need many of them, why not put them into a separate class? Separation of concerns!
One common approach to the Prototype pattern is to serialize-deserialize data. But you need to support it explicitly in each type you use.
A summary of all the things we've learned about the Prototype pattern.
Ahh, the much maligned Singleton... is it really that evil? Let's find out...
Let's put together a simple implementation of Singleton before we start to embellish it with additional traits.
So, what's wrong with the Singleton? Well, hard dependencies on singletons are hard to test.
In order to write a unit test that uses a singleton, we must abstract it away. This is typically done by extracting the singleton's interface and then taking that interface as a dependency (e.g., a constructor parameter). This way, you can supply a fake object instead, thereby getting a true unit test instead of an integration test.
The only socially acceptable way of using a singleton is when you inject it as a dependency. DI containers allow you to configure a singleton lifetime for a component.
The Monostate design pattern is a bizarre variation on the Singleton: it's a type that appears just as an ordinary type (meaning you can construct multiple instances), but all its fields are actually private and static and are exposed with non-static getters and setters. More of a scientific curiosity rather than a viable design solution, this one.
A summary of all the things we've learned about the Singleton design pattern.
An overview of the Adapter design pattern.
Let's look at a visual demonstration for a change. This MFC application can only render points, but all we have are lines. We need an adapter!
It just so happens that an adapter generates lots of temporaries. Let's see if we can add some caching to reduce the workload.
A summary of all the things we've learned about the Adapter design pattern.
A look at the Bridge design pattern...
A summary of all the things we've learned about the Bridge design pattern.
A discussion of what the Composite pattern is for and how it's used.
Let's implement the Composite pattern by considering individual geometric shapes as well as grouping of shapes.
Let's apply the Composite pattern to the implementation of simple neural networks (individual neurons and layers of neurons).
Having individual fields with getters and setters is all fine until you want to perform aggregate operations on all the available fields. This calls for an alternative approach, which is an unusual blend of the Composite and Proxy design patterns.
A summary of all the things we've learned about the Composite design pattern.
Decorators are typically applied to classes, but it is equally possible to build decorators which wrap arbitrary chunks of code.
A summary of all the things we've learned about the Decorator design pattern.
Dmitri Nesteruk is a developer, speaker and podcaster. His interests lie in software development and integration practices in the areas of computation, quantitative finance and algorithmic trading. His technological interests include C#, F# and C++ programming as well high-performance computing using technologies such as CUDA. He has been a C# MVP since 2009.
Dmitri is a graduate of University of Southampton (B.Sc. Computer Science) where he currently holds a position as a Visiting Researcher. He is also an instructor on an online intro-level Quantitative Finance course, and has also made online video courses on CUDA, MATLAB, D, the Boost libraries and other topics.