Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

Java Design Patterns A tour of 23 gang

of four design patterns in Java 1st


Edition Vaskaran Sarcar
Visit to download the full and correct content document:
https://textbookfull.com/product/java-design-patterns-a-tour-of-23-gang-of-four-design
-patterns-in-java-1st-edition-vaskaran-sarcar/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

Java design patterns a tour of 23 gang of four design


patterns in Java 1st Edition Vaskaran Sarcar

https://textbookfull.com/product/java-design-patterns-a-tour-
of-23-gang-of-four-design-patterns-in-java-1st-edition-vaskaran-
sarcar-2/

Java Design Patterns: A Hands-On Experience with Real-


World Examples 2nd Edition Vaskaran Sarcar

https://textbookfull.com/product/java-design-patterns-a-hands-on-
experience-with-real-world-examples-2nd-edition-vaskaran-sarcar/

Design Patterns in C#: A Hands-on Guide with Real-world


Examples 2nd Edition Vaskaran Sarcar

https://textbookfull.com/product/design-patterns-in-c-a-hands-on-
guide-with-real-world-examples-2nd-edition-vaskaran-sarcar/

Interactive C#: Fundamentals, Core Concepts and


Patterns 1st Edition Vaskaran Sarcar

https://textbookfull.com/product/interactive-c-fundamentals-core-
concepts-and-patterns-1st-edition-vaskaran-sarcar/
Microservices Patterns With examples in Java 1st
Edition Chris Richardson

https://textbookfull.com/product/microservices-patterns-with-
examples-in-java-1st-edition-chris-richardson/

Microservices Patterns With examples in Java 1st


Edition Chris Richardson

https://textbookfull.com/product/microservices-patterns-with-
examples-in-java-1st-edition-chris-richardson-2/

Design Patterns by Tutorials Learning design patterns


in Swift 4 2 Joshua Greene

https://textbookfull.com/product/design-patterns-by-tutorials-
learning-design-patterns-in-swift-4-2-joshua-greene/

Design Patterns by Tutorials Third Edition Learning


Design Patterns in Swift Raywenderlich Tutorial Team

https://textbookfull.com/product/design-patterns-by-tutorials-
third-edition-learning-design-patterns-in-swift-raywenderlich-
tutorial-team/

Design of New Weave Patterns 1st Edition Angelova

https://textbookfull.com/product/design-of-new-weave-
patterns-1st-edition-angelova/
T HE E X P ER T ’S VOIC E ® IN J AVA

Java Design
Patterns
A Tour with 23 Gang of Four Design
Patterns in Java

Vaskaran Sarcar

www.it-ebooks.info
Java Design Patterns
A Tour of 23 Gang of Four
Design Patterns in Java

Vaskaran Sarcar

www.it-ebooks.info
Java Design Patterns: A tour of 23 gang of four design patterns in Java
Copyright © 2016 by Vaskaran Sarcar
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. Exempted from this legal reservation are brief excerpts in connection with
reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed
on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or
parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its
current version, and permission for use must always be obtained from Springer. Permissions for use may be
obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under
the respective Copyright Law.
ISBN-13 (pbk): 978-1-4842-1801-3
ISBN-13 (electronic): 978-1-4842-1802-0
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.
Managing Director: Welmoed Spahr
Lead Editor: Pramila Balan
Technical Reviewers: Anupam Chakraborty and Shekar Maravi
Editorial Board: Steve Anglin, Pramila Balan, Louise Corrigan, Jonathan Gennick, Robert Hutchinson,
Celestin Suresh John, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie,
Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing
Coordinating Editor: Prachi Mehta
Copy Editor: Karen Jameson
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Nature, 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 rights@apress.com, or visit www.apress.com.
Apress and friends of ED books 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 Special
Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available to
readers at www.apress.com/9781484218013. For detailed information about how to locate your book’s source
code, go to www.apress.com/source-code/. Readers can also access source code at SpringerLink in the
Supplementary Material section for each chapter.

www.it-ebooks.info
Dedicated to
Almighty GOD
My Family
and
The Gang of Four
You are my inspiration.

www.it-ebooks.info
Contents at a Glance

About the Author���������������������������������������������������������������������������������������������������xvii


Acknowledgments��������������������������������������������������������������������������������������������������xix
Preface�������������������������������������������������������������������������������������������������������������������xxi
Key Points�������������������������������������������������������������������������������������������������������������xxiii

■■Chapter 1: Introduction����������������������������������������������������������������������������������������� 1

■Chapter 2: Observer Patterns�������������������������������������������������������������������������������� 3

■Chapter 3: Singleton Patterns������������������������������������������������������������������������������ 17

■Chapter 4: Proxy Patterns����������������������������������������������������������������������������������� 23

■Chapter 5: Decorator Patterns����������������������������������������������������������������������������� 27

■Chapter 6: Template Method Patterns����������������������������������������������������������������� 33

■Chapter 7: Strategy Patterns (Or, Policy Patterns)����������������������������������������������� 39

■Chapter 8: Adapter Patterns�������������������������������������������������������������������������������� 47

■Chapter 9: Command Patterns����������������������������������������������������������������������������� 53

■Chapter 10: Iterator Patterns������������������������������������������������������������������������������� 59

■Chapter 11: Facade Patterns������������������������������������������������������������������������������� 67

■Chapter 12: Factory Method Patterns������������������������������������������������������������������ 73

■Chapter 13: Memento Patterns���������������������������������������������������������������������������� 77

■Chapter 14: State Patterns���������������������������������������������������������������������������������� 83

■Chapter 15: Builder Patterns������������������������������������������������������������������������������� 89

■Chapter 16: Flyweight Patterns��������������������������������������������������������������������������� 97

www.it-ebooks.info
■ Contents at a Glance


■Chapter 17: Abstract Factory Patterns�������������������������������������������������������������� 109

■Chapter 18: Mediator Patterns�������������������������������������������������������������������������� 115

■Chapter 19: Prototype Patterns������������������������������������������������������������������������� 123

■Chapter 20: Chain of Responsibility Patterns���������������������������������������������������� 129

■Chapter 21: Composite Patterns������������������������������������������������������������������������ 135

■Chapter 22: Bridge Patterns (Or Handle/Body Patterns)����������������������������������� 141

■Chapter 23: Visitor Patterns������������������������������������������������������������������������������ 149

■Chapter 24: Interpreter Patterns����������������������������������������������������������������������� 155

■Appendix A: FAQ������������������������������������������������������������������������������������������������ 163

Index��������������������������������������������������������������������������������������������������������������������� 169

vi

www.it-ebooks.info
Contents

About the Author���������������������������������������������������������������������������������������������������xvii


Acknowledgments��������������������������������������������������������������������������������������������������xix
Preface�������������������������������������������������������������������������������������������������������������������xxi
Key Points�������������������������������������������������������������������������������������������������������������xxiii

■■Chapter 1: Introduction����������������������������������������������������������������������������������������� 1

■Chapter 2: Observer Patterns�������������������������������������������������������������������������������� 3
Concept���������������������������������������������������������������������������������������������������������������������������� 3
Real-Life Example������������������������������������������������������������������������������������������������������������ 3
Computer World Example������������������������������������������������������������������������������������������������� 3
Illustration������������������������������������������������������������������������������������������������������������������������ 3
UML Class Diagram���������������������������������������������������������������������������������������������������������� 4
Package Explorer view����������������������������������������������������������������������������������������������������� 5
Implementation���������������������������������������������������������������������������������������������������������������� 5
Output������������������������������������������������������������������������������������������������������������������������������� 7
Note���������������������������������������������������������������������������������������������������������������������������������� 7
UML Class Diagram���������������������������������������������������������������������������������������������������������� 8
Package Explorer view����������������������������������������������������������������������������������������������������� 8
Implementation���������������������������������������������������������������������������������������������������������������� 9
Output����������������������������������������������������������������������������������������������������������������������������� 11
Assignment�������������������������������������������������������������������������������������������������������������������� 11

vii

www.it-ebooks.info
■ Contents

UML Class Diagram�������������������������������������������������������������������������������������������������������� 11


Implementation�������������������������������������������������������������������������������������������������������������� 12
Output����������������������������������������������������������������������������������������������������������������������������� 15

■Chapter 3: Singleton Patterns������������������������������������������������������������������������������ 17
Concept�������������������������������������������������������������������������������������������������������������������������� 17
Real-Life Example���������������������������������������������������������������������������������������������������������� 17
Computer World Example����������������������������������������������������������������������������������������������� 17
Illustration���������������������������������������������������������������������������������������������������������������������� 17
UML Class Diagram�������������������������������������������������������������������������������������������������������� 18
Package Explorer view��������������������������������������������������������������������������������������������������� 18
Implementation�������������������������������������������������������������������������������������������������������������� 18
Output����������������������������������������������������������������������������������������������������������������������������� 19
Note�������������������������������������������������������������������������������������������������������������������������������� 20

■Chapter 4: Proxy Patterns����������������������������������������������������������������������������������� 23
Concept�������������������������������������������������������������������������������������������������������������������������� 23
Real–Life Example���������������������������������������������������������������������������������������������������������� 23
Computer World Example����������������������������������������������������������������������������������������������� 23
Illustration���������������������������������������������������������������������������������������������������������������������� 23
UML Class Diagram�������������������������������������������������������������������������������������������������������� 24
Package Explorer view��������������������������������������������������������������������������������������������������� 24
Implementation�������������������������������������������������������������������������������������������������������������� 25
Output����������������������������������������������������������������������������������������������������������������������������� 26
Note�������������������������������������������������������������������������������������������������������������������������������� 26

■Chapter 5: Decorator Patterns����������������������������������������������������������������������������� 27
Concept�������������������������������������������������������������������������������������������������������������������������� 27
Real-Life Example���������������������������������������������������������������������������������������������������������� 27
Computer World Example����������������������������������������������������������������������������������������������� 27
Illustration���������������������������������������������������������������������������������������������������������������������� 27

viii

www.it-ebooks.info
■ Contents

UML Class Diagram�������������������������������������������������������������������������������������������������������� 28


Package Explorer view��������������������������������������������������������������������������������������������������� 29
Implementation�������������������������������������������������������������������������������������������������������������� 29
Output����������������������������������������������������������������������������������������������������������������������������� 31
Note�������������������������������������������������������������������������������������������������������������������������������� 31

■Chapter 6: Template Method Patterns����������������������������������������������������������������� 33
Concept�������������������������������������������������������������������������������������������������������������������������� 33
Real-Life Example���������������������������������������������������������������������������������������������������������� 33
Computer World Example����������������������������������������������������������������������������������������������� 33
Illustration���������������������������������������������������������������������������������������������������������������������� 33
UML Class Diagram�������������������������������������������������������������������������������������������������������� 34
Package Explorer view��������������������������������������������������������������������������������������������������� 35
Implementation�������������������������������������������������������������������������������������������������������������� 35
Output����������������������������������������������������������������������������������������������������������������������������� 37
Note�������������������������������������������������������������������������������������������������������������������������������� 37

■Chapter 7: Strategy Patterns (Or, Policy Patterns)����������������������������������������������� 39
Concept�������������������������������������������������������������������������������������������������������������������������� 39
Real–Life Example���������������������������������������������������������������������������������������������������������� 39
Computer World Example����������������������������������������������������������������������������������������������� 39
Illustration���������������������������������������������������������������������������������������������������������������������� 39
UML Class Diagram�������������������������������������������������������������������������������������������������������� 40
Package Explorer view��������������������������������������������������������������������������������������������������� 41
Implementation�������������������������������������������������������������������������������������������������������������� 41
Output����������������������������������������������������������������������������������������������������������������������������� 44
Note�������������������������������������������������������������������������������������������������������������������������������� 45

ix

www.it-ebooks.info
■ Contents


■Chapter 8: Adapter Patterns�������������������������������������������������������������������������������� 47
Concept�������������������������������������������������������������������������������������������������������������������������� 47
Real–Life Example���������������������������������������������������������������������������������������������������������� 47
Computer World Example����������������������������������������������������������������������������������������������� 47
Illustration���������������������������������������������������������������������������������������������������������������������� 47
UML Class Diagram�������������������������������������������������������������������������������������������������������� 48
Package Explorer view��������������������������������������������������������������������������������������������������� 49
Implementation�������������������������������������������������������������������������������������������������������������� 49
Output����������������������������������������������������������������������������������������������������������������������������� 50
Note�������������������������������������������������������������������������������������������������������������������������������� 51
Illustration���������������������������������������������������������������������������������������������������������������������� 51
Output����������������������������������������������������������������������������������������������������������������������������� 52

■Chapter 9: Command Patterns����������������������������������������������������������������������������� 53
Concept�������������������������������������������������������������������������������������������������������������������������� 53
Real-Life Example���������������������������������������������������������������������������������������������������������� 53
Computer World Example����������������������������������������������������������������������������������������������� 53
Illustration���������������������������������������������������������������������������������������������������������������������� 53
UML Class Diagram�������������������������������������������������������������������������������������������������������� 54
Package Explorer view��������������������������������������������������������������������������������������������������� 55
Implementation�������������������������������������������������������������������������������������������������������������� 55
Output����������������������������������������������������������������������������������������������������������������������������� 57
Note�������������������������������������������������������������������������������������������������������������������������������� 57

■Chapter 10: Iterator Patterns������������������������������������������������������������������������������� 59
Concept�������������������������������������������������������������������������������������������������������������������������� 59
Real-Life Example���������������������������������������������������������������������������������������������������������� 59
Computer World Example����������������������������������������������������������������������������������������������� 59
Illustration���������������������������������������������������������������������������������������������������������������������� 59
UML Class Diagram�������������������������������������������������������������������������������������������������������� 60

www.it-ebooks.info
■ Contents

Package Explorer view��������������������������������������������������������������������������������������������������� 61


Implementation�������������������������������������������������������������������������������������������������������������� 61
Output����������������������������������������������������������������������������������������������������������������������������� 65
Note�������������������������������������������������������������������������������������������������������������������������������� 65

■Chapter 11: Facade Patterns������������������������������������������������������������������������������� 67
Concept�������������������������������������������������������������������������������������������������������������������������� 67
Real-Life Example���������������������������������������������������������������������������������������������������������� 67
Computer World Example����������������������������������������������������������������������������������������������� 67
Illustration���������������������������������������������������������������������������������������������������������������������� 67
UML Class Diagram�������������������������������������������������������������������������������������������������������� 68
Package Explorer view��������������������������������������������������������������������������������������������������� 68
Implementation�������������������������������������������������������������������������������������������������������������� 69
Output����������������������������������������������������������������������������������������������������������������������������� 71
Note�������������������������������������������������������������������������������������������������������������������������������� 71

■Chapter 12: Factory Method Patterns������������������������������������������������������������������ 73
Concept�������������������������������������������������������������������������������������������������������������������������� 73
Real-Life Example���������������������������������������������������������������������������������������������������������� 73
Computer World Example����������������������������������������������������������������������������������������������� 73
Illustration���������������������������������������������������������������������������������������������������������������������� 73
UML Class Diagram�������������������������������������������������������������������������������������������������������� 74
Package Explorer view��������������������������������������������������������������������������������������������������� 74
Implementation�������������������������������������������������������������������������������������������������������������� 75
Output����������������������������������������������������������������������������������������������������������������������������� 76
Note�������������������������������������������������������������������������������������������������������������������������������� 76

■Chapter 13: Memento Patterns���������������������������������������������������������������������������� 77
Concept�������������������������������������������������������������������������������������������������������������������������� 77
Real-Life Example���������������������������������������������������������������������������������������������������������� 77
Computer World Example����������������������������������������������������������������������������������������������� 77

xi

www.it-ebooks.info
■ Contents

Illustration���������������������������������������������������������������������������������������������������������������������� 77
UML Class Diagram�������������������������������������������������������������������������������������������������������� 78
Package Explorer view��������������������������������������������������������������������������������������������������� 79
Implementation�������������������������������������������������������������������������������������������������������������� 79
Output����������������������������������������������������������������������������������������������������������������������������� 81
Note�������������������������������������������������������������������������������������������������������������������������������� 81

■Chapter 14: State Patterns���������������������������������������������������������������������������������� 83
Concept�������������������������������������������������������������������������������������������������������������������������� 83
Real-Life Example���������������������������������������������������������������������������������������������������������� 83
Computer World Example����������������������������������������������������������������������������������������������� 83
Illustration���������������������������������������������������������������������������������������������������������������������� 83
UML Class Diagram�������������������������������������������������������������������������������������������������������� 84
Package Explorer view��������������������������������������������������������������������������������������������������� 85
Implementation�������������������������������������������������������������������������������������������������������������� 85
Output����������������������������������������������������������������������������������������������������������������������������� 87
Note�������������������������������������������������������������������������������������������������������������������������������� 87

■Chapter 15: Builder Patterns������������������������������������������������������������������������������� 89
Concept�������������������������������������������������������������������������������������������������������������������������� 89
Real-Life Example���������������������������������������������������������������������������������������������������������� 89
Computer World Example����������������������������������������������������������������������������������������������� 89
Illustration���������������������������������������������������������������������������������������������������������������������� 89
UML Class Diagram�������������������������������������������������������������������������������������������������������� 90
Package Explorer view��������������������������������������������������������������������������������������������������� 91
Implementation�������������������������������������������������������������������������������������������������������������� 92
Output����������������������������������������������������������������������������������������������������������������������������� 94
Note�������������������������������������������������������������������������������������������������������������������������������� 95

xii

www.it-ebooks.info
■ Contents


■Chapter 16: Flyweight Patterns��������������������������������������������������������������������������� 97
Concept�������������������������������������������������������������������������������������������������������������������������� 97
Real-Life Example���������������������������������������������������������������������������������������������������������� 97
Computer World Example����������������������������������������������������������������������������������������������� 97
Illustration���������������������������������������������������������������������������������������������������������������������� 97
UML Class Diagram�������������������������������������������������������������������������������������������������������� 98
Package Explorer view��������������������������������������������������������������������������������������������������� 98
Implementation�������������������������������������������������������������������������������������������������������������� 99
Output��������������������������������������������������������������������������������������������������������������������������� 102
Improvement to the program��������������������������������������������������������������������������������������� 102
UML Class Diagram������������������������������������������������������������������������������������������������������ 103
Package Explorer view������������������������������������������������������������������������������������������������� 104
Implementation������������������������������������������������������������������������������������������������������������ 104
Output��������������������������������������������������������������������������������������������������������������������������� 107
Note������������������������������������������������������������������������������������������������������������������������������ 107

■Chapter 17: Abstract Factory Patterns�������������������������������������������������������������� 109
Concept������������������������������������������������������������������������������������������������������������������������ 109
Real-Life Example�������������������������������������������������������������������������������������������������������� 109
Computer World Example��������������������������������������������������������������������������������������������� 109
Illustration�������������������������������������������������������������������������������������������������������������������� 109
UML Class Diagram������������������������������������������������������������������������������������������������������ 110
Package Explorer view������������������������������������������������������������������������������������������������� 111
Implementation������������������������������������������������������������������������������������������������������������ 111
Output��������������������������������������������������������������������������������������������������������������������������� 114
Note������������������������������������������������������������������������������������������������������������������������������ 114

xiii

www.it-ebooks.info
■ Contents


■Chapter 18: Mediator Patterns�������������������������������������������������������������������������� 115
Concept������������������������������������������������������������������������������������������������������������������������ 115
Real-Life Example�������������������������������������������������������������������������������������������������������� 115
Computer World Example��������������������������������������������������������������������������������������������� 115
Illustration�������������������������������������������������������������������������������������������������������������������� 116
UML Class Diagram������������������������������������������������������������������������������������������������������ 116
Package Explorer view������������������������������������������������������������������������������������������������� 117
Implementation������������������������������������������������������������������������������������������������������������ 117
Output��������������������������������������������������������������������������������������������������������������������������� 121
Note������������������������������������������������������������������������������������������������������������������������������ 121

■Chapter 19: Prototype Patterns������������������������������������������������������������������������� 123
Concept������������������������������������������������������������������������������������������������������������������������ 123
Real-Life Example�������������������������������������������������������������������������������������������������������� 123
Computer World Example��������������������������������������������������������������������������������������������� 123
Illustration�������������������������������������������������������������������������������������������������������������������� 124
UML Class Diagram������������������������������������������������������������������������������������������������������ 124
Package Explorer view������������������������������������������������������������������������������������������������� 125
Implementation������������������������������������������������������������������������������������������������������������ 125
Output��������������������������������������������������������������������������������������������������������������������������� 127
Note������������������������������������������������������������������������������������������������������������������������������ 128

■Chapter 20: Chain of Responsibility Patterns���������������������������������������������������� 129
Concept������������������������������������������������������������������������������������������������������������������������ 129
Real-Life Example�������������������������������������������������������������������������������������������������������� 129
Computer World Example��������������������������������������������������������������������������������������������� 129
Illustration�������������������������������������������������������������������������������������������������������������������� 130
UML Class Diagram������������������������������������������������������������������������������������������������������ 130

xiv

www.it-ebooks.info
■ Contents

Package Explorer view������������������������������������������������������������������������������������������������� 131


Implementation������������������������������������������������������������������������������������������������������������ 131
Output��������������������������������������������������������������������������������������������������������������������������� 134
Note������������������������������������������������������������������������������������������������������������������������������ 134

■Chapter 21: Composite Patterns������������������������������������������������������������������������ 135
Concept������������������������������������������������������������������������������������������������������������������������ 135
Real-Life Example�������������������������������������������������������������������������������������������������������� 135
Computer World Example��������������������������������������������������������������������������������������������� 135
Illustration�������������������������������������������������������������������������������������������������������������������� 135
UML Class Diagram������������������������������������������������������������������������������������������������������ 136
Package Explorer view������������������������������������������������������������������������������������������������� 137
Implementation������������������������������������������������������������������������������������������������������������ 137
Output��������������������������������������������������������������������������������������������������������������������������� 140
Note������������������������������������������������������������������������������������������������������������������������������ 140

■Chapter 22: Bridge Patterns (Or Handle/Body Patterns)����������������������������������� 141
Concept������������������������������������������������������������������������������������������������������������������������ 141
Real-Life Example�������������������������������������������������������������������������������������������������������� 141
Computer World Example��������������������������������������������������������������������������������������������� 141
Illustration�������������������������������������������������������������������������������������������������������������������� 142
UML Class Diagram������������������������������������������������������������������������������������������������������ 142
Package Explorer view������������������������������������������������������������������������������������������������� 143
Implementation������������������������������������������������������������������������������������������������������������ 143
Output��������������������������������������������������������������������������������������������������������������������������� 146
Note������������������������������������������������������������������������������������������������������������������������������ 146

xv

www.it-ebooks.info
■ Contents


■Chapter 23: Visitor Patterns������������������������������������������������������������������������������ 149
Concept������������������������������������������������������������������������������������������������������������������������ 149
Real-Life Example�������������������������������������������������������������������������������������������������������� 149
Computer World Example��������������������������������������������������������������������������������������������� 149
Illustration�������������������������������������������������������������������������������������������������������������������� 149
UML Class Diagram������������������������������������������������������������������������������������������������������ 150
Package Explorer view������������������������������������������������������������������������������������������������� 151
Implementation������������������������������������������������������������������������������������������������������������ 151
Output��������������������������������������������������������������������������������������������������������������������������� 153
Note������������������������������������������������������������������������������������������������������������������������������ 153

■Chapter 24: Interpreter Patterns����������������������������������������������������������������������� 155
Concept������������������������������������������������������������������������������������������������������������������������ 155
Real–Life Example�������������������������������������������������������������������������������������������������������� 155
Computer World Example��������������������������������������������������������������������������������������������� 155
Illustration�������������������������������������������������������������������������������������������������������������������� 155
UML Class Diagram������������������������������������������������������������������������������������������������������ 156
Package Explorer view������������������������������������������������������������������������������������������������� 157
Implementation������������������������������������������������������������������������������������������������������������ 158
Output��������������������������������������������������������������������������������������������������������������������������� 161
Note������������������������������������������������������������������������������������������������������������������������������ 161

■Appendix A: FAQ������������������������������������������������������������������������������������������������ 163
References������������������������������������������������������������������������������������������������������������������� 164

Index��������������������������������������������������������������������������������������������������������������������� 169

xvi

www.it-ebooks.info
About the Author

Vaskaran Sarcar, ME (Software Engineering), MCA, B Sc. (Math) is a Senior Software Engineer at Hewlett
Packard India Software Operation Pvt. Ltd. He has been working in the HP India PPS R&D Hub since
August 2009. He is an alumnus of prestigious institutions like Jadavpur University, Kolkata, WB, Vidyasagar
University, Midnapore, WB and Presidency College, Kolkata, WB, Jodhpur Park Boys School, Kolkata, WB
and Ramakrishna Mission Vidyalaya, Narendrapur, Kolkata, WB. He is also the author of the following
books: Design Patterns in C#, Operating System: Computer Science Interview Series, C# Basics: Test Your Skill,
and Easy Manifestation. He devoted his early years (2005–2007) to teaching in various engineering colleges.
Later he received the MHRD-GATE Scholarship (India). Reading and learning new things are passions for
him. You can reach him at: vaskaran@rediffmail.com.

xvii

www.it-ebooks.info
Chapter 2 ■ Observer Patterns

Implementation
package observer.pattern.demo2;
import java.util.*;

interface IObserver
{
void update(int i);
}
class Observer1 implements IObserver
{
@Override
public void update(int i)
{
System.out.println("Observer1: myValue in Subject is now: "+i);
}
}
class Observer2 implements IObserver
{
@Override
public void update(int i)
{
System.out.println("Observer2: observes ->myValue is changed in
Subject to :"+i);
}
}

interface ISubject
{
void register(IObserver o);
void unregister(IObserver o);
void notifyObservers(int i);
}

class Subject implements ISubject


{
private int myValue;

public int getMyValue() {


return myValue;
}

public void setMyValue(int myValue) {


this.myValue = myValue;
//Notify observers
notifyObservers(myValue);
}

www.it-ebooks.info
Another random document with
no related content on Scribd:
Ayoapó, 236
Ayolas, Juan de, 333, 334
Azogues, 117, 133
Azuay, 116, 117, 132, 133, 139, 145

Babahoya, 117, 139;


R., 127
Bahia, (Ec.), 136;
(Brazil), 374, 375, 381, 387, 388, 397, 398, 400, 406, 412, 415,
416, 417, 419, 428, 429, 430, 432
Bahia de Caraquez, 136, 137
Bahia Blanca, 269, 301, 302, 303, 307, 309, 310, 312, 314, 316,
318, 325, 326
Bahia Honda, 36
Bajada Grande, 305
Balatá, 43, 89, 107, 110
Balmaceda, 246
Balsa, 226, 227
Balzar R., 127
Ballenita, 131, 136, 144
Ballivián, 210
Bananas, 41, 111
Banco, 34, 36
Baños, 139
Baquedano, 266
Baragua R., 65
Barbacoas, 26, 36, 43
Barcelona, 55, 68, 79, 82, 88, 93, 94
Barima, 108
Barinas, 55, 71, 84, 94
Barquisimeto, 55, 59, 66, 81, 83, 84, 91
Barranca Bermeja, 47
Barrancas, 76, 91
Barranqueras, 329
Barranquilla, 10, 12, 13, 22, 30, 32, 33, 35, 47, 51
Barretos, 424
Baudó, Serranía de, 15, 17, 18
Baurés R., 383
Baurú, 409
Bauxite, 99, 107, 111
Bayovar, 163
Beagle Channel, 300
Beans, 88
Beer, 329
Belém, 375, 402
Belgrano, 303
Bello Horizonte, 375, 405, 408
Benalcazar, Sebastian de, 115
Beni, See El Beni
Beni R., 159, 212, 218, 219, 224, 227, 237, 242, 383, 384, 413
Berbice R., 103, 104
Bermejo R., 212, 226, 284, 288, 289, 312
Bermudez Asphalt Lake, 69, 94, 95
Berrío Puerto, 35, 36
Bio-Bio, 247, 259;
R., 252, 263, 275, 277
Bismuth, 201, 235
Bitumen, 108
Blanco Cape, 122
Blancos, 355
Bobures, 82, 87
Boca de Ceniza, 30
Boca de Navios, 61
Bocas del Drago, 69
Bocono, 72
Bodega Central, 34
Bodegas, 139;
R., 127
Bogotá, 8, 9, 10, 12, 13, 18, 19, 20, 28, 32, 33, 34, 36, 37, 38, 46,
51
Bolívar, (Col.), 9, 10, 22, 24,
(Ven.), 55, 72, 73, 74, 84, 88, 89,
(Ec.), 116, 117, 133
Bolívar Ciudad, 27, 31, 55, 73, 78, 83, 90, 92
Bolívar, Gen., 8, 54, 150
Bolivia, 4, 14, 53, 114, 148, 150, 158, 159, 205-244, 248, 292,
306, 309, 310, 332, 383, 409, 412, 413, 455
Boquete, 267
Borax, 200, 268, 275
Borja, 124, 138,
(Par.), 342
Boyacá, 9, 10, 12, 27, 37, 54
Bragança, 402, 412; 408
Brazil, 1, 2, 4, 61, 101, 112, 115, 192, 218, 242, 245, 289, 331,
333, 334, 339, 342, 343, 355, 372-434, 452, 453
Brazo, 76
Bucaramanga, 10, 28, 34, 35, 37, 38
Bucay, 136
Buenaventura, 12, 24, 26, 30, 31, 36, 37, 48
Buenos Aires, 175, 223, 280, 281, 282, 283, 284, 285, 287, 288,
289, 290, 291, 298, 301, 305, 307, 308, 309, 310, 312, 316,
317, 318, 319, 320, 322, 325, 326, 327, 328, 329, 330, 331,
334, 343, 350, 355, 361, 364, 403, 411, 454, 456, 457
Buenos Aires Lake, 288, 300, 313
Buga, 36, 38

Caapacá, 352
Caazapá, 335
Cabedello, 400, 412, 417
Cabellos, 367
Cabildo, 265
Cabral, Pedro Alvarez, 373
Cacao, 42, 86, 106, 110, 141, 188, 241, 415
Cacequy, 392, 411
Caceres, 34
Cachaca, 416
Cacheuta, 326
Cailloma, 168, 200
Cajabamba, 165, 198
Cajamarca, 149, 152, 161, 164, 169, 184, 193, 197, 200
Calabozo, 55, 70, 71
Calacoto, 238
Calama, 267, 272
Calamar, 10, 30, 32, 34
Calamara R., 127
Calantura, 182
Caldas, 10, 12, 23, 35, 40, 50
Caldera, 264, 265, 266
Calera, 265
Caleta Coloso, 266
Cali, 10, 13, 25, 34, 36, 37, 45, 46, 48
Caliche, 270
Callao, 152, 154, 158, 162, 167, 175, 176, 177, 188, 261
Callapó, 227
Camaná, 168
Camaquám, 430
Camaquán Lake, 62
Cambao, 38
Camerones Bay, 304
Camocím, 412
Campana, 301, 318
Campos, 397, 411, 416, 429
Candelaria, 313, 334
Canelas, 139
Canelones, 356, 360
Cañar, 116, 117, 133
Cañete, 186
Caño Macareo, 78
Caño Pedernales, 76
Caoba, 90
Capure, 95
Caquetá, 10, 26;
R., 17
Carabaya, 199
Carabobo, 54, 55, 66, 70, 77, 78, 91, 93
Caracas, 54, 55, 57, 60, 63, 77, 79, 80, 83
Caracoles, 234
Carache, 72
Caraguatay, 335
Carapeguá, 342
Caraquez Bay, 131
Carara V., 37
Caráz, 166, 167
Carchi, 116, 117, 133
Carenero, 79, 80
Carhuáz, 166
Carhue, 310
Cariaco Gulf, 68
Carmen de Patagones, 310, 330
Carnaüba Wax, 423
Caroni R., 61, 73, 74, 92
Carora R., 65
Cartagena, 10, 12, 22, 30, 31, 32, 34, 35, 38
Cartago, 25, 34, 36, 39
Cartavio, 186
Carúpano, 69, 77, 83, 84, 94
Carrizal, 265, 273
Casanare, 27, 38, 48
Casapalca, 196
Casiquiare R., 17, 26, 62, 76, 89
Casma, 167
Castilletes, 82, 94
Castro, 277;
Gen., 54, 69
Catacaos, 163
Catamarca, 283, 294, 321, 322, 324
Catatumbo R., 16, 28, 31, 48, 62, 64, 81, 84
Cattle, 44, 91, 108, 111, 143, 193, 240, 277, 278, 315, 350, 368,
391, 424, 425, 426
Cauca, 9, 10, 12, 25, 40, 41;
R., 16, 23, 32, 34, 35, 38, 50;
V., 15, 18, 19, 31, 35, 37, 38, 39, 42
Cauquenes, 247
Caura R., 61, 74, 90
Cautín, 247, 259, 276
Cayapas Indians, 118, 119
Cayenne, 112, 113
Cayo, 136
Ceará, 375, 400, 412, 421, 423, 428, 429
Central Valley, 257, 258, 264
Centro, 66, 67
Cerro Azul, 168
Cerro de Pasco, 152, 159, 170, 179, 182, 195, 198, 200
Cerro Largo, 356, 357, 363, 370
Cerro Potosí, 232, 233
Cerro Quespesisa, 199
César R., 34, 36
Christ of the Andes, 296
Church, George Earl, 412
Cicapra R., 93
Climar R., 359
Coal, 45, 93, 145, 198, 236, 274, 325, 370, 431
Cobija, 210, 243
Coca, 188, 242
Coca R., 126
Coconuts, 42, 88
Cochabamba, 207, 209, 216, 218, 224, 225, 227, 230, 236, 242,
243
Coche, 69, 70, 94
Codera Cape, 60
Coffee, 7, 40, 86, 106, 110, 142, 143, 188, 241, 414
Cojedes, 55, 70, 77, 93;
R., 59
Cojoro, 78
Colastiné, 289, 301, 305
Colchagua, 247, 258, 276
Collins, P. T., 413
Colombia, 4, 7-52; 76, 78, 91, 114, 124, 133, 148, 149, 174, 373
Colón, 31, 83
Colonia, 356, 357, 361, 365
Colonia Las Heras, 313
Colonia Sarmiento, 313
Colonia Suiza, 361
Colonias del Gran Chaco, 207, 220
Colonias del Noroeste, 207, 219, 237, 243
Colorado R., 290, 299, 310
Colorados, 355
Colquechaca, 217
Colquipocro, 167, 200
Columbus, 8, 53, 69, 100
Collahuasi, 223, 268
Collay, 145
Commewynne R., 109
Comodoro Rivadavia, 275, 300, 304, 313, 325, 330
Concepción, (Chile), 246, 247, 259, 263, 264, 275, 276; (Par.),
335, 337, 342, 343, 344, 349
Concepción del Uruguay, 307, 365
Concordia, 285, 298, 307, 316, 362, 367
Conchi, 268
Condoto R., 49
Confuso R., 340
Constitución, 264
Copacabana, 215, 236
Copaiba, 107
Copiapó, 247, 248, 256, 264, 265, 407
Copper, 28, 45, 93, 144, 194, 195, 234, 271, 295, 300, 352, 370,
429
Copra, 106
Coquimbo, 247, 251, 256, 261, 263, 265, 270, 273, 274, 276
Córdoba, 281, 283, 285, 287, 288, 290, 296, 307, 308, 310, 316,
319, 325, 329
Corentyn R., 102, 104, 105, 109
Corn, 43, 88, 111, 142, 143, 190, 241, 319, 349
Coro, 55, 65, 79, 81, 84, 91, 94
Corocoro, 215, 222, 234, 235, 237
Coroico, 215, 224
Coronel, 236, 261, 263, 264, 274, 275
Coropuno Mt., 168
Corumbá, 218, 220, 231, 289, 343, 365, 404, 407, 409, 428
Corral, 273
Corriente R., 125
Corrientes, 283, 288, 297, 304, 305, 306, 311, 321, 322, 328, 331,
337, 339, 343, 348
Cotabambas, 199
Cotopaxi Mt., 123, 125, 126, 137
Cotton, 42, 88, 187, 321, 349, 418
Crato, 412
Cristóbal Colón, 69
Cruzeiro do Sul, 405, 406
Cruz Grande, 273
Cua, 83
Cuareim R., 367
Cuatro Ojos, 227
Cuba, 186
Cubagua, 69
Cúcuta, 10, 13, 28, 36, 37, 38, 84
Cuchivero R., 74
Cuenca, 117, 119, 122, 123, 124, 132, 138, 139, 184
Cumaná, 55, 65, 68, 79, 83, 84, 88, 90, 94, 98
Cundinamarca, 9, 10, 28, 40, 45
Cuñapiru, 370
Cupisnique, 198
Curaçao, 31, 57, 66, 78, 79, 83, 90, 96
Curaray R., 126, 137
Curicó, 247, 258
Curityba, 375, 393, 394, 410
Cuyabá, 289, 375, 403, 407;
R., 386, 407
Cuyuni R., 92, 108
Cuzco, 149, 152, 154, 159, 171, 172, 179, 180, 183, 184, 188,
190, 193, 197, 199, 201, 203, 211, 383
Chacarilla, 234, 235
Chaco, 223, 283, 293, 306, 312, 321, 322, 329, 331, 333, 336,
340, 342, 344, 345, 346, 349, 350, 351, 352
Chachani Mt., 180
Chachapoyas, 152
Chala, 154, 168, 171
Challapata, 216
Chama R., 82
Chanchamayo V., 188
Chañaral, 265
Chaparé R., 227
Charles Isl., 133
Charrúa Indians, 354
Chatham Isl., 133
Chaves, 406
Chaves Isl., 133
Chibchas, 27
Chicama V., 164, 185, 186
Chicla, 177
Chiclayo, 152, 164
Chicle, 43, 90
Chicha, 42, 241
Chile, 2, 4, 53, 114, 121, 122, 128, 151, 157, 158, 187, 191, 193,
205, 242, 245-279, 313, 323, 373
Chilete, 164
Chili R., 180
Chiloé, 122, 247, 260, 275, 276, 277
Chillán, 247
Chillo V., 137
Chimbo R., 127
Chimbote, 159, 165, 166, 167, 176, 183, 186, 198
Chimborazo, 116, 117, 133, 139
Chimborazo Mt., 122, 123, 133;
Pass, 137
Chimoré R., 227
Chinchillas, 193, 239
Chinchipe R., 124
Chiquinquirá, 37
Chiquitos, Sierra de, 211, 383
Chirimoias, 143, 190
Chita, 27, 139
Chlorolque Mt., 234
Chocó, 10, 23, 35, 43, 49, 50
Chone, 137
Chonos Isls., 260
Chorillos, 154, 183
Chubut, 283, 299, 304, 311, 323, 325
Chuño, 241
Chuquiaguillo, 230, 231
Chuquisaca, 206, 207, 217, 223, 237;
R., 167

Dairy Industry, 328, 361, 368


Daule R., 127, 142
Delegación Nac. en el Oriente, 207, 219
Delta Amacuro, 55, 76, 94
Delta Orinoco, 56, 95, 97
Demarara R., 103, 104
Desaguadero R., 212, 226, 234, 235
Deseado, 304, 313;
R., 313
Diamante, 313
Diamantina, 429
Diamantino, 383, 386
Diamonds, 94, 107, 108, 429
Dique, 30, 31
Dividivi, 89
Doce R., 430
Dolores, 355
D’Orbigny, 210
Dulce R., 295
Durán, 136
Durazno, 356, 364, 367

East Coast, 280-433


East Indians, 102, 105, 111
Ecuador, 4, 8, 14, 15, 24, 38, 114-147, 148, 157, 163, 188, 251,
373
El Beni, 207, 218, 243
El Callao, 73, 74, 84, 93
El Canto, 154
El Dorado, 92
El Misti, 158, 180
El Oriente, 116, 117, 123, 134, 219
El Oro, 116, 117, 132, 139, 144
El Valle, 9, 10, 24, 40, 50
El Vigia, 84
Embarcación, 223, 293, 306, 312
Emeralds, 7, 50
Encarnación, 311, 335, 342, 343, 352
Encontrados, 72, 81, 84, 85, 96
Ené R., 170, 183
Ensenada, 301, 302
Entre Rios, 283, 288, 298, 305, 311, 313, 319, 329, 331
Escalante R., 62, 64, 81, 85
Esmeralda, 75, 76
Esmeraldas, 116, 117, 118, 128, 130, 131, 136, 142, 145
Esmeraldas R., 127, 130
Espejos Spring, 238
Esperança, 409
Esperanza Rapids, 227
Espirito Santo, 375, 397, 411, 414, 432
Essequibo R., 101, 104, 105
Esteros, 210
Etén, 164, 175
Eucalyptus, 234;
timber, 145, 324, 364

Facatativá, 33, 34, 37


Falcón, 54, 55, 65, 79, 88, 93, 94, 97
Federal District, 54, 55, 56, 66, 67
Fernando de Noronha Isl., 406
Ferreñafe, 164
Ferrobamba, 197
Fibre Plants, 42, 91, 107, 146, 422
Fish, 108, 144, 194, 278, 398, 400
Flandes, 37
Florencia, 10
Flores, 356, 364;
General, 355
Florianopolis, 375, 393
Florida, 355, 356, 364
Flour, 329, 427
Forestry, 43, 89, 106, 113, 143, 191, 242, 277, 323, 345, 370, 419
Formosa, 283, 293, 304, 306, 313, 321
Fortaleza, 375, 401, 412
Fragosa, 407
Francia, Dr. J. G. R., 334
Fray Bentos, 307, 356, 362, 369
Friburgo, 411
Frigoríficos, 44, 92, 278, 318, 350, 352, 368, 369
Frio Cape, 380, 386
Fruit, 106, 143, 189, 241, 276, 323, 349, 370, 419
Fundición, 36
Furniture, 328

Galápagos Isls., 116, 117, 121, 133


Galera Tunnel, 177;
Volcano, 25
Garay, Juan de, 281, 305
Garcas R., 429
General Lopez, (town), 318
Georgetown, 101, 103, 104
Girardot, 33, 34, 35, 37
Goajira Penin., 15, 21, 36, 46, 56
Goats, 45, 65, 91, 143, 239, 278, 315, 368, 426
Gold, 7, 45, 49, 92, 107, 111, 112, 145, 199, 229, 274, 324, 370,
428
Gomez, General, 92
Gorgas, General, 128
Goya, 298, 313
Goyaz, 339, 375, 381, 385, 388, 404, 411, 428, 429
Goyaz Mts., 380, 381
Goyllarisquisga, 179, 182, 184, 196
Granadillas, 143
Gran Chaco, 220, 288, 338, 339
Grande R., 127, 407
Granja, 412
Greenheart, 106
Greenwich Park, 104
Grubb, W. Barbrooke, 337
Guacara, 67
Guadalupe, 164
Guaharibos Ind., 75
Guainía R., 17, 76
Guaira, 335
Guaire R., 63, 68;
V., 80
Guajará-Mirím, 225, 227, 243, 413
Gualaquiza, 139
Guanabara Bay, 395
Guanacos, 193, 329
Guanare, 55, 71, 84
Guanipa, 82
Guano, 194
Guanoco, 69, 82, 94, 97
Guanta, 68, 79, 82
Guapo, 68, 80
Guaporé R., 211, 212, 227, 383
Guaqui, 181, 221, 223, 236
Guaranda, 117
Guaranís, 335, 354
Guarapiche R., 70
Guarapuava, 425
Guárico, 55, 70, 80, 83
Guasipati, 73
Guatire, 83, 87
Guaviare R., 17, 27, 61, 75, 76
Guayana (Guiana), 56, 76, 86, 92
Guayana Highlands, 59, 60, 62, 72, 100, 379, 380, 384
Guayaquil, 117, 119, 120, 127, 128, 132, 135, 136
Guayaquil Gulf, 122, 124, 127, 128, 131, 163
Guayas, 116, 117, 131, 139;
R., 122, 127, 138, 156;
V., 142
Guiana, 100-113
Guiana Brazilian, 100, 380, 388
Guiana British, 73, 100, 101, 102-108
Guiana Dutch, 100, 101, 109-112
Guiana French, 100, 101, 112-113
Guiana Highlands, 379, 380
Guigüe, 80, 83
Guindí, 335

Hauri Hauri R., 199


Herva Matte, 393, 422
Higuerote, 80
Hogs, 92, 193, 240, 317, 368
Honda, 33, 47;
Rapids, 33
Horses, 45, 92, 194, 278, 317, 368, 426
Huacho, 167, 187, 201
Huailas V., 158, 159, 166, 167, 176, 179, 183, 198, 457
Huaina Potosí Mt., 222, 233
Huallaga R., 159, 160, 169, 170, 173
Huamachuco, 165, 188
Huancabamba, 184
Huancavelica, 152, 171, 197, 200
Huancayo, 171, 179, 183
Huanchaca, 224, 232
Huanchaco, 165, 186
Huanday, 164
Huanta, 188
Huánuco, 152, 161, 169, 170, 188
Huarás, 152, 165, 166
Huarmey, 167, 199
Huasaga R., 125
Huascarán Mt., 166
Huasco, 256, 265
Huatanay R., 172
Huayday, 198
Huaytiquina, 269, 313
Huigra, 136
Huila, 9, 10, 28, 51
Humboldt, Alexander von, 148
Hydroplane, 33

Iabaro R., 227


Ibagué, 10, 29, 35, 39
Ibarra, 38, 117, 133, 137
Ibicuy, (Arg.), 289, 301, 311;
(Par.), 352
Ica, 152, 167, 168, 187, 197, 198
Iguapé, Ribeira de, 386, 406
Iguassú Falls, 289, 306, 331, 342

You might also like