Index

Numbers and symbols drawbacks of, 123 $ function, 151–152 event listener callbacks with, 109–110 introduction to, 109 A uses of, 110, 122 accessor methods, 29, 38, 89–90 XHR connection queue (example), action method, 233 111–122 ActiveXObject, 100 browsers, encapsulation of differences, 78–81 adapters/, 111 buttons adapting libraries (example), 150–152 command, 237–239 benefits of, 149, 158 undo, 237 characteristics of, 149–150 creating, 150 C drawbacks of, 158 C#, interfaces in, 14 email API (example), 152–158 callbacks, 213 for existing implementations, 150 event listener, 109–110, 142, 222 vs. , 144, 149 using to retrieve data from chained when to use, 158 methods, 89–90 addEvent function, 142, 146–147 catch-all receivers, 262 addEventListener function, 142 chain of responsibility pattern addForm function, 18 benefits of, 261 addRequest function, 122 and, 254–262 Ajax, 99 drawbacks of, 262 Ajax request queue, 111–122 event delegation, 255 AjaxHandler interface, 103 image gallery (example), 256–261 aliases, 78 implementing, in existing hierarchy, animation, using , 221 254–255 anonymous functions, 6–8 introduction to, 245 API class, 89 objects in, 249–251 API2 class, 89 passing requests in, 251–254 APIs structure of, 245–251 development, using bridges, 109–110 using with composite pattern, 257–262 using adapter pattern with, 152–158 when to use, 255–256 array methods, 113–114 chains/chaining, 89 asymmetry, in prototypal inheritance, 46–48 building chainable JavaScript library, 86, 88 asyncRequest function, 156 callbacks and, 89–90 attribute checking, emulating interfaces introduction to, 83 with, 16–17 structure of, 84–86 attributes, static, 35–37 _checkInitialization method, 211 augment method, 51, 59–63 checkIsbn method, 29, 37 child objects, 47–48, 57 B class declarations, 42 behaviors classes adding after method, 164 composite, 231–232 adding before method, 165 interfaces for, 26 branching, 65, 78–82 mixin, 50–51, 59–62 bridges/ using bridges to connect multiple, benefits of, 123 110–111 connecting multiple classes with, 111 See also specific classes 263 264 ■INDEX

classical inheritance, 42–45 form validation (example), 127–136 edit-in-place field using, 52–55 image gallery (example), 136–139 use of, 62 for managing extrinsic state, 183–186 vs. prototypal, 49 structure of, 126 click handlers, 241 using, 126 click listeners, 238 CompositeFieldset class, 134–136 clients, in , 227–228 configure method, 57 clone function, 46–49, 57–58, 62 connection objects closures, 7, 33–36 choosing, at run-time, 103–104 concept of, 26 specialized, 101–102 creating commands with, 227 constants, 37–38 to hide information, 33–38 constructor attribute, 45 using for private members in singleton constructors, 42 objects, 71–73 convenience functions, 143–144, 147 code cookies, 131 bridge between public and private, 110 core utilities, for request queue, 112–114 loosely coupled, 234 crash recovery, 242 organization of, 65, 81 createXhrHandler method, 104 page-specific, 68–70 createXhrObject method, 101 reusability of, 11, 39, 41, 50–51 Cursor class, 237, 240–241 code duplication, prevention of, 107 command buttons, 237–239 D command class, 233 data command objects/pattern, 23 extrinsic, 179, 182–183, 186–189, 193 benefits of, 243 intrinsic, 179 building user interfaces with, 230–235 retrieval, from changed methods, 89–90 clients in, 227–228 data encapsulation with , 237–239 data integrity and, 39 drawbacks of, 243 in manager, 182–183 execute method, 235–239 debugging introduction to, 225 command pattern and, 243 invoking object in, 227–228 encapsulation and, 39 logging and, 235, 237–242 flyweight pattern and, 194 menu items (example), 230–235 interfaces and, 12 receiving object in, 227–228 decorator objects, 201 structure of, 225–228 order of, 168–169 undo method, 235–242 using factory objects to create, 169–172 using interfaces with, 228, 230 decorator pattern, 23 when to use, 242–243 adding behavior after method, 164 commands adding behavior before method, 165 creating with closures, 227 adding new methods, 167–169 reusing, 235 benefits of, 176 See also specific commands with command pattern, 237–239 comments, describing interfaces with, 14–15 vs. composite pattern, 163 complements keyword, 14 drawbacks of, 176 components for functions, 172–173 adding new methods, 167–169 method profiler (example), 173–176 replacing methods of, 166–167 modifying behavior of components with, composite classes, 231–232 164–169 composite elements, 127–131 vs. , 201 composite pattern, 23 replacing methods, 166–167 adding more classes to hierarchy, 133–136 role of interface in, 163 benefits of, 125, 139 structure of, 159–163 chain of responsibility pattern and, use of, 159, 173 254–262 decoupling, 262 vs. decorator pattern, 163 dedMail, migrating from fooMail to, 157–158 drawbacks of, 139–140 delete keyword, 66 ■INDEX 265 deliver method, 219 common examples of, 141–142 dependencies, hard-coded, 249 as convenient methods, 143–144 dequeue, 114 creating event utility (example), 146–147 . See patterns drawbacks of, 148 DHTML, 144–145 introduction to, 141 dialog boxes, flyweight pattern for, 190–192 JavaScript libraries as, 142 directory lookup (example), 206–210 setting styles on HTML elements display method, 27–29 (example), 144–145 DOM elements, reducing needed, 186–190 steps for implementing, 147 duck typing, emulating interfaces with, uses of, 141, 148 17–18 factory method, changing function into, 84 dynamic implementations, 99 factory pattern, 23, 38, 83 dynamic user interface, 225–226 benefits of, 107 dynamic virtual proxy, creating, 210–213 creating decorator objects with, 169–172 creating, for flyweight pattern, 193 E drawbacks of, 108 edit-in-place field (example), 52 example, 99–104 using classical inheritance, 52–55 instantiation using, 181 using mixin classes, 59–62 RSS reader (example), 104–107 using prototypal inheritance, 55–58 simple, 93–96 efficiency, of virtual proxies, 213 true, 96–98 email API, adapter pattern with, 152–158 uses of, 99 encapsulation fieldsets, 128 benefits of, 39 findProduct function, 67 defined, 26 flyweight manager, 193 drawbacks of, 39–40 flyweight pattern vs. information hiding, 26 calendar (example), 185–186 inheritance and, 49–50 car registrations (example), 179–183 encapsulation functions, 226–227, 243 composite pattern with, 183–186 ensureImplements function, 18 converting objects to, 185 event bubbling, 255 drawbacks of, 194 event capturing, 255 encapsulation of extrinsic data, 182–183 event delegation, 122, 255 extrinsic state and, 180–186 event listeners for dialog boxes, 190–192 with bridge pattern, 109–110 instantiation using a factory, 181 as observers, 222 intrinsic state and, 180–181 use of, in JavaScript, 142 introduction to, 179 event objects, 255 steps for implementing, 193 event utility, creating with facade pattern, storing instances for reuse, 190–192 146–147 structure of, 179 event-driven environments, 215 tooltip objects (example), 186–190 execute method, 226, 229, 235–239 web calendar (example), 183–185 extend function, 43–45, 55, 62 when to use, 192 extend keyword, 42 fooMail, migrating to dedMail from, 157–158 externally hosted libraries, 20 form elements, 127–128 extrinsic data, 179, 192 form validation, using composite pattern, encapsulation of, 182–183 127–136 removing from class, 188–189 FormItem interface, 128, 133 storage of, 186, 193 fully exposed objects, 27–30 extrinsic state, 180–186 functions combining, with facades, 143–144 F convenience, 143–144 facades/facade pattern, 201 decorator pattern for, 172–173 vs. adapter pattern, 144, 149 as first-class objects, 6–8 addEvent function, 142, 146–147 in JavaScript, 6–8 benefits of, 148 modifying into factory method, 84 vs. bridges, 111 nested, 32–33, 36 266 ■INDEX

as objects, 6–8, 37 for command pattern, 228–230 privileged, 110 for composite objects, 140 scope of, 33 for decorator pattern, 163 singletons as, 72–73 defined, 11 See also specific functions describing with comments, 14–15 drawbacks of using, 12 G emulating, in JavaScript, 14–18 getValue method, 131–132 implementation for book, 18 getXHR function, 156 importance of, 11 GUI-based operating systems, 141 in Java, 13 patterns relying on, 23 H in PHP, 13–14 handleFilingRequest method, 249 role of, in information hiding, 26 hard-coded dependencies, 249 intrinsic data, 179 HAS-A relationships, 128 intrinsic state, 180–181 hasOwnProperty method, 47 introspection, 9 hierarchical structures, composite objects invoker class, 232 and, 139 invoking objects, in command pattern, 227–230 HTML elements IS-A relationships, 128 JavaScript objects as wrappers for, 136–139 isHighLatency method, 104 setting styles on, with facade pattern, 144–145 _isInitialized method, 211–212 isOffline method, 104 I image gallery J adding tags to photos in, 258–261 JavaScript using chain of responsibility pattern, design patterns, 9–10 256–261 emulating interfaces in, 14–18 using composite pattern, 136–139 encapsulation in, 40 implements keyword, 13–14 event listeners and, 142 information hiding features of, 3–9 encapsulation and, 26, 39–40 flexibility of, 3–6, 210 principle of, 25–26 functions in, 6–8 role of interface in, 26 inheritance in, 9 using closures, 33–38 lack of interface support in, 12 using naming conventions, 30–32 as loosely typed language, 6 inheritance, 9, 35 objects in, 8–9 classical, 42–45, 49, 52–55, 62 JavaScript libraries classical vs. prototypal, 49 chainable, 86–88 encapsulation and, 49–50 common features of, 86 introduction to, 41 facades and, 141–142 prototypal, 41, 45–49, 55–58 using adapter pattern with, 150–152 when to use, 41, 62 JavaScript objects, as wrappers for HTML init method, 68 elements, 136–139 _initialize method, 211 instanceOf check, 22 L instances, storing for reuse, 190–192 large projects, use of interfaces in, 20 instantiation , 75–78, 82, 223 lazy, 75–78 leaf elements, 127–128, 131 using a factory, 181 libraries. See JavaScript libraries Interface class, 18–22 ListBuilder class, 174–176 Interface helper class, 18 logging commands, 239–241 interface keyword, 14 command pattern and, 235–242 Interface objects, 20 for crash recovery, 242 Interface.ensureImplements class method, 18 for implementing undo on nonreversible interfaces actions, 239–242 benefits of using, 11 loosely coupled code, 234 in C#, 14 loosely coupled modules, 39 ■INDEX 267 loosely coupled objects, 41, 107, 139 reducing required number of, 179 loosely typed variables, 6 request, 251–254 sender, 245 M static members, 35–37 maintenance, flyweight pattern and, 194 using naming conventions, 30–32 manager, creating to store extrinsic data, 193 wrapper, 149 memoizing, 101 See also specific types method profiling, using decorator pattern, observable objects, 217, 223 173–176 observed role, 215–218 methods observer pattern adding behavior after, 164 animation (example), 221 adding behavior before, 165 benefits of, 223 adding new, 167–169 building observer API, 218–220 chaining, 83–90 delivery methods in, 216, 219 mutator, 29–30 drawbacks of, 223 private, 25, 37, 39, 204 event listeners, 222 privileged, 34, 110 implementation of, 216–218 public, 34 introduction to, 215 replacing, 166–167 publishers in, 215–218 static, 35–37, 75 for request queue, 114 See also specific methods subscribe method, 219–220 mixin classes, 50–51, 59–62 subscribers in, 215–218 modular user interfaces, using command unsubscribe method, 220 pattern, 230–235 uses of, 220, 223 mutable objects, 8–9 observer role, 215–218 mutator methods, 29–30 optimization with chain of responsibility pattern, N 258–261 namespaces, singletons and, 65–68, 81 with flyweight pattern, 179–192 naming conventions, emulating private with proxy pattern, 213 members using, 30–32 nested functions, 32–33, 36 P new keyword, 42, 99 page-specific code, singleton as wrapper for, new operator, 42, 57 68–70 nonreversible actions, implementing undo parentheses, empty, 36 with, 239–242 patterns, 9–10 adapter, 149–158 O bridge, 109–123 object factories, 38 chain of responsibility, 245–262 object literals, singletons and, 66, 72 closure, 33–35 objects command, 23, 225–244 adding functionality to, with decorator composite, 23, 125–140 pattern, 159–163 constants, 37–38 in chain of responsibility pattern, 249–251 for creating objects, 26–39 cloned, 48–49 decorator, 23, 159–177 converting to flyweights, 185 facade, 141–148 encapsulation of multiple into one large, factory, 23, 38, 93–108 99 flyweight, 179–195 fully exposed, 27–30 fully exposed object, 27, 29–30 intrinsic vs. extrinsic state, 180–181 observer, 215–223 invoking in command patter, 227–230 private members using naming loosely coupled, 41, 107, 139 convention, 30–32 mutability of, 8–9 proxy, 197–214 observable, 217, 223 relying on interfaces, 23 patterns for, 26–39 singleton, 38, 65–82 prototype, 45–49, 57–58 static methods and attributes, receiver, 227–230, 245, 262 35–37 268 ■INDEX

performance issues, 12, 204 Q PHP, interfaces in, 13–14 queuing system, 111–122 pooling technique, 193 primitive types, 37 R private attributes, 25 real subjects, 197–200, 212–214 scope and, 32–33 receiver classes, 230 through closures, 33–35 receiving objects, 227–230, 245, 262 private code, bridge between public code refactoring, 262 and, 110 reflection, 9 private keyword, 26 reliability issues, with remote proxy, 213 private members remote proxies, 200 emulating, using naming conventions, benefits of, 213 30–32 drawbacks of, 213 singleton with, 70–75 performance issues with, 204 syntax for, 75 use of, 201 through closures, 33–35 for wrapping web service, 201–204 private methods, 25, 37, 39, 204 renderResults method, 22 privileged functions, 110 request handling, chain of responsibility privileged methods, 34, 110 model and, 261 programming styles, 3 request method, 102–103 protection proxies, 200 request objects, 251–254 prototypal inheritance, 41, 45–49 request queue, 111–122 vs. classical, 49 ResultFormatter class, 21–22 edit-in-place field using, 55–58 reusability, 11, 39, 50–51 use of, 62 RSS reader, using factory pattern, 104–107 prototype attribute, 46–48 prototype chaining, 42–43, 46–48 S prototype method, 137 save function, 128, 130–131 prototype objects, 45–49, 57–58 scope, 32–33, 39 proxy objects/pattern Sellsian approach, 216–217 access control to real subject by, 197–200 sender objects, 245 benefits of, 213 serialize method, 50 vs. decorator pattern, 201 setup costs, combining using factory pattern, directory lookup (example), 206–210 99 drawbacks of, 213–214 shortcut icons, 141 function of, 197 SimpleHandler class, 101 general, for virtual proxy, 210–213 , 38, 66 introduction to, 197 basic structure of, 65–66 page statistics (example), 201–204 benefits of, 81 protection, 200 branching, 78–81 remote, 200–204, 213 drawbacks of, 82 structure of, 197–201 introduction to, 65 use of, 201 lazy loading, 75–78, 82 virtual, 199–201, 206–213 namespacing, 66–68 for wrapping web services, 201–206 with private members, 70–75 PS2 slot, 150 uses of, 65, 81 PS2-to-USB adapter, 150 as wrapper for page-specific code, 68–70 public code, bridge between private code static attributes, 35–37 and, 110 static methods, 35–37, 75 public members, 37, 75 strict type checking, 12, 18 public methods, 34, 204 subclasses, 35, 212–213 publisher-subscriber pattern. See observer subscribe method, 219–220 pattern subscribers, 215–218 publishers, 215–218 successors, 249 pull delivery environment, 216 superclasses, 35, 44–45 push delivery environment, 216 ■INDEX 269

T variables test-driven development (TDD), 216 closures for, 33–35 testing, interfaces and, 12 constants, 37–38 this keyword, 27, 34, 36–37, 42, 72, 233 singleton, 66 tooltip objects, with flyweight pattern, virtual proxies, 199–200 186–190 benefits of, 213 type checking, 12, 18, 20 directory lookup (example), 206–210 drawbacks of, 213 U general pattern for creating, 210–213 underscore notation, 32, 70–71, 74 use of, 201 undo buttons, 237–239 undo method, 235, 237–242 W UndoButton class, 239 web services UndoDecorator class, 239 defining interface for, 203–204 unsubscribe method, 220 proxy pattern for, 201–206 USB system, 150 webmail API, using adapter pattern with, user interfaces, with command pattern, 152–158 225–226, 230–235 X V XHR objects, 99–101 validate function, 127–128 XHR requests, 208, 213 var keyword, 36, 67, 75 XMLHttpRequest object, 100, 156