Mastering JavaScript Design Patterns
Table of Contents
Mastering JavaScript Design Patterns
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Designing for Fun and Profit
The road to JavaScript
The early days
A pause
The way of Gmail
JavaScript everywhere
What is a design pattern?
Antipatterns
Summary
I. Classical Design Patterns
2. Organizing Code
Chunks of code
What's the matter with global scope anyway?
Objects in JavaScript
Build me a prototype
Inheritance
Modules
ECMAScript 6 classes and modules
Best practices and troubleshooting
Summary
3. Creational Patterns
Abstract Factory
Implementation
Builder
Implementation
Factory Method
Implementation
Singleton
Implementation
Disadvantages
Prototype
Implementation
Hints and tips
Summary
4. Structural Patterns
Adapter
Implementation
Bridge
Implementation
Composite
An example
Implementation
Decorator
Implementation
Façade
Implementation
Flyweight
Implementation
Proxy
Implementation
Hints and tips
Summary
5. Behavioral Patterns
Chain of responsibility
Implementation
Command
The command message
The invoker
The receiver
Interpreter
An example
Implementation
Iterator
Implementation
ECMAScript 6 iterators
Mediator
Implementation
Memento
Implementation
Observer
Implementation
State
Implementation
Strategy
Implementation
Template method
Implementation
Visitor
Hints and tips
Summary
II. Other Patterns
6. Functional Programming
Functional functions are side-effect free
Function passing
Implementation
Filters and pipes
Implementation
Accumulators
Implementation
Memoization
Implementation
Immutability
Lazy instantiation
Implementation
Hints and tips
Summary
7. Model View Patterns
First, some history
Model View Controller
The MVC code
Model View Presenter
The MVP code
Model View ViewModel
The MVVM code
A better way to transfer changes between the model and the view
Observing view changes
Hints and tips
Summary
8. Web Patterns
Sending JavaScript
Combining files
Minification
Content delivery networks
Plugins
jQuery
d3
Doing two things at once – multithreading
The circuit breaker pattern
Back-off
Degraded application behavior
The promise pattern
Hints and tips
Summary
9. Messaging Patterns
What's a message anyway?
Commands
Events
Request-reply
Publish-subscribe
Fan out and fan in
Dead-letter queues
Message replay
Pipes and filters
Versioning messages
Hints and tips
Summary
10. Patterns for Testing
The testing pyramid
Test in the small with unit tests
Arrange-Act-Assert
Asserts
Fake objects
Test spies
Stub
Mock
Monkey patching
Interacting with the user interface
Browser testing
Faking the DOM
Wrapping the manipulation
Build and test tools
Hints and tips
Summary
11. Advanced Patterns
Dependency injection
Live postprocessing
Aspect-oriented programming
Macros
Hints and tips
Summary
12. ES6 Solutions Today
TypeScript
The class syntax
The module syntax
Arrow functions
Typing
Traceur
Classes
Default parameters
Template literals
Block bindings with let
Async
Conclusion
Hints and tips
Summary
A. Conclusion
Index
Mastering JavaScript Design Patterns
Copyright © 2014 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: November 2014
Production reference: 1151114
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-798-6
www.packtpub.com
Author
Simon Timms
Reviewers
Amrita Chaturvedi
Philippe Renevier Gonin
Pedro Miguel Barros Morgado
Mani Nilchiani
Commissioning Editor
Kunal Parikh
Acquisition Editor
Meeta Rajani
Content Development Editor
Sweny M. Sukumaran
Technical Editor
Siddhi Rane
Copy Editor
Laxmi Subramanian
Project Coordinator
Rashi Khivansara
Proofreaders
Simran Bhogal
Lawrence A. Herman
Elinor Perry-Smith
Indexer
Hemangini Bari
Graphics
Sheetal Aute
Ronak Dhruv
Valentina D'silva
Disha Haria
Abhinash Sahu
Production Coordinator
Nitesh Thakur
Cover Work
Nitesh Thakur
Simon Timms is a developer who loves to write code. He writes in a variety of languages using a number of tools. For the most part, he develops web applications with .NET backends. He is very interested in visualizations and cloud computing. A background in build and system administration keeps him on the straight and narrow when it comes to DevOps.
He is the author of Social Data Visualization with HTML5 and JavaScript, Packt Publishing. He blogs at http://blog.simontimms.com/ and is also a frequent contributor to the Canadian Developer Connection, where his latest series explores evolving ASP.NET applications.
He is the President of the Calgary .NET User Group and a member of half a dozen others. He speaks on a variety of topics from DevOps to how the telephone system works.
He works as a web developer for Pacesetter Directional Drilling, the friendliest performance drilling company around.
Amrita Chaturvedi is a PhD researcher in the Department of Computer Science and Engineering at Indian Institute of Technology, Kanpur, Uttar Pradesh, India (http://www.cse.iitk.ac.in/users/amrita/index.htm). She was schooled (kindergarten to intermediate) at City Montessori School, Aliganj, Lucknow, Uttar Pradesh, India. She received a Bachelor of Technology degree in Information Technology from Institute of Engineering and Technology, Lucknow, Uttar Pradesh, India and a Master of Technology degree in Information Technology (with a specialization in Software Engineering) from Indian Institute of Information Technology, Allahabad (Deemed University), Uttar Pradesh, India. She has worked in Nucleus Software Exports Ltd., Noida, Uttar Pradesh, India as a software engineer. She was also employed as a faculty in Institute of Engineering and Technology, Lucknow, Uttar Pradesh, India. She has worked in user interface design as a senior project associate at Indian Institute of Technology, Kanpur, Uttar Pradesh, India. She was selected as the first female PhD student from Asia under EURECA (European Research and Educational Collaboration with Asia) project 2009 to conduct research at VU University Amsterdam, the Netherlands. Her areas of specialization are software engineering, software architecture, software design patterns, and ontologies. Her research interests include software architecture and design, ontologies-based software engineering, service-oriented and model-driven architecture, semantic web, Internet technologies, and mobile agents.
She has given several talks and seminars as well as conference welcome/key notes at international conferences. She has also earned various awards such as best paper award for her research paper in an international conference, ACM SIGAPP award, and has also been a Physics Olympiad topper. She has traveled several European, North American, African, and Asian countries for educational/conference/research purposes. She has teaching as well as research exper...