Go from competent C++ developer to skilled designer or architect using this book as your personal C++ design master class. Updated for the C++20 standard, this title will guide you through the design and implementation of an engaging case study that forms the backdrop for learning the art of applying design patterns and modern C++ techniques to create a high quality, robust application.
Starting with a quick exploration of the requirements for building the application, you'll delve into selecting an appropriate architecture, eventually designing and implementing all of the necessary modules to meet the project’s requirements. By the conclusion of Practical C++ Design, you'll have constructed a fully functioning calculator capable of building and executing on any platform that supports both Qt and C++20. Access to the complete source code will help speed your learning.
Utilize the Model-View-Controller pattern as the basis for the architecture of the calculator; the observer pattern to design an event system; the singleton pattern as you design the calculator’s central data repository, a reusable stack; the command pattern to design a command system supporting unlimited undo/redo; the abstract factory pattern to build a cross-platform plugin infrastructure for extensibility; coroutines to implement a command line interface with a lazy tokenizer; and more.
After reading and using this book, you’ll have begun the transition from C++ programmer to architect.
What You Will Learn
- Read a specification document and translate it into a practical C++ design using some of the latest language features from C++20
- Understand trade-offs in selecting between alternative design scenarios
- Gain practical experience in applying design patterns to realistic development scenarios
- Learn how to effectively use language elements of modern C++ to create a lasting design
- Develop a complete C++ program from a blank canvas through to a fully functioning, cross platform application
- Read, modify, and extend existing, high quality code
- Learn the fundamentals of API design, including class, module, and plugin interfaces
Who This Book Is For
The experienced C++ developer ready to take the next step to becoming a skilled C++ designer.
Author(s): Adam B. Singer
Edition: 2
Publisher: Apress
Year: 2021
Language: English
Pages: 309
Tags: C++; Software Design; Software Architecture; Software Design Patterns; Design Patterns
Table of Contents
About the Author
About the Technical Reviewer
Preface
Chapter 1: Defining the Case Study
1.1 A Brief Introduction
1.2 A Few Words About Requirements
1.3 Reverse Polish Notation (RPN)
1.4 The Calculator’s Requirements
1.5 The Source Code
Chapter 2: Decomposition
2.1 The Elements of a Good Decomposition
2.2 Selecting an Architecture
2.2.1 Multitiered Architecture
2.2.2 Model-View-Controller (MVC) Architecture
2.2.3 Architectural Patterns Applied to the Calculator
2.2.4 Choosing the Calculator’s Architecture
2.3 Interfaces
2.3.1 Calculator Use Cases
Use Case: User enters a floating-point number onto the stack
Use Case: User undoes last operation
Use Case: User redoes last operation
Use Case: User swaps top stack elements
Use Case: User drops the top stack element
Use Case: User clears the stack
Use Case: User duplicates the top stack element
Use Case: User negates the top stack element
Use Case: User performs an arithmetic operation
Use Case: User performs a trigonometric operation
Use Case: User performs y x
Use Case: User performs
Use Case: User loads a plugin
2.3.2 Analysis of Use Cases
2.3.3 A Quick Note on Actual Implementation
2.4 Assessment of Our Current Design
2.5 Implementing Our Design Using C++20 Modules
2.5.1 Why Modules?
2.5.2 Using Legacy Headers
2.5.3 Source Code Organization Prior to C++20
2.5.4 Source Code Organization Using C++20 Modules
2.5.5 Modules and pdCalc
Refining pdCalc’s Modules
Code Representation of Modules in pdCalc
Modules and DLLs for pdCalc
2.6 Next Steps
Chapter 3: The Stack
3.1 Decomposition of the Stack Module
3.2 The Stack Class
3.2.1 The Singleton Pattern
3.2.2 The Stack Module As a Singleton Class
3.3 Adding Events
3.3.1 The Observer Pattern
Enhancing the Observer Pattern Implementation
Handling Event Data
3.3.2 The Stack As an Event Publisher
3.3.3 The Complete Stack Module Interface
3.4 A Quick Note on Testing
Chapter 4: The Command Dispatcher
4.1 Decomposition of the Command Dispatcher
4.2 The Command Class
4.2.1 The Command Pattern
4.2.2 More on Implementing Undo/Redo
4.2.3 The Command Pattern Applied to the Calculator
The Command Interface
The Undo Strategy
Concrete Commands
An Alternative to Deep Command Hierarchies
4.3 The Command Factory
4.3.1 The CommandFactory Class
4.3.2 Registering Core Commands
4.4 The Command Manager
4.4.1 The Interface
4.4.2 Implementing Undo and Redo
4.5 The Command Interpreter
4.5.1 The Interface
The Pimpl Idiom
Do Modules Obviate the Pimpl Idiom?
4.5.2 Implementation Details
4.6 Revisiting Earlier Decisions
Chapter 5: The Command Line Interface
5.1 The User Interface Abstraction
5.1.1 The Abstract Interface
5.1.2 User Interface Events
Command Data
User Interface Observers
5.2 The Concrete CLI Class
5.2.1 Requirements
5.2.2 The CLI Design
The Interface
The Implementation
5.3 Tying It Together: A Working Program
Chapter 6: The Graphical User Interface
6.1 Requirements
6.2 Building GUIs
6.2.1 Building GUIs in IDEs
6.2.2 Building GUIs in Code
6.2.3 Which GUI Building Method Is Better?
6.3 Modularization
6.3.1 The CommandButton Abstraction
The CommandButton Design
The CommandButton Interface
6.3.2 Getting Input
The Design of the InputWidget
The Interface of the InputWidget
6.3.3 The Display
The Design of the Display Class
A Poor Design
An Improved Display Design
6.3.4 The Model
6.3.5 The Display Redux
6.3.6 Tying It Together: The Main Window
6.3.7 Look-and-Feel
6.4 A Working Program
6.5 A Microsoft Windows Build Note
Chapter 7: Plugins
7.1 What Is a Plugin?
7.1.1 Rules For C++ Plugins
7.2 Problem 1: The Plugin Interface
7.2.1 The Interface for Discovering Commands
7.2.2 The Interface for Adding New GUI Buttons
7.2.3 Plugin Allocation and Deallocation
7.2.4 The Plugin Command Interface
7.2.5 API Versioning
7.2.6 Making the Stack Available
7.3 Problem 2: Loading Plugins
7.3.1 Platform-Specific Plugin Loading
7.3.2 Loading, Using, and Closing a Shared Library
7.3.3 A Design for Multiplatform Code
The Obvious Solution: Libraries
Raw Preprocessor Directives
(Slightly) More Clever Preprocessor Directives
A Build System Solution
A Platform Factory Function
An Abstract Factory for Generalized Platform-Independent Code
7.4 Problem 3: Retrofitting pdCalc
7.4.1 Injecting Commands
7.4.2 Adding Plugin Buttons to the GUI
7.5 Incorporating Plugins
7.5.1 Loading Plugins
7.5.2 Injecting Functionality
7.6 A Concrete Plugin
7.6.1 Plugin Interface
7.6.2 Source Code Dependency Inversion
7.6.3 Implementing HyperbolicLnPlugin’s Functionality
7.7 Next Steps
Chapter 8: New Requirements
8.1 Fully Designed New Features
8.1.1 Batch Operation
8.1.2 Stored Procedures
The User Interface
Changes to the Command Dispatcher Module
Designing the StoredProcedure Class
The Composite Pattern
A First Attempt
A Final Design for the StoredProcedure Class
8.2 Designs Toward a More Useful Calculator
8.2.1 Complex Numbers
Modifying Input and Output
Modifying the Stack
Modifying Commands
8.2.2 Variables
Input and New Commands
Number Representation and the Stack
The Symbol Table
A Trivial Extension: Numeric Constants
Functionality Enabled by Variables
8.3 Some Interesting Extensions for Self-Exploration
8.3.1 High DPI Scaling
8.3.2 Dynamic Skinning
8.3.3 Flow Control
8.3.4 An Alternative GUI Layout
8.3.5 A Graphing Calculator
8.3.6 A Plugin Management System
8.3.7 A Mobile Device Interface
8.3.8 pdCalc in the Cloud
Appendix A:
Acquiring, Building, and Executing pdCalc
A.1 Getting the Source Code
A.2 Dependencies
A.3 Building pdCalc
A.3.1 Using Qt Creator
A.3.2 Using the Command Line
A.4 Executing pdCalc
A.4.1 Using Qt Creator
A.4.2 Using the Command Line
A.5 Troubleshooting
Appendix B:
Organization of the Source Code
B.1 The src Directory
B.1.1 The 3rdParty Directory
B.1.2 The app/pdCalc Directory
B.1.3 The app/pdCalc-simple-cli Directory
B.1.4 The app/pdCalc-simple-gui Directory
B.1.5 The utilities Directory
B.1.6 The backend Directory
B.1.7 The ui Directory
B.1.8 The plugins Directory
B.2 The test Directory
B.2.1 The testDriver Directory
B.2.2 The utilitiesTest Directory
B.2.3 The backendTest Directory
B.2.4 The uiTest Directory
B.2.5 The pluginsTest Directory
References
Index