Get an overview of what we'll learn in this chapter.

In the last two chapters, we’ve learned patterns that aid us in the creation of objects and in building complex object structures. Now it’s time to move on to another aspect of software design, which concerns the behavior of components. In this chapter, we’ll learn how to combine objects and how to define the way they communicate so that the behavior of the resulting structure becomes extensible, modular, reusable, and adaptable. Problems such as “How do I change parts of an algorithm at runtime?”, “How can I change the behavior of an object based on its state?”, and “How can I iterate over a collection without knowing its implementation?” are the typical kinds of problems solved by the patterns presented in this chapter. 

The Observer pattern is one of the foundational patterns of the Node.js platform as it provides us with a simple interface for dealing with events and subscriptions, which are the life force of Node.js’s event-driven architecture. 

We’re already familiar with the Gang of Four (GoF) design patterns, in this chapter, we’ll witness once again how the implementation of some of those patterns can be radically different in JavaScript compared to a purer object-oriented approach. A great example of this thesis can be found in the Iterator pattern, which we’ll explore later in the chapter. To implement the Iterator pattern, in fact, we won’t need to extend any class or build any complex hierarchy. Instead, we’ll just need to add a special method to a class. Moreover, one particular pattern in this chapter, Middleware, closely resembles another popular GoF pattern, which is the Chain of Responsibility pattern, but its implementation in Node.js has become such a standard that it can be considered a pattern of its own. 

In this chapter, we’ll learn about the following: 

Get hands-on with 1200+ tech skills courses.