Behavioral Patterns
Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects.
Patterns in this Category
Defines a subscription mechanism to notify multiple objects about events.
Interview Examples:
Stock market ticker where multiple dashboards update when prices change.
Event-driven systems (Pub/Sub like Kafka/RabbitMQ concepts).
Model-View-Controller (MVC) where View observes Model changes.
Java Implementation:
interfacefor the Observer (with anupdate()method).interfaceorclassfor the Subject maintaining a List of Observers.
Defines a family of algorithms and makes them interchangeable.
Interview Examples:
Payment Gateway (Credit Card, PayPal, UPI strategies calculating different fees/processes).
Navigation map routing (Driving, Walking, Transit strategies).
Sorting algorithms (QuickSort, MergeSort based on data size or type).
Java Implementation:
interfacefor the Strategy algorithm.classfor the Context holding a reference to a Strategy and delegating work to it.
Encapsulates a request as an object, allowing parameterization and queuing of requests.
Interview Examples:
Implementing Undo/Redo functionality in text editors.
GUI buttons where each button triggers a specific command object.
Job Queue processing where tasks are executed asynchronously.
Java Implementation:
interfacefor the Command (with anexecute()method).classfor Concrete Commands storing a reference to the Receiver.
Allows an object to alter its behavior when its internal state changes.
Interview Examples:
Vending Machine state transitions (HasCoin, NoCoin, Dispensing).
Media Player (Play, Pause, Stop states).
Document workflows (Draft, In-Review, Published).
Java Implementation:
interfacefor the State defining behaviors.classfor the Context holding a reference to the current State.
Separates algorithms from the objects they operate on.
Interview Examples:
Abstract Syntax Tree (AST) parsing in compilers.
Exporting data from a complex object hierarchy (e.g., returning JSON/XML from a document structure).
Processing tax calculations on different types of grocery items.
Java Implementation:
interfacefor the Visitor (with overloadedvisit()methods for each element type).interfacefor the Element (with anaccept(Visitor)method).
Provides a way to access elements of a collection sequentially without exposing underlying representation.
Interview Examples:
Custom collection classes in Java/C++ (like Trees, Graphs) providing a unified way to iterate over nodes.
Paginating results from a database.
Java Implementation:
interfacefor the Iterator (withhasNext()andnext()).interfacefor the Iterable Collection (with a method returning the Iterator).
Reduces coupling by making objects communicate through a mediator rather than directly.
Interview Examples:
Chat Room application where users don't talk directly but through a central server.
Air Traffic Control system coordinating planes.
UI Dialog boxes coordinating complex interactions between various UI components.
Java Implementation:
interfacefor the Mediator.abstract classorclassfor Colleagues holding a reference to the Mediator.
Chain of Responsibility Passes requests along a chain of handlers, where each handler decides to process or pass it.
Interview Examples:
Middleware in web frameworks (e.g., Express.js, Spring Security filters).
Logging frameworks where a log goes through multiple filters before being written.
Request approval workflows (Manager -> Director -> VP).
Java Implementation:
abstract classfor the Handler (holds a reference to thenextHandler).classfor each Concrete Handler implementing the processing logic.
Defines the skeleton of an algorithm, letting subclasses override specific steps.
Interview Examples:
Data parsing framework where steps are (Open, Parse, Close) and subclasses implement
Parse(CSV, XML, JSON).Game AI behavior skeletons.
UI lifecycle methods (React/Android component lifecycles).
Java Implementation:
abstract classwith afinaltemplate method andabstractprimitive operation steps.
When to Use Behavioral Patterns
When you need flexible communication between objects
When you want to define algorithms independently
When object behavior depends on state
When you need to decouple sender and receiver
Last updated