Java Design Patterns
Explained in simple, everyday terms
Design patterns are just reusable, proven solutions to common problems that come up again and again when
writing object-oriented code. They aren't finished code you copy-paste — they're general templates or ways of
thinking. Below are 9 of the most common patterns in Java, grouped into three families: Creational (creating
objects), Structural (organizing objects), and Behavioral (objects talking to each other).
Creational Patterns — how objects get created
Singleton
What it means: Make sure a class has only ONE instance in the whole app, and give everyone a single shared
access point to it.
Everyday analogy: Think of it like the President's office — there's only one, and everyone goes through that
same office.
Use it when: Config managers, logging, connection pools — things you never want duplicated.
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
Factory Method
What it means: Instead of using 'new' directly, you ask a factory method to create the object for you. The factory
decides which exact class to instantiate.
Everyday analogy: Like ordering 'a pizza' from a restaurant — you don't build it yourself, the kitchen (factory)
makes it and hands it to you.
Use it when: When you don't know in advance exactly which subclass you'll need, or you want to hide creation
logic.
interface Shape { void draw(); }
class Circle implements Shape {
public void draw() { [Link]("Circle"); }
}
class ShapeFactory {
public Shape createShape(String type) {
if ([Link]("circle")) return new Circle();
return null;
}
}
Builder
What it means: Build a complex object step by step, instead of using one giant constructor with tons of
parameters.
Everyday analogy: Like ordering a sandwich at Subway — you add bread, then meat, then toppings, one step at
a time.
Use it when: Objects with many optional fields (e.g. a Pizza with size, toppings, crust type).
Pizza pizza = new [Link]()
.setSize("Large")
.setCheese(true)
.setPepperoni(true)
.build();
Structural Patterns — how objects fit together
Adapter
What it means: Converts one interface into another interface a client expects, so two incompatible things can
work together.
Everyday analogy: Like a plug adapter that lets a US phone charger fit into a European wall socket.
Use it when: Integrating an old class or a third-party library that doesn't match the interface your code expects.
interface Usb { void connectWithUsb(); }
class Microphone { void connectWithMic() { /* ... */ } }
class MicToUsbAdapter implements Usb {
private Microphone mic;
MicToUsbAdapter(Microphone mic) { [Link] = mic; }
public void connectWithUsb() { [Link](); }
}
Decorator
What it means: Add new behavior to an object at runtime by 'wrapping' it, without changing its original class.
Everyday analogy: Like adding toppings to a coffee — each topping wraps the base coffee and adds to the
price/flavor.
Use it when: You want optional add-on features without creating a huge number of subclasses.
interface Coffee { double cost(); }
class SimpleCoffee implements Coffee {
public double cost() { return 2.0; }
}
class MilkDecorator implements Coffee {
private Coffee coffee;
MilkDecorator(Coffee c) { [Link] = c; }
public double cost() { return [Link]() + 0.5; }
}
Facade
What it means: Provide one simple interface that hides a bunch of complicated subsystems behind it.
Everyday analogy: Like a car's ignition button — you press one button, and it quietly starts the fuel pump,
engine, and electronics for you.
Use it when: You want to simplify a complex library or set of classes for the rest of your app.
class CPU { void start() {} }
class Memory { void load() {} }
class ComputerFacade {
private CPU cpu = new CPU();
private Memory memory = new Memory();
public void startComputer() {
[Link]();
[Link]();
}
}
Behavioral Patterns — how objects communicate
Observer
What it means: One object (the subject) keeps a list of dependents (observers) and automatically notifies them
whenever its state changes.
Everyday analogy: Like subscribing to a YouTube channel — you get notified any time the channel posts
something new.
Use it when: Event systems, UI updates, anything with a publish/subscribe relationship.
interface Observer { void update(String msg); }
class Subscriber implements Observer {
public void update(String msg) { [Link](msg); }
}
class Channel {
List<Observer> subs = new ArrayList<>();
void notifyAll(String msg) {
for (Observer o : subs) [Link](msg);
}
}
Strategy
What it means: Define a family of interchangeable algorithms, and let the client pick which one to use at
runtime.
Everyday analogy: Like choosing a route in a maps app — walking, driving, or biking. Same destination,
different strategy.
Use it when: You have several ways to do the same task (e.g. different sorting or payment methods).
interface PaymentStrategy { void pay(int amount); }
class CreditCard implements PaymentStrategy {
public void pay(int amount) { [Link]("Paid by card"); }
}
class Checkout {
PaymentStrategy strategy;
Checkout(PaymentStrategy s) { [Link] = s; }
void doPay(int amt) { [Link](amt); }
}
Command
What it means: Turn a request or action into a standalone object, so it can be stored, passed around, queued, or
undone.
Everyday analogy: Like a restaurant order slip — the waiter writes down the request, and the kitchen executes it
later, independent of who ordered it.
Use it when: Undo/redo systems, task queues, remote-control style APIs.
interface Command { void execute(); }
class LightOnCommand implements Command {
Light light;
LightOnCommand(Light l) { [Link] = l; }
public void execute() { [Link](); }
}
Quick Reference Table
Pattern Family One-line summary
Singleton Creational Only one instance exists, shared everywhere
Factory Method Creational A method creates objects so you don't call 'new' directly
Builder Creational Build complex objects step by step
Adapter Structural Makes two incompatible interfaces work together
Pattern Family One-line summary
Decorator Structural Adds features to an object by wrapping it
Facade Structural One simple interface hides a complex subsystem
Observer Behavioral Subscribers get notified automatically on change
Strategy Behavioral Swap algorithms/behaviors at runtime
Command Behavioral Turns an action into an object you can store or undo