Full Design Patterns in Modern C++20: Reusable Approaches For Object-Oriented Software Design 2nd Edition Dmitri Nesteruk Ebook All Chapters
Full Design Patterns in Modern C++20: Reusable Approaches For Object-Oriented Software Design 2nd Edition Dmitri Nesteruk Ebook All Chapters
com
https://ebookmeta.com/product/design-patterns-in-
modern-c20-reusable-approaches-for-object-oriented-software-
design-2nd-edition-dmitri-nesteruk/
OR CLICK HERE
DOWLOAD NOW
https://ebookmeta.com/product/black-light-rescued-black-light-series-
book-6-1st-edition-livia-grant/
ebookmeta.com
Mechanics and Physics of Structured Media : Asymptotic and
Integral Equations Methods of Leonid Filshtinsky 1st
Edition Igor Andrianov
https://ebookmeta.com/product/mechanics-and-physics-of-structured-
media-asymptotic-and-integral-equations-methods-of-leonid-
filshtinsky-1st-edition-igor-andrianov/
ebookmeta.com
https://ebookmeta.com/product/energy-and-development-1st-edition-
frauke-urban-2/
ebookmeta.com
https://ebookmeta.com/product/uniting-their-pack-the-lost-therian-
omega-book-3-1st-edition-aisling-cousins/
ebookmeta.com
https://ebookmeta.com/product/if-you-re-freaking-out-read-this-1st-
edition-simone-deangelis/
ebookmeta.com
https://ebookmeta.com/product/attacked-1st-edition-marc-favreau/
ebookmeta.com
Learn From Bent Larsen Mihail Marin
https://ebookmeta.com/product/learn-from-bent-larsen-mihail-marin/
ebookmeta.com
Design Patterns
in Modern C++20
Reusable Approaches for Object-
Oriented Software Design
—
Second Edition
—
Dmitri Nesteruk
Design Patterns in
Modern C++20
Reusable Approaches
for Object-Oriented
Software Design
Second Edition
Dmitri Nesteruk
Design Patterns in Modern C++20: Reusable Approaches for
Object-Oriented Software Design
Dmitri Nesteruk
St. Petersburg, Russia
Chapter 1: Introduction������������������������������������������������������������������������1
Who This Book Is For���������������������������������������������������������������������������������������������2
On Code Examples������������������������������������������������������������������������������������������������3
On Developer Tools������������������������������������������������������������������������������������������������4
Preface to the Second Edition�������������������������������������������������������������������������������5
Important Concepts�����������������������������������������������������������������������������������������������6
Curiously Recurring Template Pattern�������������������������������������������������������������6
Mixin Inheritance���������������������������������������������������������������������������������������������8
Old-Fashioned Static Polymorphism���������������������������������������������������������������8
Static Polymorphism with Concepts��������������������������������������������������������������11
Properties������������������������������������������������������������������������������������������������������12
The SOLID Design Principles�������������������������������������������������������������������������������14
Single Responsibility Principle����������������������������������������������������������������������15
Open-Closed Principle�����������������������������������������������������������������������������������17
Liskov Substitution Principle�������������������������������������������������������������������������26
Interface Segregation Principle���������������������������������������������������������������������29
Dependency Inversion Principle��������������������������������������������������������������������33
iii
Table of Contents
Chapter 3: Factories���������������������������������������������������������������������������67
Scenario��������������������������������������������������������������������������������������������������������������67
Factory Method���������������������������������������������������������������������������������������������������70
Factory����������������������������������������������������������������������������������������������������������������72
Factory Methods and Polymorphism������������������������������������������������������������������75
Nested Factory����������������������������������������������������������������������������������������������������76
Abstract Factory��������������������������������������������������������������������������������������������������78
Functional Factory����������������������������������������������������������������������������������������������82
Object Tracking���������������������������������������������������������������������������������������������������83
Summary������������������������������������������������������������������������������������������������������������85
Chapter 4: Prototype���������������������������������������������������������������������������87
Object Construction���������������������������������������������������������������������������������������������87
Ordinary Duplication�������������������������������������������������������������������������������������������88
Duplication via Copy Construction����������������������������������������������������������������������89
Virtual Constructor����������������������������������������������������������������������������������������������92
iv
Table of Contents
Serialization��������������������������������������������������������������������������������������������������������94
Prototype Factory������������������������������������������������������������������������������������������������98
Summary������������������������������������������������������������������������������������������������������������99
Chapter 5: Singleton�������������������������������������������������������������������������101
Singleton As Global Object��������������������������������������������������������������������������������102
Classic Implementation�������������������������������������������������������������������������������������103
Thread Safety����������������������������������������������������������������������������������������������105
The Trouble with Singleton�������������������������������������������������������������������������������107
Per-Thread Singleton�����������������������������������������������������������������������������������111
Ambient Context������������������������������������������������������������������������������������������114
Singletons and Inversion of Control�������������������������������������������������������������118
Monostate����������������������������������������������������������������������������������������������������119
Summary�����������������������������������������������������������������������������������������������������120
Chapter 7: Bridge������������������������������������������������������������������������������135
The Pimpl Idiom������������������������������������������������������������������������������������������������135
Bridge���������������������������������������������������������������������������������������������������������������138
Summary����������������������������������������������������������������������������������������������������������141
v
Table of Contents
Chapter 8: Composite�����������������������������������������������������������������������143
Array-Backed Properties�����������������������������������������������������������������������������������145
Grouping Graphic Objects���������������������������������������������������������������������������������148
Neural Networks�����������������������������������������������������������������������������������������������150
Shrink-Wrapping the Composite�����������������������������������������������������������������155
Conceptual Improvements���������������������������������������������������������������������������155
Concepts and Global Operators�������������������������������������������������������������������157
Composite Specification�����������������������������������������������������������������������������������159
Summary����������������������������������������������������������������������������������������������������������161
Chapter 9: Decorator������������������������������������������������������������������������163
Scenario������������������������������������������������������������������������������������������������������������163
Dynamic Decorator�������������������������������������������������������������������������������������������166
Static Decorator������������������������������������������������������������������������������������������������169
Functional Decorator�����������������������������������������������������������������������������������������172
Summary����������������������������������������������������������������������������������������������������������177
vi
Table of Contents
Naïve Approach�������������������������������������������������������������������������������������������195
Flyweight Implementation���������������������������������������������������������������������������197
Summary����������������������������������������������������������������������������������������������������������200
vii
Table of Contents
viii
Table of Contents
ix
Table of Contents
Index�������������������������������������������������������������������������������������������������377
x
About the Author
Dmitri Nesteruk is a quantitative analyst,
developer, course and book author, and
an occasional conference speaker. His
professional interests lie in software
development and integration practices in the
areas of computation, quantitative finance,
and algorithmic trading. His technological
interests include C# and C++ programming
as well as high-performance computing using
technologies such as CUDA and FPGAs. He
has been a C# MVP since 2009.
xi
About the Technical Reviewers
David Pazmino has been developing software applications for 20 years in
Fortune 100 companies. He is an experienced developer in front-end and
back-end development who specializes in developing machine learning
models for financial applications. David has developed many applications
in C++, STL, and ATL for companies using Microsoft technologies.
He currently develops applications in Scala and Python for deep
learning neural networks. David has a degree from Cornell University, a
masters from Pace University in Computer Science, and a masters from
Northwestern in Predictive Analytics.
xiii
About the Technical Reviewers
xiv
CHAPTER 1
Introduction
The topic of design patterns sounds dry, academically dull, and, in
all honesty, done to death in almost every programming language
imaginable – including programming languages such as JavaScript which
aren’t even properly OOP! So why another book on it? I know that if
you’re reading this, you probably have a limited amount of time to decide
whether this book is worth the investment.
The main reason why this book exists is that C++ is “great again.” After
a long period of stagnation, it’s now evolving and growing, and, despite
the fact that it has to contend with backward C compatibility, good things
are happening – they may not always happen at the pace we’d all like, but
this is a byproduct of the way the evolution of the C++ language standard is
structured.
Now, on to design patterns – we shouldn’t forget that the original
Design Patterns book1 was published with examples in C++ and Smalltalk.
Since then, plenty of programming languages have incorporated
design patterns directly into the language: for example, C# directly
incorporated the Observer pattern with its built-in support for events
(and the corresponding event keyword). C++ has not done the same, at
least not on the syntax level. That said, the introduction of types such as
std::function sure made things a lot simpler for many programming
scenarios.
1
Erich Gamma et al. (1994), Design Patterns: Elements of Reusable Object-Oriented
Software, Addison-Wesley
“WHOA, thar, pards! The world ain’t fell over the moon, jist yet!”
guffawed Big John at their blank faces. “She’s thar, boys, only you’ve
got to dig fer her. This desert’s full of them little tricks on the pore
tenderfoot.”
He got out his camp plate from a saddle bag and started digging.
Ruler and the dogs were already shoveling industriously with eager
paws, for their noses smelled the water. Sid grabbed out his plate
and fell to, while Scotty held back the horses to keep them from
burying their hoofs to the fetlocks in the sand and packing it too tight
to dig.
After a time it came out damp, then moist, then wet mud. Big John
hove out the dogs and stood Sid aside, as they all watched the deep
hollow they had made, nine pairs of eyes all trained on the one
object of most engrossing interest in all the world,—the seepage of
an almost invisible puddle of cool, clear water!
“Git me the canvas pail, an’ a cup, Sid—the hosses is first. Git outer
thar, Ruler, you ole potlicker!” he roared, batting back the persistent
hound. Scotty was struggling with the horses, jamming back on their
curbs as they plunged and pawed, wild to get down into the sand
and drink, drink, drink! Then three equine noses shoved urgently,
fiercely at them, as a few cupfuls in a canvas pail were passed up.
“This is nawthin’, boys!” grinned Big John, as the impatient animals
were being watered. “Onct I hed to save myself by cuttin’ open a
bisanaga cactus and go to poundin’ the inside with a club. Thet pulp
is full of sweet water, an’ ye squeezes out the pulp an’ throws it away
ontil ye hev maybe a pint of good clear water to drink. No old-timer
dies of thirst, the way them writers is allus makin’ ’em do, down
south in the barrel cactus country!”
It was all of two hours later when the last of the water bags was filled
and the party set off toward the southeast. If no accident befell they
had water enough for the run to Los Capitanos del Canyon, where a
blessed brook awaited them. The sandstorm had delayed them one
day; the whole party were now worried lest they should be too late,
for Neyani’s fate hung in the balance, and, perhaps, also that of
Niltci’s mother and his sister.
“That thar letter says the Injuns has took ’em off, somewhere, don’t
it, Sid?” asked Big John as they discussed the matter, urging the
horses along. “Waal, it’s a leetle deetour over to White Mesa, but I’m
going thar, boys. That’s a sacred spot to them Navaho; they’re scairt
to death of it, an’ think it’s full of ghosts, but the hull tribe sometimes
comes thar to pull off some reeligious stunt, each brave sorta
bolsterin’ up the other’s courage. It’s just whar they mought take ole
Neyani—an’, of course, the Major, he couldn’t do nawthin’ but follow
an’ try to talk some sense inter them, ef he heard tell that was whar
Neyani was.”
“I’ve heard of the Enchanted Mesa,” replied Sid. “The Navaho call it
the ‘Judgment Throne of the Ghosts,’ don’t they?”
“I dunno. It’s a skeery place to go by, in the moonlight, even for a
white man. It’s as full of howls and roars, and the awfullest sounds a
body ever listened to,” said Big John. “But I’ll bet my ol’ lid thet that’s
whar Neyani is, right now!”
After an hour’s riding White Mesa itself jutted up, in a long
escarpment, shimmering with heat, in the immense distances. As
they gradually neared it Sid felt that never had he beheld such a
place. The odd chalky formation rose in ramparts and pinnacles, like
bastions of huge whitened giant’s bones. By moonlight one could
well expect to see whole regiments of bleached skeletons of
departed Indians skipping across it. But there were living beings
there, now!
“What did I tell you?” chortled Big John. “Them little moving dots
along the base is Injuns, on their ponies. Somethin’ doin’, thar, boys!”
As they rode nearer to the blazing white sepulcher the moving dots
showed color and took form. They were the Navaho, a lot of them.
Soon some stopped to look at them and there was a commotion
among the tiny black figures. Then a lone rider galloped furiously out
toward them, mounted on a large black horse.
“That’s no Injun, an’ no pony—it’s Major Hinchman himself!”
exclaimed Big John as the rider streaked out toward them.
The man waved his sombrero. “Hi! Hi!—Back!—Halt!” he called
excitedly as he thundered toward them.
“Yaas—we’re haltin’, a lot!” muttered Big John as they all reined up
and he called back Ruler. “Wonder what’s up?”
“Hi!—Is that you, boys?—Howdy, strangers—get right down!” yelled
the white giant on the horse, for it was Hinchman himself. “Jee-
mentley-dingit, but it’s a mercy of Providence that you-all happened
to come this way!” he exploded as he rode up close. “I couldn’t have
met you at Monument Canyon with this coming off. But I see you’ve
got the dawgs—Jeementley but they look good to me! Whar’s the
Colonel, Sid?”
“Father’s laid up, sir, over in the Grand Canyon,” explained Sid. “Had
a mix-up with a whale of a grizzly and got mauled some, but he’ll be
all right, soon. It’s an old-time California silver-tip, Major, the biggest
bear I ever saw. Got him in back of Buckskin. Niltci’s looking after
father, while we came over——”
“Niltci! Eh? Splen—did!” beamed Major Hinchman, his keen face
lighting up with joy and relief. “You don’t tell me! Nothing on earth
can convince these Injuns but that the Black Panther came and took
him that night——”
The boys laughed, and Big John grinned sheepishly. At the Major’s
questioning glance he told him just how that affair really was
managed. Hinchman howled with delight. “Better and better and
better!” he cried, his eyes snapping with pleasure. “You see, boys,
since you have been gone the Black Panther has been visiting all the
Navaho sheep corrals—— I suppose the dogs running him scared
him off Neyani’s—but anyhow he’s become a regular plague. The