| Lesson 2 | What is a behavioral pattern? |
| Objective | Define Behavioral Patterns |
A behavioral design pattern explains how objects interact: how they send messages to each other, how the steps of a task are divided among different objects, and how responsibilities are allocated across a system. Behavioral patterns identify common communication patterns between objects and provide proven solutions for implementing those patterns. By applying a behavioral pattern, you increase the flexibility of the communication between objects without having to redesign the objects themselves.
Understanding where behavioral patterns fit within the GoF taxonomy requires comparing them to the other two categories. The three GoF pattern categories each address a different concern:
Behavioral patterns make up the largest GoF category. Eleven of the twenty-three GoF patterns are behavioral, reflecting how much of software design concerns itself with interaction and communication rather than structure or creation.
A behavioral pattern describes how different objects and classes send messages to each other to accomplish a task and how the responsibilities for the steps of that task are divided. Five patterns illustrate the range of interaction problems behavioral patterns solve:
java.util.concurrent.Flow (Java 9+)
provides the standard reactive Observer implementation, replacing the deprecated
java.util.Observable.Comparator passed as a lambda expression to
Collections.sort() is a Strategy.java.lang.Iterable and java.util.Iterator are the
standard library implementations, and the enhanced for-each loop is built on
this pattern.GoF behavioral patterns divide into two subcategories based on whether they use inheritance or object composition as the primary mechanism:
The Gang of Four defined eleven behavioral patterns. These form the core behavioral pattern catalog:
java.lang.Iterable and java.util.Iterator are the
standard implementations.java.util.Observable
is java.util.concurrent.Flow (Java 9+).Beyond the original GoF catalog, the software engineering community has identified additional behavioral patterns that address common problems not covered by the eleven GoF patterns. These community-recognized patterns follow the same pattern template (intent, applicability, structure, and consequences) but are not attributed to the Gang of Four: