Practical C++ Design: From Programming to Architecture Adam B. Singer instant download
Practical C++ Design: From Programming to Architecture Adam B. Singer instant download
https://ebookmeta.com/product/practical-c-design-from-
programming-to-architecture-adam-b-singer/
https://ebookmeta.com/product/practical-c-design-from-
programming-to-architecture-2nd-edition-adam-b-singer/
https://ebookmeta.com/product/practical-c-design-from-
programming-to-architecture-1st-edition-adam-b-singer/
https://ebookmeta.com/product/mnemonics-for-radiologists-and-
frcr-2b-viva-preparation-a-systematic-approach-
aug-15-2013-_-1908911956-_-crc-press-1st-edition-yoong/
https://ebookmeta.com/product/the-dark-side-of-social-media-
psychological-managerial-and-societal-perspectives-1st-edition-
pavica-sheldon/
Forensic Mental Health 2nd Edition Michele P. Bratina
https://ebookmeta.com/product/forensic-mental-health-2nd-edition-
michele-p-bratina/
https://ebookmeta.com/product/minor-emergencies-philip-m-
buttaravoli/
https://ebookmeta.com/product/in-the-wake-of-the-mongols-the-
making-of-a-new-social-order-in-north-china-1200-1600-27th-
edition-jinping-wang/
https://ebookmeta.com/product/the-road-not-taken-a-selection-of-
robert-frost-s-poems-frost/
https://ebookmeta.com/product/united-states-anna-obiols/
CompTIA A Complete Practice Tests Exam Core 1 220 1001
and Exam Core 2 220 1002 2nd Edition Jeff T Parker
Quentin Docter
https://ebookmeta.com/product/comptia-a-complete-practice-tests-
exam-core-1-220-1001-and-exam-core-2-220-1002-2nd-edition-jeff-t-
parker-quentin-docter/
Practical
C++ Design
From Programming to Architecture
—
Second Edition
—
Adam B. Singer
Practical C++ Design
From Programming to Architecture
Second Edition
Adam B. Singer
Practical C++ Design: From Programming to Architecture
Adam B. Singer
The Woodlands, TX, USA
Chapter 2: Decomposition���������������������������������������������������������������������������������������� 9
2.1 The Elements of a Good Decomposition�������������������������������������������������������������������������������� 10
2.2 Selecting an Architecture������������������������������������������������������������������������������������������������������ 12
2.2.1 Multitiered Architecture������������������������������������������������������������������������������������������������ 13
2.2.2 Model-View-Controller (MVC) Architecture������������������������������������������������������������������� 14
2.2.3 Architectural Patterns Applied to the Calculator����������������������������������������������������������� 16
2.2.4 C
hoosing the Calculator’s Architecture������������������������������������������������������������������������ 17
2.3 I nterfaces������������������������������������������������������������������������������������������������������������������������������ 17
2.3.1 C
alculator Use Cases���������������������������������������������������������������������������������������������������� 18
2.3.2 A
nalysis of Use Cases��������������������������������������������������������������������������������������������������� 21
2.3.3 A Quick Note on Actual Implementation����������������������������������������������������������������������� 28
2.4 Assessment of Our Current Design��������������������������������������������������������������������������������������� 29
2.5 Implementing Our Design Using C++20 Modules����������������������������������������������������������������� 29
2.5.1 W
hy Modules?�������������������������������������������������������������������������������������������������������������� 29
2.5.2 U
sing Legacy Headers�������������������������������������������������������������������������������������������������� 33
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
viii
Table of Contents
References������������������������������������������������������������������������������������������������������������ 279
Index��������������������������������������������������������������������������������������������������������������������� 283
ix
About the Author
Adam B. Singer graduated first in his class at the Georgia Institute of Technology in
1999 with a bachelor’s degree in chemical engineering. He subsequently attended
the Massachusetts Institute of Technology (MIT) on a National Defense Science and
Engineering Graduate Fellowship. He graduated from MIT with a Ph.D. in chemical
engineering in 2004 after defending his thesis titled “Global Dynamic Optimization.”
Since graduation, Adam has been employed by ExxonMobil1 in various Upstream
organizations, including research, information technology, and digital transformation.
He has held both technical and managerial roles in software development, design, and
project management in areas such as mathematical optimization, reservoir modeling
and simulation, decision support under uncertainty, basin modeling, well log modeling,
process stratigraphy, geoscience platform architecture, and data science. He has also
served on and chaired committees designing in-house training in the areas of technical
software development and computational and applied mathematics. He is currently the
Senior Principal Digital Architect for ExxonMobil.
Adam additionally held the title of adjunct assistant professor in the Department of
Computational and Applied Mathematics at Rice University from 2007 to 2012. In 2006
and 2007, he taught a graduate-level course, CAAM 520, on computational science.
The course focused on the design and implementation of high-performance parallel
programs.
1
The advice, information, and conclusions discussed in this book are those of the author and
have not been endorsed by, or reflect the opinions or practices of, ExxonMobil Corporation or its
affiliates.
xi
About the Technical Reviewer
German Gonzalez-Morris is a software architect/engineer working with C/C++,
Java, and different application containers, in particular with WebLogic Server.
He has developed various applications using JEE, Spring, and Python. His areas
of expertise also include OOP, Java/JEE, Python, design patterns, algorithms, Spring
Core/MVC/security, and microservices. German has worked with performance
messaging, RESTful API, and transactional systems. For more information about him,
visit www.linkedin.com/in/german-gonzalez-morris.
xiii
Preface
Motivation of the Author
Throughout my career, I have mentored both students and fellow employees in
programming, and many of them have suggested that I write my thoughts down in book
form. However, I have typically responded with the rebuttal that I felt I had nothing novel
to present. Being a largely self-taught programmer, I have always been able to rattle off
a long list of books from which I have derived most of my knowledge. Therefore, what
could I write about that has not already been said?
I came to realize, however, that the majority of books that I had encountered
tended to focus only on pieces of design or implementation rather than taking a holistic
approach. For example, if one wants to learn the C++ language, Stroustrup [30] or
Lippman and Lajoie [19] are excellent references. For learning C++ best practices, one
need only read the books by Sutter [31, 32, 33], Sutter and Alexandrescu [34], or Meyers
[22, 21, 23]. Of course, learning to program extends well beyond C++. For data structures
and algorithms, there are always the classics by Knuth [15, 16, 17] or the more accessible
and concise book by Cormen et al. [10]. To learn object-oriented analysis and design,
the book by Booch et al. [8] is an excellent reference. Of course, design patterns can be
learned from Gamma et al. [11], and general programming practices can be learned from
many books such as those by McConnell [20], Spinellis [29], or Kernighan and Pike [14].
Certainly, the deeper the specialty one seeks, the more esoteric the book one can
find (and should eventually read). This book is not such a book. Rather, I have striven to
write a book that operates from the premise that the reader already possesses a working
knowledge of the information encased in works such as the aforementioned titles. In this
book, I instead attempt to ground the reader’s theoretical knowledge of design through
practice using a single case study.
xv
Preface
Target Audience
As mentioned previously, the goal of this book is not to present any specific topic
but rather to explore the interrelationship of often compartmentalized subjects. The
successful combination of these components to form a cohesive, maintainable, elegant
piece of software is, in essence, design. As such, this book is intended to target practicing
professionals, in particular those who have several years of development experience but
who do not yet possess sufficient experience to architect independently a large software
project.
Because my intent is to emphasize utilization of one’s existing knowledge effectively
to design software, I make little effort to explain individual topics in great depth. I believe
too many books classified as intermediate to advanced fail because the author devotes
too much content describing prerequisites. The result is a massive tome filled with
unnecessary detail for the advanced reader, while the beginner is left with a long and
complicated exposition that is still inaccessible because the beginner does not possess
sufficient knowledge or experience to grasp the subject regardless of the amount of detail
devoted to the description. I have, therefore, aimed for conciseness over completeness.
Often, I simply refer the reader to relevant material rather than myself describe a
background topic in great detail. While this strategy may indeed make this book difficult
for beginners, I hope experienced professionals appreciate both the brevity of the book
and its tone, which assumes the reader is competent at their craft.
xvi
Preface
hopefully this book will enable the reader to learn from the author’s experiences without
having to repeat his mistakes. That is, I hope I have created a book that can serve as a
self-contained master class in design.
Language Selection
Design as an abstract concept can be taught in the absence of a particular programming
language. However, once committed to a concrete example, a specific language must
be chosen for the implementation. I decided to write the case study exclusively using
C++. While every line of the program does not appear in the text, all of the source code
is available for the reader to examine. Despite this book’s primary focus on design,
reading the source code is a good way to learn how implementation details in a specific
language enable or, at least, facilitate a chosen design. The source code also serves as
a high-quality (I hope) exemplar of a modern C++ implementation of a complete user
application. I highly recommend reading and modifying the source code in conjunction
with reading the text.
The decision to use C++ does not imply that C++ is the best choice for all programs,
and, in fact, it may not even be the best choice for the program examined in this book.
However, to ground the abstraction, a concrete language had to be selected. I chose
xvii
Preface
C++ because it is standardized, widely deployed, and available at zero cost on many
platforms. Selfishly, I also chose C++ because it is my most proficient language. While I
could, perhaps, have chosen another language meeting the aforementioned objective
criteria (e.g., Python), the resulting code would probably have been functional but
nonidiomatic due to my relative lack of expertise.
During the writing of the first edition of this book, C++0x was ratified as C++11
and then updated as C++14. C++11, with smart pointers, move semantics, lambdas,
variadic templates, and a host of other new features, fundamentally changed how
one could express a design in the C++ language. My objective at the time was to avoid
incorporating modern C++ features where they were inappropriate just to demonstrate
usage. Instead, I sought to highlight how these new language elements could be used
effectively in the design of a large-scale program. Hopefully, I achieved my objective.
Design and architecture have not changed significantly since the publication of
the first edition of this book, so why write a second edition? If this were an abstract
design book, such an update would be unnecessary. However, since the publication
of the first edition, C++17 and C++20 were both ratified. Because the book’s case study
is accompanied by a complete implementation, expressing a concrete design using
C++ has evolved with new language developments. While C++17 added several new
implementation features, none of these features substantially impacted the ability to
express designs in the language. C++20, on the other hand, is different.
C++20, maybe even more so than C++11, fundamentally changes how designs may
be expressed in C++. In particular, four new major language features were added: ranges,
coroutines, concepts, and modules, all of which make, at least, a brief appearance in
the case study. The ranges library does not fundamentally alter program design. It does,
however, fundamentally change the way algorithms can be implemented, especially
with the better integration of the functional programming paradigm as enabled through
views. Coroutines add cooperative multitasking to the C++ language. We will see that
even in a single-threaded program such as the one described in this book, coroutines
can be used to express an idea from the first edition differently. Concepts change the
way generic programs are constructed. This book’s design does not make extensive
use of templates; therefore, concepts make only the briefest of appearances. Lastly,
C++20 finally brings modularization to C++ as a formal language construct. This change
drastically impacts how code can be organized, including, in many instances, completely
eliminating the need for header files. We will explore the usage of C++ modules, and
their interaction with dynamically linked libraries, in some depth.
xviii
Other documents randomly have
different content
Kun hän heräsi, oli taaskin yö. Hän oli varmaankin nukkunut
kahdeksan tai kymmenen tuntia. Ensi hetkessä hän ei tiennyt, missä
hän oli, mutta vähitellen silmä tottui pimeyteen ja hän muisti
paenneensa orjuudesta.
Hänellä oli ladattu pyssy kädessään, eikä puuhun johtava kolo ollut
niin suuri, että eläin olisi voinut tunkeutua siitä sisään.
"Oi, jospa vielä kerran saisin nähdä meren, jospa saisin nähdä
valkean rannan, jossa leikittelin Abdullahin ja toisten toverieni
kanssa, ennenkuin heittäydyimme uimaan mereen! Muistan, miten
me kotona keräännyimme mangopuun alle, miten sen hedelmät
riippuivat kultaisina ja purppuranpunaisina ja miten iltatuuli liikutteli
lehtiä, taivutti kuninkaallisen kokospalmun latvaa ja tuoksuvaa
kaneelipuuta ja kohotteli räikeänvihreän appelsiinipuun oksia, jonka
hedelmät olivat kuin palsamia sielulleni. Jospa minulla olisi nyt yksi
ainoakin appelsiini! Muistan sokeriruo'on ambranväriset varret ja sen
ihanan mehun, kokospähkinöiden virvoittavan maidon, loistavan
granaattiomenan suloisine tuoksuineen ja makuineen, keltaiset
sitruunat, jotka poistavat kuumeen ja janon sekä meloonit, joiden
tummanvihreän kuoren sisäpuolella on niin ihania aarteita. Oi,
Sansibar, paratiisi maan päällä! Amer ben Osmanin onneton poika
näkee sen puiden ja kukkien, talojen ja puutarhojen tulevan yhä
lähemmäksi."
"Minä näin jalon isäni kaatuvan ja itse jäin yksin — minä itkin
ensin suurta suruani, sitten kärsin kaikki helvetin tuskat ja lopulta
toivoin, etten koskaan olisi nähnyt päivän valoa. Oi, noita hirveitä
marssipäiviä, joista ei tuntunut tulevan koskaan loppua, kun jano
kalvoi ruumistani eivätkä väsyneet jäsenet totelleet tahtoani."
Houreissaan hän näki edessään kuolleet, jotka oli jätetty tielle ja
makasivat siinä avoimin, jäykin silmin, ja kuolemankauhu valtasi
hänet, kun hän kuvitteli näkevänsä oman hautansa tämän metsän
korkean, vihreän holvin alla.
"Siksi että hän oli aasi", vastasi Ferodia katkerasti. "En ole koskaan
nähnyt niin röyhkeää poikaa. Suu hänellä oli täynnä sanoja, mutta
hänen selkänsä ei kyennyt työhön, ja siksi hän karkasi. Mutta
rohkeutta hänellä oli kahden sotilaan edestä, ja aikaa myöten
hänestä olisi tullut komea orja."
"Kenestä sinä puhut, Ferodia?" kysyi Kalulu, joka oli tullut lähelle.
"Ole vaiti poika", sanoi Katalambula. "Sukulaisesi Ferodia on
lahjoittanut sinulle tämän pienen valkean orjan leikkitoveriksi. Avaa
hänen siteensä ja opeta hänet sotilaaksi."
"Niin, pyssyn, jossa oli lääkettä — pulveria ja kuula — sillä kun hän
leikitteli sillä, pamahti se ja oli säikäyttää hänet kuoliaaksi."
"Hyvin omituista", sanoi Moto koettaen hillitä kärsimättömyyttään,
"eikö palvelijasi löytänyt muuta?"
"Hän elää, nuori Selim herra elää!" hän huusi, ja Kalulu kumartui
innokkaana hänen ylitseen, sillä Simban äänestäjä kasvojen ilmeestä
hän oli arvannut sanojen merkityksen.
"Mutta hän ei voi elää kauan, jollei hän saa jotakin syödäkseen",
Moto sanoi. "Näetkö, Simba, miten laiha hän on, vain luuta ja
nahkaa! Mikä sheita paha henki — on häntä pidellyt noin pahoin?
Katso naarmuja hänen käsivarsissaan ja — käännäpä häntä Simba —
haavoja hänen selässään!"
"Missä minä olen?" Selim kysyi. "Minulla oli hirveä uni. Minusta
tuntui, että olin kuolla nälkään ja janoon. Mutta minä olin kaukana
kaameassa metsässä. Ja nyt olen sisällä huoneessa, ja Simba on
vieressäni. Mitä on tapahtunut, Simba?"
"Ei, herra, sinä et ole enää yksin, mutta syö hiukan, niin tulet taas
voimiisi", sanoi Simba. Ja Selim söi katsellen samalla ympärilleen.
Hänen aivonsa koettivat ratkaista kysymystä, mitä oli tapahtunut sen
jälkeen, kun hän metsässä oli kaatunut kumoon nälästä,
väsymyksestä ja janosta heikontuneena.
Mutta kun hän oli syönyt kaiken maidon loppuun, olivat hänen
voimansa palanneet siinä määrin, että Simba saattoi lyhyesti kertoa
hänelle kaiken sen, minkä me jo tiedämme molempien orjien
matkasta Katalambulan luo.
Ja sitten tuli Kalulukin esille, ja Simba kertoi, miten hyvä hän oli
ollut Motolle ja miten he hänen avullaan olivat löytäneet Selimin.
Selim nosti kättään, puristi lämpimästi Kalulun kättä ja pyysi
Moton tulkitsemaan hänelle hänen kiitollisuuttaan.
"Vai Tifum Byah, tuo julma koira, kyllä minä hänen selkänsä
pehmitän!"
"Älä tee hänelle pahaa minun tähteni, Kalulu", pyysi Selim, "pahat
päivät ovat nyt lopussa."