Edward Sciore
Java Program DesignPrinciples, Polymorphism, and Patterns
Edward Sciore
Newton, MA, USA
ISBN 978-1-4842-4142-4e-ISBN 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 [email protected], 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 don’t 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 don’t really get the point of it. You don’t 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 don’t 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 subclasses—when 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 1 , and consists of a single, non–object-oriented class. New versions are created throughout the book, as each new concept is introduced. By the end of Chapter 11 , 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....

My daughter, Leah Schneider, wanted to know more about object orientation for her job, so I had her preview an early draft of the book. Her enthusiasm for each chapter was gratifying. I appreciate the time she put in and the comments she made. She is wonderful. She is also a stickler for good grammar and mercilessly hounded me about my inappropriate overuse of commas. She was, of course, correct, and I did my best to tighten things up, except perhaps for this sentence, where I couldn’t help myself.

Finally, I need to acknowledge my wife, Amy Schneider, who supported me far more than she realizes—through discussions, proofreading, explaining comma usage, and by simply being there. Thanks for everything.

Table of Contents

Index 445

About the Author and About the Technical Reviewer

About the Author

Edward Sciore
../images/470600_1_En_BookFrontmatter_Figb_HTML.jpg

is a recently retired Associate Professor at Boston College, who gleefully taught computer science to college students for over 35 years. This book is the result of his experiences teaching courses on Java programming, object-oriented design, and software engineering. Edward is author of Understanding Oracle APEX 5 Application Development (Apress, 2015) and Database Design and Implementation (Wiley, 2008).

 

About the Technical Reviewer

Alexandru Jecan
../images/470600_1_En_BookFrontmatter_Figc_HTML.jpg

is a senior software engineer, consultant, author, trainer and speaker currently residing in Munich, Germany. He earned a degree in computer science from the Technical University of Cluj-Napoca, Romania. Alexandru is the author of the “Java 9 Modularity Revealed” book and is currently writing another book on Eclipse MicroProfile - all for Apress. He speaks at tech conferences and user groups, both in Europe and the United States, on different topics related to software development and software technologies. He is also involved in the Java community. In his spare time, Alexandru likes to spend time with his wonderful wife. Whenever he gets some free time, he likes to read and to develop tools for the GitHub Marketplace. He also likes to play sports like soccer and tennis and to go skiing in the mountains. You can follow Alexandru on Twitter at @alexandrujecan , read his tech blog at www.alexandrujecan.com , or email him at [email protected] . If you would like to join Alexandru in his endeavor to build world class tools that enhance work productivity, feel free to contact him. He’ll be glad to discuss with you.

 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.226.120