Hands-On Design Patterns with C++

Book description

A comprehensive guide with extensive coverage on concepts such as OOP, functional programming, generic programming, and STL along with the latest features of C++

Key Features

  • Delve into the core patterns and components of C++ in order to master application design
  • Learn tricks, techniques, and best practices to solve common design and architectural challenges
  • Understand the limitation imposed by C++ and how to solve them using design patterns

Book Description

C++ is a general-purpose programming language designed with the goals of efficiency, performance, and flexibility in mind. Design patterns are commonly accepted solutions to well-recognized design problems. In essence, they are a library of reusable components, only for software architecture, and not for a concrete implementation.

The focus of this book is on the design patterns that naturally lend themselves to the needs of a C++ programmer, and on the patterns that uniquely benefit from the features of C++, in particular, the generic programming. Armed with the knowledge of these patterns, you will spend less time searching for a solution to a common problem and be familiar with the solutions developed from experience, as well as their advantages and drawbacks. The other use of design patterns is as a concise and an efficient way to communicate. A pattern is a familiar and instantly recognizable solution to specific problem; through its use, sometimes with a single line of code, we can convey a considerable amount of information. The code conveys: "This is the problem we are facing, these are additional considerations that are most important in our case; hence, the following well-known solution was chosen."

By the end of this book, you will have gained a comprehensive understanding of design patterns to create robust, reusable, and maintainable code.

What you will learn

  • Recognize the most common design patterns used in C++
  • Understand how to use C++ generic programming to solve common design problems
  • Explore the most powerful C++ idioms, their strengths, and drawbacks
  • Rediscover how to use popular C++ idioms with generic programming
  • Understand the impact of design patterns on the program's performance

Who this book is for

This book is for experienced C++ developers and programmers who wish to learn about software design patterns and principles and apply them to create robust, reusable, and easily maintainable apps.

Publisher resources

Download Example Code

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Design Patterns with C++
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  6. An Introduction to Inheritance and Polymorphism
    1. Classes and objects
    2. Inheritance and class hierarchies
    3. Polymorphism and virtual functions
    4. Multiple inheritance
    5. Summary
    6. Questions
    7. Further reading
  7. Class and Function Templates
    1. Templates in C++
      1. Function templates
      2. Class templates
      3. Variable template
      4. Non-type template parameters
    2. Template instantiations
      1. Function templates
      2. Class templates
    3. Template specializations
      1. Explicit specialization
      2. Partial specialization
    4. Template function overloading
    5. Variadic templates
    6. Lambda expressions
    7. Summary
    8. Questions
    9. Further reading
  8. Memory Ownership
    1. Technical requirements
    2. What is memory ownership?
      1. Well-designed memory ownership
      2. Poorly designed memory ownership
    3. Expressing memory ownership in C++
      1. Expressing non-ownership
      2. Expressing exclusive ownership
      3. Expressing transfer of exclusive ownership
      4. Expressing shared ownership
    4. Summary
    5. Questions
    6. Further reading
  9. Swap - From Simple to Subtle
    1. Technical requirements
    2. Swap and the standard template library
      1. Swap and STL containers
      2. Non-member swap
      3. Swapping like the standard
    3. When and why to use swap
      1. Swap and exception safety
      2. Other common swap idioms
    4. How to implement and use swap correctly
      1. Implementing swap
      2. Using swap correctly
    5. Summary
    6. Questions
  10. A Comprehensive Look at RAII
    1. Technical requirements
    2. Resource management in C++
      1. Installing the microbenchmark library
      2. Installing Google Test
      3. Counting resources
    3. Dangers of manual resource management
      1. Manual resource management is error-prone
      2. Resource management and exception safety
    4. The RAII idiom
      1. RAII in a nutshell
      2. RAII for other resources
      3. Releasing early
      4. Careful implementation of Resource Acquisition is Initialization objects
      5. Downsides of RAII
    5. Summary
    6. Questions
    7. Further reading
  11. Understanding Type Erasure
    1. Technical requirements
    2. What is type erasure?
      1. Type erasure by example
    3. How is type erasure implemented in C++?
      1. Very old type erasure
      2. Object-oriented type erasure
      3. The opposite of the type erasure
      4. Type erasure in C++
    4. When to use type erasure, and when to avoid it
      1. Type erasure and software design
      2. Installing the micro-benchmark library
      3. The overhead of type erasure
    5. Summary
    6. Questions
  12. SFINAE and Overload Resolution Management
    1. Technical requirements
    2. Overload resolution and overload sets
      1. C++ function overloading
      2. Template functions
    3. Type substitution in template functions
      1. Type deduction and substitution
      2. Substitution failure
      3. Substitution Failure Is Not An Error
    4. Taking control of overload resolution
      1. Basic SFINAE
      2. Advanced SFINAE
      3. Advanced SFINAE revisited
      4. The ultimate SFINAE
    5. Summary
    6. Questions
    7. Further reading
  13. The Curiously Recurring Template Pattern
    1. Technical requirements
    2. Wrapping your head around CRTP
      1. What is wrong with a virtual function?
      2. Introducing CRTP
    3. CRTP and static polymorphism
      1. Compile-time polymorphism
      2. The compile-time pure virtual function
      3. Destructors and polymorphic deletion
      4. CRTP and access control
    4. CRTP as a delegation pattern
      1. Expanding the interface
    5. Summary
    6. Questions
  14. Named Arguments and Method Chaining
    1. Technical requirements
    2. The problem with arguments
      1. What's wrong with many arguments
      2. Aggregate parameters
    3. Named arguments in C++
      1. Method chaining
      2. Method chaining and named arguments
      3. Performance of the named arguments idiom
    4. General method chaining
      1. Method chaining versus method cascading
      2. General method chaining
      3. Method chaining in class hierarchies
    5. Summary
    6. Questions
  15. Local Buffer Optimization
    1. Technical requirements
    2. The overhead of small memory allocations
      1. The cost of memory allocations
    3. Introducing local buffer optimization
      1. The main idea
      2. Effect of local buffer optimization
      3. Additional optimizations
    4. Local buffer optimization beyond strings
      1. Small vector
      2. Type-erased and callable objects
      3. Local buffer optimization in the C++ library
    5. Downsides of local buffer optimization
    6. Summary
    7. Questions
    8. Further reading
  16. ScopeGuard
    1. Technical requirements
    2. Error handling and Resource Acquisition Is Initialization
      1. Error safety and exception safety
      2. Resource Acquisition Is Initialization
    3. The ScopeGuard pattern
      1. ScopeGuard basics
      2. Generic ScopeGuard
    4. ScopeGuard and exceptions
      1. What must not throw an exception
      2. Exception-driven ScopeGuard
    5. Type-erased ScopeGuard
    6. Summary
    7. Questions
  17. Friend Factory
    1. Technical requirements
    2. Friends in C++
      1. How to grant friendship in C++
      2. Friends versus member functions
    3. Friends and templates
      1. Friends of template classes
    4. The template friend factory
      1. Generating friends on demand
    5. The friend factory and the Curiously Recurring Template Pattern
    6. Summary
    7. Questions
  18. Virtual Constructors and Factories
    1. Technical requirements
    2. Why constructors cannot be virtual
      1. When does an object get its type?
    3. The Factory pattern
      1. The basics of the Factory method
      2. Arguments for factory methods
      3. Dynamic type registry
      4. Polymorphic factory
    4. Factory-like patterns in C++
      1. Polymorphic copy
      2. CRTP Factory and return types
      3. CRTP Factory with less copy-paste
    5. Summary
    6. Questions
  19. The Template Method Pattern and the Non-Virtual Idiom
    1. Technical requirements
    2. The Template Method pattern
      1. The Template Method in C++
      2. Applications of the Template Method
      3. Pre-and post-conditions and actions
    3. The Non-Virtual Interface
      1. Virtual functions and access
      2. The NVI idiom in C++
      3. A note about destructors
    4. Drawbacks of the Non-Virtual Interface
      1. Composability
      2. The Fragile Base Class problem
    5. Summary
    6. Questions
    7. Further reading
  20. Singleton - A Classic OOP Pattern
    1. Technical requirements
    2. The singleton pattern – what is it and what is it for?
      1. What is a singleton?
      2. When to use the singleton
    3. Types of singletons
      1. Static singleton
      2. Meyers' Singleton
      3. Leaky singletons
    4. Summary
    5. Questions
  21. Policy-Based Design
    1. Technical requirements
    2. Strategy pattern and policy-based design
      1. Foundations of policy-based design
      2. Implementation of policies
      3. Use of policy objects
    3. Advanced policy-based design
      1. Policies for constructors
      2. Policies for test
      3. Policy adapters and aliases
      4. Using policies to control the public interface
      5. Rebinding policies
    4. Recommendations and guidelines
      1. Strengths of the policy-based design
      2. Disadvantages of policy-based design
      3. Guidelines for policy-based designs
    5. Almost policy-based approach
    6. Summary
    7. Questions
  22. Adapters and Decorators
    1. Technical requirements
    2. The decorator pattern
      1. Basic decorator pattern
      2. Decorators the C++ way
      3. Polymorphic decorators and their limitations
      4. Composable decorators
    3. The Adapter pattern
      1. Basic Adapter pattern
      2. Function adapters
      3. Compile-time adapters
    4. Adapter versus policy
    5. Summary
    6. Questions
  23. The Visitor Pattern and Multiple Dispatch
    1. Technical requirements
    2. The Visitor pattern
      1. What is the Visitor pattern?
      2. Basic Visitor in C++
      3. Visitor generalizations and limitations
    3. Visiting complex objects
      1. Visiting composite objects
      2. Serialization and deserialization with Visitor
    4. Acyclic Visitor
    5. Visitors in modern C++
      1. Generic Visitor
      2. Lambda Visitor
      3. Generic Acyclic Visitor
    6. Compile-time Visitor
    7. Summary
    8. Questions
  24. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
    15. Chapter 15
    16. Chapter 16
    17. Chapter 17
    18. Chapter 18
  25. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Design Patterns with C++
  • Author(s): Fedor G. Pikus
  • Release date: January 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781788832564