Apply the latest editions of the C++ standard to the implementation of design patterns. As well as covering traditional design patterns, this book fleshes out new design patterns and approaches that will be useful to modern C++ developers. Author Dmitri Nesteruk presents concepts as a fun investigation of how problems can be solved in different ways, along the way using varying degrees of technical sophistication and explaining different sorts of trade-offs.
Design Patterns in Modern C++20, Second Edition also provides a technology demo for modern C++, showcasing how some of its latest features (e.g., coroutines, modules and more) make difficult problems a lot easier to solve. The examples in this book are all suitable for putting into production, with only a few simplifications made in order to aid readability.
What You Will Learn
- Use creational patterns such as builder, factories, prototype and singleton
- Implement structural patterns such as adapter, bridge, decorator, facade and more
- Work with the behavioral patterns such as chain of responsibility, command, iterator, mediator and more
- Apply functional design patterns such as the Maybe Monad
Who This Book Is For
This book is for both beginner and experienced C++ developers.
Author(s): Dmitri Nesteruk
Edition: 2
Publisher: Apress
Year: 2021
Language: English
Pages: 400
Tags: C++; C++20; Design Patterns; Object Oriented Design;
Table of Contents
About the Author
About the Technical Reviewers
Chapter 1: Introduction
Who This Book Is For
On Code Examples
On Developer Tools
Preface to the Second Edition
Important Concepts
Curiously Recurring Template Pattern
Mixin Inheritance
Old-Fashioned Static Polymorphism
Static Polymorphism with Concepts
Properties
The SOLID Design Principles
Single Responsibility Principle
Open-Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle
Part I: Creational
Chapter 2: Builder
Scenario
Simple Builder
Fluent Builder
Communicating Intent
Groovy-Style Builder
Composite Builder
Builder Parameter
Builder Inheritance
Summary
Chapter 3: Factories
Scenario
Factory Method
Factory
Factory Methods and Polymorphism
Nested Factory
Abstract Factory
Functional Factory
Object Tracking
Summary
Chapter 4: Prototype
Object Construction
Ordinary Duplication
Duplication via Copy Construction
Virtual Constructor
Serialization
Prototype Factory
Summary
Chapter 5: Singleton
Singleton As Global Object
Classic Implementation
Thread Safety
The Trouble with Singleton
Per-Thread Singleton
Ambient Context
Singletons and Inversion of Control
Monostate
Summary
Part II: Structural
Chapter 6: Adapter
Scenario
Adapter
Adapter Temporaries
Bidirectional Converter
Summary
Chapter 7: Bridge
The Pimpl Idiom
Bridge
Summary
Chapter 8: Composite
Array-Backed Properties
Grouping Graphic Objects
Neural Networks
Shrink-Wrapping the Composite
Conceptual Improvements
Concepts and Global Operators
Composite Specification
Summary
Chapter 9: Decorator
Scenario
Dynamic Decorator
Static Decorator
Functional Decorator
Summary
Chapter 10: Façade
Magic Square Generator
Fine-Tuning
Building a Trading Terminal
An Advanced Terminal
Where’s the Façade?
Summary
Chapter 11: Flyweight
User Names
Boost.Flyweight
String Ranges
Naïve Approach
Flyweight Implementation
Summary
Chapter 12: Proxy
Smart Pointers
Property Proxy
Virtual Proxy
Communication Proxy
Value Proxy
Summary
Part III: Behavioral
Chapter 13: Chain of Responsibility
Scenario
Pointer Chain
Broker Chain
Summary
Chapter 14: Command
Scenario
Implementing the Command Pattern
Undo Operations
Composite Command
Command Query Separation
Summary
Chapter 15: Interpreter
Parsing Integral Numbers
Numeric Expression Evaluator
Lexing
Parsing
Using the Lexer and Parser
Parsing with Boost.Spirit
Abstract Syntax Tree
Parser
Printer
Summary
Chapter 16: Iterator
Iterators in the Standard Library
Traversing a Binary Tree
Iteration with Coroutines
Summary
Chapter 17: Mediator
Chat Room
Mediator with Events
Service Bus As Mediator
Summary
Chapter 18: Memento
Bank Account
Undo and Redo
Memory Considerations
Using Memento for Interop
Summary
Chapter 19: Null Object
Scenario
Null Object
shared_ptr Is Not a Null Object
Design Improvements
Implicit Null Object
Interaction with Other Patterns
Summary
Chapter 20: Observer
Property Observers
Observer
Observable
Connecting Observers and Observables
Dependency Problems
Unsubscription and Thread Safety
Reentrancy
Observer with Boost.Signals2
Views
Summary
Chapter 21: State
State-Driven State Transitions
Handmade State Machine
Switch-Based State Machine
State Machines with Boost.MSM
Summary
Chapter 22: Strategy
Dynamic Strategy
Static Strategy
Summary
Chapter 23: Template Method
Game Simulation
Functional Template Method
Summary
Chapter 24: Visitor
Intrusive Visitor
Reflective Printer
What Is Dispatch?
Classic Visitor
Implementing an Additional Visitor
Acyclic Visitor
Variants and std::visit
Summary
Index