Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Fundamentals of Object Oriented Programming Level Three ยท Lesson 5 of 9

Module Four

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
5
Module 4: Design Patterns โ€“ Behavioral & Architectural for Young Coders
โญ Module 4 ยท Level Three

Design Patterns:
Behavioral & Architectural

๐ŸŽญ How objects talk, cooperate, and build whole systems

๐ŸŽฏ What you'll learn in this module

  • โœ” How objects communicate using behavioral patterns (Strategy, Observer, Command, State)
  • โœ” How to loop through collections neatly with Iterator and coordinate with Mediator
  • โœ” How to chain handlers with Chain of Responsibility
  • โœ” The big architectural styles: MVC, MVVM, layered, and hexagonal
  • โœ” How dependency injection and eventโ€‘driven design glue systems together
  • โœ” How to choose the right style for the right project
๐Ÿ’ฌ
What Are Behavioral Patterns?
The Big Idea

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.

๐Ÿ“ฃ Creational patterns are about building people. Structural patterns are about arranging them. Behavioral patterns are about conversations between them.
๐Ÿงญ
Strategy: Swap the Algorithm
Behavioral

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.

๐Ÿ—บ๏ธ Like choosing "avoid highways" on a maps app: the destination is the same, but the route strategy changes.
๐Ÿ“ก
Observer: Broadcast the News
Behavioral

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.

๐Ÿ“บ Like subscribing to a YouTube channel: you don't check for new videos every hour; you get notified the moment one appears.
๐ŸŽฎ
Command: Wrap Actions in Objects
Behavioral

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.

๐Ÿ“ผ A remote control: each button press is a stored command โ€” press undo and the last command reverses.
๐Ÿšฆ
State: Behave Differently Based on Mode
Behavioral

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.

๐ŸŽง Like a music player: paused, playing, and stopped are three states โ€” each button press does something different depending on the state.
๐Ÿ“
Template Method: Fill in the Blanks
Behavioral

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.

๐Ÿฐ A recipe that says "add filling of your choice" โ€” the steps stay the same, but each baker fills in their own flavour.
๐Ÿ”
Iterator & Mediator: Loops and Middlemen
Behavioral

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.

๐ŸŽ™๏ธ Iterator = a pageโ€‘turner for a book. Mediator = a group chat host who passes messages between members.
โ›“๏ธ
Chain of Responsibility: Pass It Along
Behavioral

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.

๐Ÿ™‹ Like asking a classroom: "Does anyone know the answer?" Each student checks; if not, they pass it to the next student.
๐Ÿ–ฅ๏ธ
MVC & MVVM: The Big Organizers
Architectural

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.

๐Ÿง  Model
โ‡„
๐ŸŽฎ Controller / ViewModel
โ‡„
๐Ÿ–ผ๏ธ View
๐ŸŽฌ The Model is the script, the View is the screen, and the Controller is the director calling the shots.
๐Ÿง…
Layered & Hexagonal Architecture
Architectural

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.

๐Ÿง… Layered = an onion with concentric layers. Hexagonal = a castle with a strong core and multiple drawbridges.
๐Ÿ”—
Dependency Injection & Eventโ€‘Driven Design
Architectural

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.

๐Ÿ“จ DI = ordering pizza delivery instead of cooking. Eventโ€‘driven = an announcement board: whoever cares about the announcement reacts.
๐Ÿงญ
Choosing the Right Architecture
Wisdom

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.

๐Ÿ  A treehouse needs different plans than a skyscraper. Don't build a skyscraper when you just need a treehouse.
โš ๏ธ Remember: Patterns and architectures are tools, not goals. Choose the simplest style that solves your problem, and don't let patterns make your code harder to read. Clarity beats cleverness every time.

๐Ÿ› ๏ธ Hands-on activities (no boring stuff!)

1

Strategy swap: Create a TravelPlanner that accepts a strategy object (CarRoute, BikeRoute, WalkRoute). Swap strategies at runtime and print the route description.

2

Weather station: Build a WeatherStation (subject) with subscribe() and notify(). Add three observers that each print a different format of the temperature.

3

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.

4

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.

5

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.

6

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.

7

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.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’