Thông tin tài liệu
this print for content only—size & color not accurate spine = 0.693" 296 page count
Books for professionals By professionals
®
Pro JavaScript
™
Design Patterns
Dear Reader,
Web programming is becoming more complex and collaborative each day. A
new JavaScript
™
library is born each week, and we are getting closer to the time
when web applications can seamlessly replace those found on our desktops. It
is no longer possible to design the behavior of your sites haphazardly without
thinking about long-term software maintainability.
The JavaScript language has matured. We have reached a point where soft-
ware development techniques once considered useful only in languages such
as Java and C++ are being applied to web programming. Therefore, we felt the
time has come for a book that explores object-oriented design principles and
applies them to the JavaScript language. The techniques needed to implement
patterns like factory, singleton, observer, composite, and facade in JavaScript
are easily understood, but they have not previously been discussed in depth in
a single book. We wanted to show programmers that JavaScript contains features
on par with other high-level languages and is an object-oriented programming
language in its own right. In fact, we wrote the book that we ourselves have always
wanted to read.
In this book, we will teach you about commonly used software patterns for
designing the code that drives your websites and applications. You will learn
object-oriented JavaScript programming, starting with routine tasks and pro-
gressing to advanced techniques and patterns. We will help you create libraries
and APIs that can be used by others, as well as show you techniques that will
help you interact with other JavaScript programmers and work effectively in
large teams. Most of all, we will show you how powerful, expressive, and flexible
the JavaScript language can be.
Ross Harmes and Dustin Diaz
US $44.99
Shelve in
Web development
User level:
Intermediate–Advanced
Harmes,
Diaz
Pro JavaScript
™
Design Patterns
The eXperT’s Voice
®
in WeB DeVelopmenT
Pro
JavaScript
™
Design Patterns
cyan
maGenTa
yelloW
Black
panTone 123 c
Ross Harmes and Dustin Diaz
Companion
eBook
Available
www.apress.com
SOURCE CODE ONLINE
Companion eBook
See last page for details
on $10 eBook version
ISBN-13: 978-1-59059-908-2
ISBN-10: 1-59059-908-X
9 781590 599082
5 4 4 9 9
The essentials of object-oriented
JavaScript
™
programming
RELATED TITLES
www.it-ebooks.info
www.it-ebooks.info
Pro JavaScript
™
Design Patterns
Ross Harmes and Dustin Diaz
908Xch00FM.qxd 11/16/07 1:05 PM Page i
www.it-ebooks.info
Pro JavaScript
™
Design Patterns
Copyright © 2008 by Ross Harmes and Dustin Diaz
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-59059-908-2
ISBN-10 (pbk): 1-59059-908-X
ISBN-13 (electronic): 978-1-4302-0495-4
ISBN-10 (electronic): 1-4302-0495-8
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems Inc. in the
United States and other countries. Apress Inc. is not affiliated with Sun Microsystems Inc., and this book
was written without endorsement from Sun Microsystems Inc.
Lead Editors: Chris Mills, Tom Welsh
Technical Reviewer: Simon Willison
Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick,
Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper,
Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Richard Dal Porto
Copy Editor: Jennifer Whipple
Associate Production Director: Kari Brooks-Copony
Production Editor: Kelly Winquist
Compositor and Artist: Kinetic Publishing Services, LLC
Proofreader: Dan Shaw
Indexer: Julie Grady
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 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 http://www.springeronline.com.
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley,
CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com.
908Xch00FM.qxd 11/16/07 1:05 PM Page ii
www.it-ebooks.info
To Mom, and those who have listened, thanks
—Dustin Diaz
To Alec, Dymphi, and Terry
—Ross Harmes
908Xch00FM.qxd 11/16/07 1:05 PM Page iii
www.it-ebooks.info
908Xch00FM.qxd 11/16/07 1:05 PM Page iv
www.it-ebooks.info
Contents at a Glance
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
PART 1
■ ■ ■
Object-Oriented JavaScript
■CHAPTER 1 Expressive JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
■CHAPTER 2 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
■CHAPTER 3 Encapsulation and Information Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . 25
■CHAPTER 4 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
■CHAPTER 5 The Singleton Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
■CHAPTER 6 Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
PART 2
■ ■ ■
Design Patterns
■CHAPTER 7 The Factory Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
■CHAPTER 8 The Bridge Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
■CHAPTER 9 The Composite Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
■CHAPTER 10 The Facade Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
■CHAPTER 11 The Adapter Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
■CHAPTER 12 The Decorator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
■CHAPTER 13 The Flyweight Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
■CHAPTER 14 The Proxy Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
■CHAPTER 15 The Observer Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
■CHAPTER 16 The Command Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
■CHAPTER 17 The Chain of Responsibility Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
v
908Xch00FM.qxd 11/16/07 1:05 PM Page v
www.it-ebooks.info
908Xch00FM.qxd 11/16/07 1:05 PM Page vi
www.it-ebooks.info
Contents
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
PART 1
■ ■ ■
Object-Oriented JavaScript
■CHAPTER 1 Expressive JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The Flexibility of JavaScript. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
A Loosely Typed Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Functions As First-Class Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The Mutability of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Design Patterns in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
■CHAPTER 2 Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
What Is an Interface? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Benefits of Using Interfaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Drawbacks of Using Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
How Other Object-Oriented Languages Handle Interfaces. . . . . . . . . . . . . 12
Emulating an Interface in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Describing Interfaces with Comments . . . . . . . . . . . . . . . . . . . . . . . . . 14
Emulating Interfaces with Attribute Checking. . . . . . . . . . . . . . . . . . . 16
Emulating Interfaces with Duck Typing . . . . . . . . . . . . . . . . . . . . . . . . 17
The Interface Implementation for This Book. . . . . . . . . . . . . . . . . . . . . . . . . 18
The Interface Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
When to Use the Interface Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
How to Use the Interface Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Example: Using the Interface Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Patterns That Rely on the Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
vii
908Xch00FM.qxd 11/16/07 1:05 PM Page vii
www.it-ebooks.info
■CHAPTER 3 Encapsulation and Information Hiding . . . . . . . . . . . . . . . . . . . . 25
The Information Hiding Principle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Encapsulation vs. Information Hiding. . . . . . . . . . . . . . . . . . . . . . . . . . 26
The Role of the Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Basic Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Fully Exposed Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Private Methods Using a Naming Convention. . . . . . . . . . . . . . . . . . . 30
Scope, Nested Functions, and Closures. . . . . . . . . . . . . . . . . . . . . . . . 32
Private Members Through Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
More Advanced Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Static Methods and Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Singletons and Object Factories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Benefits of Using Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Drawbacks to Using Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
■CHAPTER 4 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Why Do You Need Inheritance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Classical Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
The Prototype Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
The extend Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Prototypal Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Asymmetrical Reading and Writing of Inherited Members . . . . . . . . 46
The clone Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Comparing Classical and Prototypal Inheritance . . . . . . . . . . . . . . . . . . . . . 49
Inheritance and Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Mixin Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Example: Edit-in-Place . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Using Classical Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Using Prototypal Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Using Mixin Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
When Should Inheritance Be Used? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
■CHAPTER 5 The Singleton Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
The Basic Structure of the Singleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Namespacing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
■CONTENTSviii
908Xch00FM.qxd 11/16/07 1:05 PM Page viii
www.it-ebooks.info
[...]... alternative approaches to object-oriented programming by using concepts from functional programming We discuss why you should use design patterns in the first place and how adapting them to JavaScript will make your code more efficient and easier to work with The Flexibility of JavaScript One of the most powerful features of the language is its flexibility As a JavaScript programmer, you can make your programs... features that we can build upon to implement specific design patterns The second part deals with specific design patterns and how they can be used in the JavaScript language We took great pains to make the examples in each chapter as practical as possible We tried to list some of the most common tasks performed by JavaScript programmers, and then used design patterns to make them more modular, efficient,... 194 ■ CHAPTER 14 The Proxy Pattern 197 The Structure of the Proxy 197 How Does the Proxy Control Access to Its Real Subject? 197 Virtual Proxy, Remote Proxy, and Protection Proxy 200 The Proxy Pattern vs the Decorator Pattern 201 When Should the Proxy Be Used? ... approaches to object-oriented programming by using concepts from functional programming We discuss why you should use design patterns in the first place, and how adapting them to the JavaScript language will make your code more efficient and easier to work with Chapter 2: Interfaces We look at how other object-oriented languages implement interfaces and try to emulate the best features of each in JavaScript. .. each chapter is available as a zip file at the book’s website, http://jsdesignpatterns.com, and at the Apress website, http://www.apress.com Contacting the Authors You can contact the authors at dustin@jsdesignpatterns.com and ross@jsdesignpatterns.com www.it-ebooks.info 908Xch01.qxd 11/15/07 10:31 AM PART Page 1 1 ■■■ Object-Oriented JavaScript www.it-ebooks.info 908Xch01.qxd 11/15/07 10:31 AM Page 2... engineers who know some JavaScript and wish to learn more Specifically, those who want to improve their understanding of the object-oriented capabilities of JavaScript and learn how they can make their code more modular, maintainable, and efficient This book will teach these readers about the basics of object-oriented programming in JavaScript It will also teach them about specific design patterns, showing... 11/16/07 1:05 PM Page xxi Introduction J avaScript is at a turning point The language and those who program with it have matured People are starting to realize that it is a complex subject, worthy of further study Design patterns have been used in programming for years They were first formally documented in Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (affectionately known... objects can be created in JavaScript, and the techniques available within each to create public, private, and protected methods We also take a look at the situations where using complex encapsulated objects can benefit the JavaScript programmer Chapter 4: Inheritance We look at the techniques that can be used to create subclasses in JavaScript We cover both classical and prototypal inheritance, and... basic JavaScript syntax and will focus more on the sections that deal with converting existing code to implement specific patterns, and explanations of when each pattern should or shouldn’t be used The second type of readers are programmers who are more involved with server-side languages such as Java and C++ and are relative beginners in JavaScript They wish to use their knowledge of design patterns. .. reader may already be familiar with specific design patterns and so may get more out of the JavaScript- specific, object-oriented techniques covered in Part 1 Readers who are unfamiliar with the basics of JavaScript and object-oriented programming may have a tough time following some of the examples This is not an entry-level text, and it assumes a certain level of programming knowledge That being said, we .
Intermediate–Advanced
Harmes,
Diaz
Pro JavaScript
™
Design Patterns
The eXperT’s Voice
®
in WeB DeVelopmenT
Pro
JavaScript
™
Design Patterns
cyan
maGenTa
yelloW
. 0.693" 296 page count
Books for professionals By professionals
®
Pro JavaScript
™
Design Patterns
Dear Reader,
Web programming is becoming more complex
Ngày đăng: 15/03/2014, 21:20
Xem thêm: Pro JavaScript Design Patterns ppt, Pro JavaScript Design Patterns ppt