实例介绍
【实例简介】
【实例截图】
【核心代码】
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 www.allitebooks.com ■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 Inhe A Singleton As a Wrapper for Page-Specific Code. . . . . . . . . . . . . . . . . . . . 68 A Singleton with Private Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Using the Underscore Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Using Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Comparing the Two Techniques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Lazy Instantiation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Example: Creating XHR Objects with Branching . . . . . . . . . . . . . . . . . . . . . 79 When Should the Singleton Pattern Be Used? . . . . . . . . . . . . . . . . . . . . . . . 81 Benefits of the Singleton Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Drawbacks of the Singleton Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 ■CHAPTER 6 Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 The Structure of a Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Building a Chainable JavaScript Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Using Callbacks to Retrieve Data from Chained Methods . . . . . . . . . . . . . 89 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 PART 2 ■ ■ ■ Design Patterns ■CHAPTER 7 The Factory Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 The Simple Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 The Factory Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 When Should the Factory Pattern Be Used? . . . . . . . . . . . . . . . . . . . . . . . . . 99 Dynamic Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Combining Setup Costs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Abstracting Many Small Objects into One Large Object . . . . . . . . . . 99 Example: XHR Factory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Specialized Connection Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Choosing Connection Objects at Run-Time. . . . . . . . . . . . . . . . . . . . 103 Example: RSS Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Benefits of the Factory Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Drawbacks of the Factory Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 ■CONTENTS ix ■CHAPTER 8 The Bridge Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Example: Event Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Other Examples of Bridges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Bridging Multiple Classes Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Example: Building an XHR Connection Queue . . . . . . . . . . . . . . . . . . . . . . 111 Including the Core Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Including an Observer System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Developing the Queue Skeleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Implementing the Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Where Have Bridges Been Used? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 When Should the Bridge Pattern Be Used?. . . . . . . . . . . . . . . . . . . . . . . . . 122 Benefits of the Bridge Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Drawbacks of the Bridge Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 ■CHAPTER 9 The Composite Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 The Structure of the Composite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Using the Composite Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Example: Form Validation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Adding Operations to FormItem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Adding Classes to the Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Adding More Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Example: Image Gallery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Benefits of the Composite Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Drawbacks of the Composite Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 ■CHAPTER 10 The Facade Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Some Facade Functions You Probably Already Know About. . . . . . . . . . . 141 JavaScript Libraries As Facades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Facades As Convenient Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Example: Setting Styles on HTML Elements . . . . . . . . . . . . . . . . . . . . . . . . 144 Example: Creating an Event Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 General Steps for Implementing the Facade Pattern. . . . . . . . . . . . . . . . . 147 When Should the Facade Pattern Be Used? . . . . . . . . . . . . . . . . . . . . . . . . 148 Benefits of the Facade Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Drawbacks of the Facade Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 x ■CONTENTS ■CHAPTER 11 The Adapter Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Characteristics of an Adapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Adapting Existing Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Example: Adapting One Library to Another . . . . . . . . . . . . . . . . . . . . . . . . . 150 Example: Adapting an Email API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Wrapping the Webmail API in an Adapter . . . . . . . . . . . . . . . . . . . . . 157 Migrating from fooMail to dedMail . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 When Should the Adapter Pattern Be Used?. . . . . . . . . . . . . . . . . . . . . . . . 158 Benefits of the Adapter Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Drawbacks of the Adapter Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 ■CHAPTER 12 The Decorator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 The Structure of the Decorator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 The Role of the Interface in the Decorator Pattern. . . . . . . . . . . . . . 163 The Decorator Pattern vs. the Composite Pattern . . . . . . . . . . . . . . 163 In What Ways Can a Decorator Modify Its Component? . . . . . . . . . . . . . . 164 Adding Behavior After a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Adding Behavior Before a Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Replacing a Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Adding New Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 The Role of the Factory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Function Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 When Should the Decorator Pattern Be Used?. . . . . . . . . . . . . . . . . . . . . . 173 Example: Method Profiler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Benefits of the Decorator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Drawbacks of the Decorator Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 ■CHAPTER 13 The Flyweight Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 The Structure of the Flyweight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Example: Car Registrations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Intrinsic and Extrinsic State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Instantiation Using a Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Extrinsic State Encapsulated in a Manager. . . . . . . . . . . . . . . . . . . . 182 Managing Extrinsic State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Example: Web Calendar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Converting the Day Objects to Flyweights. . . . . . . . . . . . . . . . . . . . . 185 Where Do You Store the Extrinsic Data? . . . . . . . . . . . . . . . . . . . . . . 186 ■CONTENTS xi Example: Tooltip Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 The Unoptimized Tooltip Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Tooltip As a Flyweight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Storing Instances for Later Reuse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 When Should the Flyweight Pattern Be Used? . . . . . . . . . . . . . . . . . . . . . . 192 General Steps for Implementing the Flyweight Pattern . . . . . . . . . . . . . . 193 Benefits of the Flyweight Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Drawbacks of the Flyweight Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Example: Page Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 General Pattern for Wrapping a Web Service . . . . . . . . . . . . . . . . . . . . . . . 205 Example: Directory Lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 General Pattern for Creating a Virtual Proxy . . . . . . . . . . . . . . . . . . . . . . . . 210 Benefits of the Proxy Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Drawbacks of the Proxy Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 ■CHAPTER 15 The Observer Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Example: Newspaper Delivery. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Push vs. Pull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Pattern in Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Building an Observer API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Delivery Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Subscribe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Unsubscribe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Observers in Real Life. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Example: Animation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Event Listeners Are Also Observers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 When Should the Observer Pattern Be Used? . . . . . . . . . . . . . . . . . . . . . . 223 Benefits of the Observer Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Drawbacks of the Observer Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 xii ■CONTENTS ■CHAPTER 16 The Command Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 The Structure of the Command. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Creating Commands with Closures . . . . . . . . . . . . . . . . . . . . . . . . . . 227 The Client, the Invoker, and the Receiver. . . . . . . . . . . . . . . . . . . . . . 227 Using Interfaces with the Command Pattern. . . . . . . . . . . . . . . . . . . 228 Types of Command Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Example: Menu Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 The Menu Composites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 The Command Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Adding More Menu Items Later On. . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Example: Undo and Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Implementing Undo with Nonreversible Actions By Logging Commands. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Logging Commands for Crash Recovery . . . . . . . . . . . . . . . . . . . . . . 242 When to Use the Command Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 Benefits of the Command Pattern. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Drawbacks of the Command Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 ■CHAPTER 17 The Chain of Responsibility Pattern . . . . . . . . . . . . . . . . . . . . . . 245 The Structure of the Chain of Responsibility. . . . . . . . . . . . . . . . . . . . . . . . 245 Passing on Requests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Implementing a Chain of Responsibility in an Existing Hierarchy . . . . . . 254 Event Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 When Should the Chain of Responsibility Pattern Be Used? . . . . . . . . . . 255 Example: Image Gallery Revisited. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 Using the Chain of Responsibility to Make Composites More Efficient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Adding Tags to Photos. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Benefits of the Chain of Responsibility Pattern . . . . . . . . . . . . . . . . . . . . . 261 Drawbacks of the Chain of Responsibility Pattern. . . . . . . . . . . . . . . . . . . 262 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论