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).
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.
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.
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.
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.
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.
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.
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 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.
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.
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.
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.
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.
Pizza builder: Write a PizzaBuilder with methods for size, crust, sauce, and toppings. Build two different pizzas with readable chained calls.
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().
Dress your coffee: Implement a Coffee class and decorators for milk, sugar, and whipped cream. Compute the total cost by wrapping layers.
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.
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.