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

Module Three

๐Ÿ“– 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.
4
Module 3: Design Patterns โ€“ Creational & Structural for Young Coders
โญ Module 3 ยท Level Three

Design Patterns:
Creational & Structural

๐Ÿงฑ Lego bricks for building better software

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

  • โœ” What design patterns are โ€” and why they're like cheat codes for programmers
  • โœ” How to create objects the smart way (Factory, Builder, Singleton)
  • โœ” How to structure objects so they fit together neatly (Adapter, Decorator, Facade)
  • โœ” When NOT to use a pattern (spoiler: sometimes simpler is better)
  • โœ” How patterns combine in real frameworks
  • โœ” How to implement patterns in Python, Java, and C++
๐Ÿ“–
What Are Design Patterns?
The Big Idea

A design pattern is a well-known recipe for solving a common programming problem. It's not code you copy โ€” it's a template you adapt to your situation.

Patterns are grouped into three families: creational (how to make objects), structural (how to connect objects), and behavioral (how objects communicate โ€” coming next module).

๐Ÿช A recipe isn't a cookie. But with the recipe, you can bake cookies that turn out great every time.
๐Ÿญ
Factory Method: The Object Bakery
Creational

Instead of calling new Circle() everywhere in your code, you ask a factory to create the right object for you. The factory decides which class to instantiate.

Why? Because if you later add a new shape, you only update the factory โ€” not every place in your code that creates objects. This ties nicely into the Open/Closed principle.

๐Ÿฅ You ask the bakery for "a pastry." The bakery picks the right recipe. You don't need to know how to bake it yourself.
๐Ÿญ๐Ÿญ
Abstract Factory: The Theme Pack
Creational

An abstract factory creates families of related objects. For example, a "Light Theme" factory creates light buttons, light windows, and light menus. A "Dark Theme" factory creates all dark versions.

You pick the factory once, and everything you create afterward matches the theme. Perfect for apps that support multiple looks or platforms.

๐ŸŽจ Choosing a Minecraft texture pack: every block changes to match the theme, all at once.
๐Ÿ”จ
Builder: Step-by-Step Construction
Creational

Some objects are complicated โ€” a Pizza with crust, sauce, cheese, and toppings. The Builder pattern lets you construct it step by step: .withCrust("thin"), .withTopping("pepperoni"), .build().

This avoids monstrous constructors with 12 parameters. Each step is readable, and you only include what you need.

๐Ÿ• Like ordering a pizza on an app: pick size, crust, sauce, toppings โ€” one tap at a time, then hit "order."
1๏ธโƒฃ
Singleton: Only One Allowed
Creational

The Singleton pattern guarantees that only one instance of a class ever exists, and provides a global way to access it. Useful for things like a configuration manager or a database connection pool.

Warning: Singletons can make testing tricky and introduce hidden global state. Use sparingly โ€” often a dependency-injected single instance is better.

๐Ÿ‘‘ A kingdom has one king. Everyone knows who the king is, and there's never a second one.
๐Ÿ”Œ
Adapter: The Universal Plug
Structural

You have a class with the right data but the wrong interface. The Adapter wraps it and translates its methods into what your code expects.

Example: your app expects fetch(), but a third-party library provides getData(). An adapter class bridges the gap, so you don't have to rewrite anything.

โœˆ๏ธ A travel power adapter: your plug doesn't match the wall socket, so you use an adapter instead of rewiring the building.
๐ŸŽ
Decorator: Wrap It Up
Structural

The Decorator pattern lets you add behaviour to an object by wrapping it โ€” without changing the original class or creating a giant inheritance tree.

Example: a Coffee object can be wrapped with MilkDecorator, then SugarDecorator, then WhipDecorator. Each adds something small. You pay for exactly what you add.

๐Ÿงฃ You don't get a whole new coat to stay warm โ€” you add a scarf, then gloves, then a hat. Layer by layer.
๐ŸŒณ
Composite: Treat One and Many the Same
Structural

The Composite pattern lets you build tree structures where a single item and a group of items look the same to the code using them. A File and a Folder both have a size() method โ€” but the folder's size is the sum of its contents.

Perfect for UI trees, file systems, organization charts, and menus.

๐ŸŽ’ A backpack can hold a single pencil โ€” or a small pencil case, which holds many pencils. Either way, you can ask "how much does this weigh?"
๐Ÿ›๏ธ
Facade: One Simple Front Door
Structural

A Facade gives you a single, simple interface over a complicated subsystem. Instead of calling five classes to start a video, you call videoPlayer.play(), and the facade handles all the internal steps.

It doesn't hide the complex parts โ€” it just gives you an easier way in. Great for taming messy libraries.

๐Ÿš— Driving a car involves engine, transmission, and brakes โ€” but you just press the gas pedal. The car's controls are a facade over the machinery.
๐Ÿชถ
Flyweight & Proxy: Light and Guarded
Structural

Flyweight saves memory by sharing common data across many objects. A forest of 10,000 trees doesn't need 10,000 separate copies of the same tree model โ€” it shares one.

Proxy stands in for another object, controlling access to it. A proxy could lazy-load a huge image only when needed, or check permissions before letting you use a database.

๐ŸŒฒ Flyweight = 10,000 trees drawn from one stamp. Proxy = a security guard standing at the door, only letting you in if you have permission.
โš ๏ธ
When NOT to Use a Pattern
Wisdom

Patterns are powerful, but overusing them is a trap. Beginners sometimes try to fit every pattern into every project โ€” that leads to over-engineering: extra classes, more complexity, and less clarity.

Ask yourself: "Does this problem actually need a pattern?" Often a simple function or plain class is better. Patterns are tools, not trophies. Use them when they genuinely simplify the design โ€” not to look clever.

๐Ÿ”จ You don't use a power drill to hang a single picture. Sometimes a hammer (or a simple design) is the right tool.
โš ๏ธ Remember: Design patterns are solutions to recurring problems, not a checklist. Always prefer the simplest design that works. If you're unsure, start simple and refactor when the need appears.

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

1

Factory bakery: Create a ShapeFactory that returns the right shape object based on a string ("circle", "square", "triangle"). Add a new shape โ€” and notice you only change the factory.

2

Theme switch: Build an abstract factory for a "Light" and "Dark" theme, each producing matching Button and Window objects. Swap the factory at runtime and see the whole UI change.

3

Pizza builder: Write a PizzaBuilder with methods for size, crust, sauce, and toppings. Build two different pizzas with readable chained calls.

4

Adapter rescue: Take an old class with a weird method name (like doStuff()) and wrap it in an adapter so it fits a modern interface with run().

5

Dress your coffee: Implement a Coffee class and decorators for milk, sugar, and whipped cream. Compute the total cost by wrapping layers.

6

File tree: Build a File and Folder class using the Composite pattern. Give both a size() method. Add files to folders, nest folders, and print the total size.

7

Pattern detective (bonus): Search a real open-source project (or your own code) for at least one pattern in use. Write a short note describing which pattern it uses and why.

๐Ÿ†

By the end of this module, you'll be able to: explain the difference between creational and structural patterns, implement Factory, Builder, Singleton, Adapter, Decorator, Composite, and Facade, and judge when a pattern helps โ€” and when it hurts.

๐Ÿ† 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.
โ†’