Behavioral patterns are about how objects talk to each other โ who sends messages, who receives them, and how they cooperate to get work done.
Instead of hardโwiring classes together, these patterns give them flexible ways to communicate. That means you can swap, extend, and test each piece in isolation.
The Strategy pattern lets you swap algorithms at runtime. Instead of a giant if/else, you give the object a strategy object that knows how to do the work.
Example: a NavigationApp can be given a CarRoute, BikeRoute, or WalkRoute strategy. The app doesn't know or care which โ it just asks the strategy to compute a route.
The Observer pattern lets objects subscribe to events. When something interesting happens in the subject, it notifies all observers automatically.
Example: a WeatherStation broadcasts temperature updates. A PhoneApp, a Website, and a SmartDisplay all subscribe and update their displays instantly.
The Command pattern turns an action into an object. That means you can store actions, queue them, log them, and even undo them.
Example: every keystroke in a text editor is a Command object. Undo works by keeping a history of commands and reversing them one by one.
The State pattern lets an object change its behaviour when its internal state changes. Instead of a giant switch statement, each state becomes its own class.
Example: a traffic light has RedState, YellowState, and GreenState. Each knows what to display and which state comes next. The traffic light just delegates.
The Template Method pattern defines the skeleton of an algorithm in a parent class, but lets subclasses fill in certain steps.
Example: a DataImporter parent class handles "open file โ parse โ validate โ save." Subclasses override only "parse" โ for CSV, JSON, or XML โ without rewriting the whole flow.
Iterator gives you a clean way to step through a collection without knowing whether it's a list, tree, or graph. That's how for x in items works under the hood.
Mediator puts a middleman between objects so they don't all talk directly to each other. A chat room is a mediator: you send your message to the room, and it forwards it to everyone.
The Chain of Responsibility pattern passes a request along a chain of handlers until one of them handles it. Each handler decides: "Is this for me? If yes, handle it. If no, pass it on."
Example: a support system where Bot handles simple questions, Agent handles medium ones, and Manager handles complex ones. Each forwards to the next if it can't help.
MVC (Model-View-Controller) splits an app into three roles: the Model holds data, the View shows it, and the Controller handles input. MVVM (Model-View-ViewModel) swaps the controller for a ViewModel that binds data to the view automatically.
Both help keep your UI separate from your logic โ so you can redesign the screen without rewriting your data code.
Layered architecture stacks your app into layers: UI on top, business logic in the middle, data storage at the bottom. Each layer only talks to the one below it.
Hexagonal architecture (also called "ports and adapters") puts your business logic in the centre, and lets the outside world โ databases, APIs, UIs โ plug in through "ports." This means you can replace the database or UI without touching your core logic.
Dependency Injection (DI) means a class receives its dependencies from outside, instead of creating them itself. This makes testing easier and swapping implementations trivial.
Eventโdriven design flips the flow: instead of one thing calling another, events are published and any interested listener reacts. Systems become loosely coupled and easy to extend.
There is no "best" architecture โ only the right fit for the project. Small projects may only need MVC. Big projects may need layered or hexagonal architecture.
Ask yourself: How big will this grow? How many developers? How often will it change? How much testing is needed? The answers guide your choice. Start simple; grow the architecture only when needed.
Strategy swap: Create a TravelPlanner that accepts a strategy object (CarRoute, BikeRoute, WalkRoute). Swap strategies at runtime and print the route description.
Weather station: Build a WeatherStation (subject) with subscribe() and notify(). Add three observers that each print a different format of the temperature.
Undo button: Create a simple text editor with AddTextCommand and DeleteTextCommand. Record commands in a list and implement undo by reversing them one at a time.
Traffic light: Implement a traffic light with three state classes (Red, Yellow, Green). Each state decides what to show and which state comes next. Call next() three times and watch the cycle.
Template importer: Write a DataImporter base class with a run() method that calls parse(). Make CsvImporter and JsonImporter subclasses that override only parse(). Show the shared flow.
Support chain: Build a support system with three handlers (Bot, Agent, Manager). Each handles a category of request or passes it on. Send different requests and see who handles them.
Mini MVC: Build a tiny "counter" app with a Model (count value), a View (prints the count), and a Controller (handles "increment" and "reset"). Demonstrate that the View never touches the Model directly.
By the end of this module, you'll be able to: use Strategy, Observer, Command, State, and Template Method patterns, explain MVC/MVVM/layered/hexagonal architectures, apply dependency injection, and choose the right style for your project.