Behavioral design patterns describe how objects and classes interact and divide responsibilities among themselves. A behavioral pattern abstracts an action from the object or class that performs it. By changing the object or class, you change the algorithm used, the objects affected, or the behavior, while preserving the same basic interface for client classes.
Behavioral patterns form the largest category in the GoF catalog: eleven of the twenty-three GoF patterns are behavioral. The key to understanding behavioral patterns is communication. The focus shifts from the objects and classes themselves to how those objects and classes communicate with one another. In the truest form of composition, behavioral patterns are best understood in terms of how objects work together to perform tasks that no single object can carry out alone.
This module explores behavioral design patterns: patterns that describe the way objects and classes interact and divide responsibilities among themselves. A good toolbox of behavioral patterns allows you to solve many challenging problems you are likely to encounter when designing object-oriented systems, including enumerating lists, responding to changes of state in an object, and serializing or deserializing objects without penetrating data encapsulation.
In this module, you will learn:
java.util.concurrent.Flow,
functional interfaces, and lambda expressions relate to the GoF behavioral
patternsBehavioral object patterns use object composition rather than inheritance. Some patterns describe how a group of peer objects cooperate to perform a task that no single object can carry out by itself. An important design question is how peer objects know about each other. Peers could maintain explicit references to each other, but that increases coupling. In the extreme, every object would know about every other.
The Mediator pattern avoids this by introducing a mediator object between peers. The mediator provides the indirection needed for loose coupling.
The Chain of Responsibility provides even looser coupling. It lets you send requests to an object implicitly through a chain of candidate objects. Any candidate may fulfill the request depending on runtime conditions. The number of candidates is open-ended, and you can select which candidates participate in the chain at runtime.
Behavioral patterns are concerned with algorithms and the communication between objects that carry them out. They identify common communication patterns between objects and increase flexibility in how that communication is carried out. Behavioral patterns are typically used to:
Several concrete examples show how behavioral patterns improve communication design in real systems:
Behavioral design patterns make up the largest section of the GoF catalog. The Gang of Four defined eleven behavioral patterns:
java.lang.Runnable
and java.util.concurrent.Callable are standard Command
implementations.java.lang.Iterable and java.util.Iterator
are the standard library implementations, and the enhanced for-each loop is
built on this pattern.java.util.concurrent.Flow (Java 9+) is the standard
reactive Observer implementation, replacing the deprecated
java.util.Observable.Comparator lambda
passed to Collections.sort() is a Strategy.The key to understanding behavioral design patterns is communication. The focus shifts from the individual objects and classes that make up a pattern to the communication between them. Behavioral patterns are best understood in terms of how objects work together to perform tasks.
The emphasis on interaction is so significant that some class diagrams look identical. The State and Strategy patterns, for example, share a nearly identical class diagram structure. However, because of the way their participants communicate and handle responsibilities, the two patterns are very different in intent and application.