Edward Sciore
Java Program Design Principles, Polymorphism, and Patterns
Edward Sciore
Newton, MA, USA
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484241424 . For more detailed information, please visit www.apress.com/source-code .
ISBN 978-1-4842-4142-4 e-ISBN 978-1-4842-4143-1
https://doi.org/10.1007/978-1-4842-4143-1
Library of Congress Control Number: 2018965461
Edward Sciore 2019
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.
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.
Introduction
This book is concerned with the following topic: How to use the object-oriented programming facilities of Java effectively. There are several reasons why you might find this book useful.
Perhaps you are one of the many Java programmers who dont know a lot about object orientation. You can use classes and write simple ones, and may even have written an interface or a subclass. But you dont really get the point of it. You dont see (let alone appreciate) the power and expressiveness of an object-oriented program design. This book will give you that understanding and appreciation.
Or perhaps you are comfortable with object-oriented concepts and appreciate the usefulness of polymorphism. You might have even dabbled with design patterns. They seem like a good idea, and everyone talks about them, but you dont really understand how to use them in your own programs. This book will show you the underlying intent of the patterns, how their techniques can become a seamless addition to your programming toolbox, and how you can adapt their solutions to fit the needs of a given situation.
The Java library has a vast number of classes. You are probably comfortable with some of these classes, such as String , ArrayList , and HashMap , but you may have encountered other classes whose intent seems obscure. For example, perhaps you were told that you should always combine a BufferedReader with a FileReader when reading from a text file, but you never understood why things have to be so complicated. And what about all the other Reader subclasseswhen should they be used? This book can help you make sense of the Java library. Many of its classes are organized around design patterns, and understanding the pattern helps to understand why the classes are the way they are and how they should be used.
Design patterns were introduced by Gamma, Held, Johnson, and Vlissides in their seminal 1994 book. The Java language has several features that support the use of these patterns. For example, the Iterable and Iterator interfaces support the iterator pattern, the Observable class and Observer interface support the observer pattern, and the Stream and Consumer interfaces support the visitor pattern. In addition, the availability of lambda expressions in Java makes it possible to reformulate several design patterns more simply and organically. I view this book as a modern treatment of design patterns because it presents them from the standpoint of modern Java. My goal is to show how design patterns arise naturally from the principles of object-oriented design. Although this book does not cover all design patterns, I am confident that it will give you the tools you need to more deeply understand other patterns you may read about.
This book illustrates object-oriented concepts by taking examples from the Java class library. Many of the library classes are well designed, and it is instructive to examine their designs and implementations. Some library classes have questionable designs, and it is equally instructive to examine their defects and consider alternatives.
The book also has an extensive running example, which is the design of a simplified banking program. The first version of the program appears in Chapter , the example will have expanded to encompass five programs, containing 53 classes and interfaces. I hope that you will enjoy following its development as you read the book, and appreciate how the different design concepts can interact to produce an elegant, nontrivial architecture.
Needless to say, these examples constitute a lot of code. To save space and enhance readability, not all of the code appears in the book. When a class is being discussed, only the portion of the class relevant to the discussion is shown. Import declarations are always omitted.
A good way to examine the example code is to use a Java editor or IDE. Complete working versions of all files mentioned in the text are available from the Apress website, and I encourage you to download them. Included with the files is a README file that explains the scope of the code and how to install it. Each package also has a README file that explains the purpose of its source files. While writing this book, I found it useful (and often necessary) to have the code in an IDE on my computer so that I could consult it and execute it. I expect that the source code files will be equally useful to you as you read the book.
Acknowledgments
My interest in object-oriented programming began in the mid-1980s, and steadily increased over the years as the discipline developed and matured. I am indebted to those who helped introduce the big ideas along the way, as well as those who filled in the gaps. I view this book as a consolidation of the current state of the discipline, and hope it will help others appreciate the power and potential of the object-oriented approach to programming.
My editor at Apress, Jonathan Gennick, has been unconditionally supportive during the entire process. I think of him as a friend that I have never met. Perhaps some day....