You are previewing Mastering JavaScript Design Patterns.
O'Reilly logo
Mastering JavaScript Design Patterns

Book Description

Discover how to use JavaScript design patterns to create powerful applications with reliable and maintainable code

In Detail

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.

What You Will Learn

  • Organize your code with classes and modules
  • Explore the original Gang of Four patterns and learn how to apply them to JavaScript
  • Decrease the degree of coupling in applications and increase code reuse through creational patterns
  • Rethink your coding through the use of functional patterns
  • Master advanced JavaScript patterns such as dependency injection and live postprocessing
  • Improve the testability of your code with mock objects, mocking frameworks, and monkey patching
  • Discover the patterns that are the basis of modern JavaScript libraries such as Knockout.js, Angular.js, and Backbone.js
  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. Mastering JavaScript Design Patterns
      1. Table of Contents
      2. Mastering JavaScript Design Patterns
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Designing for Fun and Profit
        1. The road to JavaScript
          1. The early days
          2. A pause
          3. The way of Gmail
          4. JavaScript everywhere
        2. What is a design pattern?
        3. Antipatterns
        4. Summary
      9. I. Classical Design Patterns
        1. 2. Organizing Code
          1. Chunks of code
          2. What's the matter with global scope anyway?
          3. Objects in JavaScript
          4. Build me a prototype
          5. Inheritance
          6. Modules
          7. ECMAScript 6 classes and modules
          8. Best practices and troubleshooting
          9. Summary
        2. 3. Creational Patterns
          1. Abstract Factory
          2. Implementation
          3. Builder
            1. Implementation
          4. Factory Method
            1. Implementation
          5. Singleton
            1. Implementation
            2. Disadvantages
          6. Prototype
            1. Implementation
          7. Hints and tips
          8. Summary
        3. 4. Structural Patterns
          1. Adapter
            1. Implementation
          2. Bridge
            1. Implementation
          3. Composite
            1. An example
            2. Implementation
          4. Decorator
            1. Implementation
          5. Façade
            1. Implementation
          6. Flyweight
            1. Implementation
          7. Proxy
            1. Implementation
          8. Hints and tips
          9. Summary
        4. 5. Behavioral Patterns
          1. Chain of responsibility
            1. Implementation
          2. Command
            1. The command message
            2. The invoker
            3. The receiver
          3. Interpreter
            1. An example
            2. Implementation
          4. Iterator
            1. Implementation
            2. ECMAScript 6 iterators
          5. Mediator
            1. Implementation
          6. Memento
            1. Implementation
          7. Observer
            1. Implementation
          8. State
            1. Implementation
          9. Strategy
            1. Implementation
          10. Template method
            1. Implementation
          11. Visitor
          12. Hints and tips
          13. Summary
      10. II. Other Patterns
        1. 6. Functional Programming
          1. Functional functions are side-effect free
          2. Function passing
            1. Implementation
          3. Filters and pipes
            1. Implementation
          4. Accumulators
            1. Implementation
          5. Memoization
            1. Implementation
          6. Immutability
          7. Lazy instantiation
            1. Implementation
          8. Hints and tips
          9. Summary
        2. 7. Model View Patterns
          1. First, some history
          2. Model View Controller
            1. The MVC code
          3. Model View Presenter
            1. The MVP code
          4. Model View ViewModel
            1. The MVVM code
            2. A better way to transfer changes between the model and the view
            3. Observing view changes
          5. Hints and tips
          6. Summary
        3. 8. Web Patterns
          1. Sending JavaScript
            1. Combining files
            2. Minification
            3. Content delivery networks
          2. Plugins
            1. jQuery
            2. d3
          3. Doing two things at once – multithreading
          4. The circuit breaker pattern
            1. Back-off
            2. Degraded application behavior
          5. The promise pattern
          6. Hints and tips
          7. Summary
        4. 9. Messaging Patterns
          1. What's a message anyway?
            1. Commands
            2. Events
          2. Request-reply
          3. Publish-subscribe
            1. Fan out and fan in
          4. Dead-letter queues
            1. Message replay
            2. Pipes and filters
            3. Versioning messages
          5. Hints and tips
          6. Summary
        5. 10. Patterns for Testing
          1. The testing pyramid
          2. Test in the small with unit tests
          3. Arrange-Act-Assert
            1. Asserts
          4. Fake objects
            1. Test spies
            2. Stub
            3. Mock
          5. Monkey patching
          6. Interacting with the user interface
            1. Browser testing
            2. Faking the DOM
            3. Wrapping the manipulation
          7. Build and test tools
          8. Hints and tips
          9. Summary
        6. 11. Advanced Patterns
          1. Dependency injection
          2. Live postprocessing
          3. Aspect-oriented programming
          4. Macros
          5. Hints and tips
          6. Summary
        7. 12. ES6 Solutions Today
          1. TypeScript
            1. The class syntax
            2. The module syntax
            3. Arrow functions
            4. Typing
          2. Traceur
            1. Classes
            2. Default parameters
            3. Template literals
            4. Block bindings with let
            5. Async
            6. Conclusion
          3. Hints and tips
          4. Summary
        8. A. Conclusion
      11. Index