Practical C++ Design From Programming to Architecture — Adam B. Singer WOW! eBook www.wowebook.org Practical C++ Design From Programming to Architecture Adam B. Singer WOW! eBook www.wowebook.org Practical C++ Design: From Programming to Architecture Adam B. Singer The Woodlands, Texas, USA ISBN-13 (pbk): 978-1-4842-3056-5 ISBN-13 (electronic): 978-1-4842-3057-2 DOI 10.1007/978-1-4842-3057-2 Library of Congress Control Number: 2017954981 Copyright © 2017 by Adam B. Singer This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Cover image by Freepik (www.freepik.com) Managing Director: Welmoed Spahr Editorial Director: Todd Green Acquisitions Editor: Steve Anglin Development Editor: Matthew Moodie Technical Reviewer: Michael Thomas Coordinating Editor: Mark Powers Copy Editor: Mary Behr Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer- sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit www.apress.com/ rights-permissions. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484230565. For more detailed information, please visit www.apress.com/source-code. Printed on acid-free paper WOW! eBook www.wowebook.org For Terri, Caroline, and Rebecca WOW! eBook www.wowebook.org Table of Contents About the Author �����������������������������������������������������������������������������������������������������xi About the Technical Reviewer �������������������������������������������������������������������������������xiii Preface ��������������������������������������������������������������������������������������������������������������������xv Chapter 1: Defining the Case Study ��������������������������������������������������������������������������1 1.1 A Brief Introduction ................................................................................................................1 1.2 A Few Words About Requirements .........................................................................................2 1.3 Reverse Polish Notation .........................................................................................................3 1.4 The Calculator’s Requirements ..............................................................................................5 1.5 The Source Code ....................................................................................................................6 Chapter 2: Decomposition ����������������������������������������������������������������������������������������9 2.1 The Elements of a Good Decomposition ..............................................................................10 2.2 Selecting an Architecture .....................................................................................................12 2.2.1 Multi-Tiered Architecture .............................................................................................12 2.2.2 Model-View-Controller (MVC) Architecture ..................................................................14 2.2.3 Architectural Patterns Applied to the Calculator ..........................................................15 2.2.4 Choosing the Calculator’s Architecture .......................................................................16 2.3 Interfaces .............................................................................................................................17 2.3.1 Calculator Use Cases ...................................................................................................18 2.3.2 Analysis of Use Cases ..................................................................................................21 2.3.3 A Quick Note on Actual Implementation ......................................................................27 2.4 Assessment of Our Current Design ......................................................................................28 2.5 Next Steps ............................................................................................................................28 v WOW! eBook www.wowebook.org Table of ConTenTs Chapter 3: The Stack ����������������������������������������������������������������������������������������������31 3.1 Decomposition of the Stack Module ....................................................................................31 3.2 T he Stack Class ....................................................................................................................34 3.2.1 The Singleton Pattern ..................................................................................................34 3.2.2 The Stack Module as a Singleton Class .......................................................................36 3.3 A dding Events ......................................................................................................................40 3.3.1 The Observer Pattern ...................................................................................................41 3.3.2 The Stack as an Event Publisher .................................................................................52 3.3.3 The Complete Stack Interface .....................................................................................53 3.4 A Quick Note on Testing .......................................................................................................54 Chapter 4: The Command Dispatcher ���������������������������������������������������������������������57 4.1 Decomposition of the Command Dispatcher ........................................................................57 4.2 T he Command Class ............................................................................................................58 4.2.1 The Command Pattern .................................................................................................58 4.2.2 More on Implementing Undo/Redo ..............................................................................59 4.2.3 The Command Pattern Applied to the Calculator .........................................................61 4.3 T he Command Repository ....................................................................................................81 4.3.1 The CommandRepository Class ...................................................................................81 4.3.2 Registering Core Commands .......................................................................................87 4.4 The Command Manager .......................................................................................................88 4.4.1 The Interface ...............................................................................................................88 4.4.2 Implementing Undo and Redo .....................................................................................89 4.5 The Command Dispatcher ....................................................................................................91 4.5.1 The Interface ...............................................................................................................92 4.5.2 Implementation Details ...............................................................................................93 4.6 Revisiting Earlier Decisions .................................................................................................95 vi WOW! eBook www.wowebook.org Table of ConTenTs Chapter 5: The Command Line Interface ����������������������������������������������������������������97 5.1 T he User Interface Abstraction .............................................................................................97 5.1.1 The Abstract Interface .................................................................................................98 5.1.2 User Interface Events ................................................................................................101 5.2 The Concrete CLI Class ......................................................................................................105 5.2.1 Requirements ............................................................................................................105 5.2.2 The CLI Design ...........................................................................................................107 5.3 Tying It Together: A Working Program ................................................................................112 Chapter 6: The Graphical User Interface ���������������������������������������������������������������115 6 .1 R equirements .....................................................................................................................115 6.2 B uilding GUIs ......................................................................................................................118 6.2.1 B uilding GUIs in IDEs .................................................................................................119 6.2.2 Building GUIs in Code ................................................................................................120 6.2.3 Which GUI Building Method Is Better? .......................................................................121 6.3 M odularization ...................................................................................................................122 6.3.1 T he CommandButton Abstraction ..............................................................................122 6.3.2 T he CommandButton Design .....................................................................................123 6.3.3 T he CommandButton Interface ..................................................................................127 6.3.4 G etting Input ..............................................................................................................129 6.3.5 T he Design of the InputWidget ..................................................................................129 6.3.6 The Interface of the InputWidget ...............................................................................131 6.4 T he Display.........................................................................................................................132 6.4.1 The Design of the Display Class ................................................................................134 6.4.2 A Poor Design ............................................................................................................134 6.4.3 An Improved Display Design ......................................................................................135 6.5 T he Model ..........................................................................................................................136 6.6 T he Display Redux ..............................................................................................................138 6.7 Tying It Together: The Main Window ...................................................................................139 vii WOW! eBook www.wowebook.org Table of ConTenTs 6.8 L ook-and-Feel ....................................................................................................................141 6.9 A Working Program ............................................................................................................142 6.10 A Microsoft Windows Build Note ......................................................................................144 Chapter 7: Plugins ������������������������������������������������������������������������������������������������145 7.1 What Is a Plugin? ...............................................................................................................145 7.1.1 Rules for C++ Plugins ...............................................................................................146 7.2 Problem 1: The Plugin Interface .........................................................................................149 7.2.1 The Interface for Discovering Commands .................................................................149 7.2.2 The Interface for Adding New GUI Buttons ................................................................154 7.2.3 P lugin Allocation and Deallocation ............................................................................156 7.2.4 T he Plugin Command Interface .................................................................................156 7.2.5 A PI Versioning ............................................................................................................159 7.2.6 M aking the Stack Available .......................................................................................160 7.3 Problem 2: Loading Plugins ...............................................................................................161 7.3.1 P latform-Specific Plugin Loading ..............................................................................162 7.3.2 Loading, Using, and Closing a Shared Library ...........................................................162 7.3.3 A Design for Multi-Platform Code ..............................................................................164 7.4 Problem 3: Retrofitting pdCalc ...........................................................................................177 7.4.1 M odule Interfaces ......................................................................................................178 7.4.2 Adding Plugin Buttons to the GUI ...............................................................................182 7.5 I ncorporating Plugins .........................................................................................................183 7.5.1 L oading Plugins .........................................................................................................184 7.5.2 I njecting Functionality ...............................................................................................185 7.6 A Concrete Plugin ...............................................................................................................187 7.6.1 P lugin Interface .........................................................................................................187 7.6.2 S ource Code Dependency Inversion ..........................................................................189 7.6.3 I mplementing HyperbolicLnPlugin’s Functionality .....................................................190 7.7 N ext Steps ..........................................................................................................................193 viii WOW! eBook www.wowebook.org Table of ConTenTs Chapter 8: New Requirements ������������������������������������������������������������������������������195 8.1 F ully Designed New Features ............................................................................................195 8.1.1 B atch Operation .........................................................................................................196 8.1.2 S tored Procedures .....................................................................................................197 8.2 Designs Toward a More Useful Calculator ..........................................................................211 8.2.1 C omplex Numbers .....................................................................................................211 8.2.2 V ariables ....................................................................................................................216 8.3 Some Interesting Extensions for Self-Exploration ..............................................................221 8.3.1 H igh DPI Scaling ........................................................................................................221 8.3.2 D ynamic Skinning ......................................................................................................221 8.3.3 F low Control...............................................................................................................222 8.3.4 A n Alternative GUI Layout ..........................................................................................222 8.3.5 A Graphing Calculator ................................................................................................222 8.3.6 A Plugin Management System ...................................................................................223 8.3.7 A Mobile Device Interface ..........................................................................................223 Appendix A: Acquiring, Building, and Executing pdCalc ���������������������������������������225 A .1 Getting the Source Code ....................................................................................................225 A.2 D ependencies ....................................................................................................................225 A.3 B uilding pdCalc ..................................................................................................................226 A.3.1 U sing Qt Creator ........................................................................................................227 A.3.2 Using the Command Line ..........................................................................................228 A.4 E xecuting pdCalc ...............................................................................................................229 A.4.1 U sing Qt Creator ........................................................................................................229 A.4.2 Using the Command Line ..........................................................................................229 A.5 T roubleshooting .................................................................................................................230 ix WOW! eBook www.wowebook.org Table of ConTenTs Appendix B: Organization of the Source Code ������������������������������������������������������233 B.1 T he src Directory ................................................................................................................233 B.1.1 T he pdCalc Directory .................................................................................................233 B.1.2 T he pdCalc-simple-cli Directory ................................................................................234 B.1.3 T he pdCalc-simple-gui Directory ...............................................................................234 B.1.4 T he utilities Directory ................................................................................................234 B.1.5 T he backend Directory ..............................................................................................234 B.1.6 T he cli Directory ........................................................................................................235 B.1.7 T he gui Directory .......................................................................................................236 B.1.8 T he plugins Directory ................................................................................................236 B.2 T he test Directory ..............................................................................................................236 B.2.1 T he testDriver Directory ............................................................................................237 B.2.2 T he utilitiesTest Directory ..........................................................................................237 B.2.3 T he backendTest Directory ........................................................................................237 B.2.4 T he cliTest Directory ..................................................................................................238 B.2.5 T he guiTest Directory .................................................................................................238 B.2.6 T he pluginsTest Directory ..........................................................................................238 References ������������������������������������������������������������������������������������������������������������239 Index ���������������������������������������������������������������������������������������������������������������������243 x WOW! eBook www.wowebook.org
Description: