Enhance your JavaScript code with this essential collection of design patterns. Discover an extensive range of techniques and strategies to successfully tackle complex JavaScript development problems and put them into practice by following detailed examples that demonstrate each design pattern at its most effective. Dive deeper into JavaScript and master these powerful design patterns for an innovative and cutting-edge approach to JavaScript that meets the demands of modern web development. Explore how design patterns can help you improve your JavaScript code and get to grips with creational, structural behavioural patterns, as you discover how to put them to work in different scenarios. Then look deeper at patterns used in functional programming, as well as model view patterns, and patterns to build web applications. You will also find patterns to improve the testability of your code using mock objects, mocking frameworks and monkey patching along with some advanced patterns including dependency injection and live postprocessing. Taking a complete and journey through the most valuable design patterns, and providing clear guidance on how to effectively use them in JavaScript, this JavaScript book gives you access to one of the best ways to learn JavaScript and use it to meet the demands of web development today. Discover and use tried and tested design patterns for a more effective approach to JavaScript Learn simple strategies for improving your code organization Get to grips with creational, behavioural and structural design patterns Rethink and revitalize your code with the use of functional patterns Harness the power of design patterns for a range of development tasks from building an application to code testing Explore advanced JavaScript design patterns including dependency injection Cover......Page 1 Copyright......Page 3 Credits......Page 4 About the Author......Page 5 About the Reviewers......Page 6 www.PacktPub.com......Page 9 Table of Contents......Page 14 Preface......Page 20 The road to JavaScript......Page 26 The early days......Page 27 The way of Gmail......Page 29 JavaScript everywhere......Page 32 What is a design pattern?......Page 35 Antipatterns......Page 37 Summary......Page 39 Chunks of code......Page 42 What's the matter with global scope anyway?......Page 44 Objects in JavaScript......Page 46 Build me a prototype......Page 50 Inheritance......Page 53 Modules......Page 55 ECMAScript 6 classes and modules......Page 59 Summary......Page 60 Chapter 3: Creational Patterns......Page 62 Abstract Factory......Page 63 Implementation......Page 68 Builder......Page 70 Implementation......Page 71 Implementation......Page 74 Singleton......Page 77 Implementation......Page 78 Prototype......Page 79 Implementation......Page 80 Hints and tips......Page 81 Summary......Page 82 Adapter......Page 84 Implementation......Page 86 Bridge......Page 88 Implementation......Page 90 Composite......Page 93 An example......Page 94 Implementation......Page 95 Decorator......Page 97 Implementation......Page 98 Façade......Page 99 Implementation......Page 100 Implementation......Page 102 Proxy......Page 104 Implementation......Page 105 Summary......Page 106 Chapter 5: Behavioral Patterns......Page 108 Chain of responsibility......Page 109 Implementation......Page 110 Command......Page 113 The command message......Page 114 The invoker......Page 116 The receiver......Page 117 An example......Page 118 Implementation......Page 119 Implementation......Page 120 Mediator......Page 122 Implementation......Page 123 Memento......Page 124 Implementation......Page 125 Observer......Page 128 Implementation......Page 129 State......Page 131 Implementation......Page 132 Strategy......Page 135 Implementation......Page 136 Template method......Page 138 Implementation......Page 140 Visitor......Page 142 Summary......Page 147 Chapter 6: Functional Programming......Page 150 Function passing......Page 151 Implementation......Page 153 Filters and pipes......Page 155 Implementation......Page 156 Accumulators......Page 158 Implementation......Page 159 Memoization......Page 160 Implementation......Page 161 Immutability......Page 163 Implementation......Page 164 Hints and tips......Page 166 Summary......Page 167 Chapter 7: Model View Patterns......Page 168 Model View Controller......Page 169 The MVC code......Page 174 Model View Presenter......Page 179 The MVP code......Page 180 Model View ViewModel......Page 183 The MVVM code......Page 184 A better way to transfer changes between the model and the view......Page 186 Observing view changes......Page 188 Summary......Page 189 Sending JavaScript......Page 190 Combining files......Page 191 Minification......Page 194 Content delivery networks......Page 195 jQuery......Page 196 d3......Page 198 Doing two things at once – multithreading......Page 201 The circuit breaker pattern......Page 204 Back-off......Page 205 Degraded application behavior......Page 206 The promise pattern......Page 207 Summary......Page 209 Chapter 9: Messaging Patterns......Page 210 What's a message anyway?......Page 211 Commands......Page 212 Events......Page 213 Request-reply......Page 215 Publish-subscribe......Page 218 Fan out and fan in......Page 221 Dead-letter queues......Page 224 Message replay......Page 226 Pipes and filters......Page 227 Versioning messages......Page 228 Hints and tips......Page 229 Summary......Page 230 Chapter 10: Patterns for Testing......Page 232 Test in the small with unit tests......Page 233 Arrange-Act-Assert......Page 235 Asserts......Page 236 Fake objects......Page 237 Test spies......Page 238 Stub......Page 239 Mock......Page 241 Monkey patching......Page 242 Browser testing......Page 243 Faking the DOM......Page 244 Wrapping the manipulation......Page 245 Hints and tips......Page 246 Summary......Page 247 Dependency injection......Page 248 Live postprocessing......Page 252 Aspect-oriented programming......Page 253 Macros......Page 257 Hints and tips......Page 258 Summary......Page 259 TypeScript......Page 260 The class syntax......Page 261 The module syntax......Page 262 Arrow functions......Page 263 Typing......Page 265 Traceur......Page 267 Classes......Page 268 Default parameters......Page 269 Template literals......Page 270 Block bindings with let......Page 271 Async......Page 273 Hints and tips......Page 274 Summary......Page 275 Appendix: Conclusion......Page 276 Index......Page 280 Cover 1 Copyright 3 Credits 4 About the Author 5 About the Reviewers 6 www.PacktPub.com 9 Table of Contents 14 Preface 20 Chapter 1: Designing for Fun and Profit 26 The road to JavaScript 26 The early days 27 A pause 29 The way of Gmail 29 JavaScript everywhere 32 What is a design pattern? 35 Antipatterns 37 Summary 39 Chapter 2: Organizing Code 42 Chunks of code 42 What's the matter with global scope anyway? 44 Objects in JavaScript 46 Build me a prototype 50 Inheritance 53 Modules 55 ECMAScript 6 classes and modules 59 Best practices and troubleshooting 60 Summary 60 Chapter 3: Creational Patterns 62 Abstract Factory 63 Implementation 68 Builder 70 Implementation 71 Factory Method 74 Implementation 74 Singleton 77 Implementation 78 Disadvantages 79 Prototype 79 Implementation 80 Hints and tips 81 Summary 82 Chapter 4: Structural Patterns 84 Adapter 84 Implementation 86 Bridge 88 Implementation 90 Composite 93 An example 94 Implementation 95 Decorator 97 Implementation 98 Façade 99 Implementation 100 Flyweight 102 Implementation 102 Proxy 104 Implementation 105 Hints and tips 106 Summary 106 Chapter 5: Behavioral Patterns 108 Chain of responsibility 109 Implementation 110 Command 113 The command message 114 The invoker 116 The receiver 117 Interpreter 118 An example 118 Implementation 119 Iterator 120 Implementation 120 ECMAScript 6 iterators 122 Mediator 122 Implementation 123 Memento 124 Implementation 125 Observer 128 Implementation 129 State 131 Implementation 132 Strategy 135 Implementation 136 Template method 138 Implementation 140 Visitor 142 Hints and tips 147 Summary 147 Chapter 6: Functional Programming 150 Functional functions are side-effect free 151 Function passing 151 Implementation 153 Filters and pipes 155 Implementation 156 Accumulators 158 Implementation 159 Memoization 160 Implementation 161 Immutability 163 Lazy instantiation 164 Implementation 164 Hints and tips 166 Summary 167 Chapter 7: Model View Patterns 168 First, some history 169 Model View Controller 169 The MVC code 174 Model View Presenter 179 The MVP code 180 Model View ViewModel 183 The MVVM code 184 A better way to transfer changes between the model and the view 186 Observing view changes 188 Hints and tips 189 Summary 189 Chapter 8: Web Patterns 190 Sending JavaScript 190 Combining files 191 Minification 194 Content delivery networks 195 Plugins 196 jQuery 196 d3 198 Doing two things at once – multithreading 201 The circuit breaker pattern 204 Back-off 205 Degraded application behavior 206 The promise pattern 207 Hints and tips 209 Summary 209 Chapter 9: Messaging Patterns 210 What's a message anyway? 211 Commands 212 Events 213 Request-reply 215 Publish-subscribe 218 Fan out and fan in 221 Dead-letter queues 224 Message replay 226 Pipes and filters 227 Versioning messages 228 Hints and tips 229 Summary 230 Chapter 10: Patterns for Testing 232 The testing pyramid 233 Test in the small with unit tests 233 Arrange-Act-Assert 235 Asserts 236 Fake objects 237 Test spies 238 Stub 239 Mock 241 Monkey patching 242 Interacting with the user interface 243 Browser testing 243 Faking the DOM 244 Wrapping the manipulation 245 Build and test tools 246 Hints and tips 246 Summary 247 Chapter 11: Advanced Patterns 248 Dependency injection 248 Live postprocessing 252 Aspect-oriented programming 253 Macros 257 Hints and tips 258 Summary 259 Chapter 12: ES6 Solutions Today 260 TypeScript 260 The class syntax 261 The module syntax 262 Arrow functions 263 Typing 265 Traceur 267 Classes 268 Default parameters 269 Template literals 270 Block bindings with let 271 Async 273 Conclusion 274 Hints and tips 274 Summary 275 Appendix: Conclusion 276 Index 280
Applying design patterns to JavaScript code creates more reliable and maintainable code. In this book, you will explore different design patterns and learn where and how to implement them with the help of detailed examples.
In the first part of the book, we start off with an introduction to design patterns, and then move on to creating classical structures that are used to organize code. Next, we look at the creational, structural, and behavioral patterns. The second part of the book dives into patterns used for functional programming, model view patterns, patterns to build web applications, and messaging patterns. A number of very interesting advanced JavaScript patterns such as dependency injection and live postprocessing are also covered.
By the end of this book, you will learn to identify places where a pattern would improve code readability and maintainability.
Applying design patterns to JavaScript code creates more reliable and maintainable code. In this book, you will explore different design patterns and learn where and how to implement them with the help of detailed examples. In the first part of the book, we start off with an introduction to design patterns, and then move on to creating classical structures that are used to organize code. Next, we look at the creational, structural, and behavioral patterns. The second part of the book dives into patterns used for functional programming, model view patterns, patterns to build web applications, and messaging patterns. A number of very interesting advanced JavaScript patterns such as dependency injection and live postprocessing are also covered. By the end of this book, you will learn to identify places where a pattern would improve code readability and maintainability If you are a developer interested in creating easily maintainable applications that can grow and change with your needs, then this book is for you. Some experience with JavaScript (not necessarily with entire applications written in JavaScript) is required to follow the examples written in the book.