QUICK REFERENCE

Design Patterns Cheat Sheet

The standard solutions to common problems in software design, with dual-language examples.

Creational

Singleton

Ensures a class has only one instance and provides a global point of access.

public class Database {
private static volatile Database instance;
private Database() {} // Private constructor
public static Database getInstance() {
if (instance == null) {
synchronized (Database.class) {
if (instance == null) instance = new Database();
}
}
return instance;
}
}
Creational

Factory Method

Creates objects without specifying the exact class of object that will be created.

interface Transport { void deliver(); }
class Truck implements Transport {
public void deliver() { System.out.println("Land"); }
}
class Ship implements Transport {
public void deliver() { System.out.println("Sea"); }
}
class Logistics {
// Factory Method
Transport createTransport(String type) {
if (type.equals("Road")) return new Truck();
return new Ship();
}
}
Creational

Builder

Constructs complex objects step by step.

class Burger {
private boolean cheese;
private boolean pepperoni;
public static class Builder {
private Burger b = new Burger();
public Builder addCheese() {
b.cheese = true; return this;
}
public Burger build() { return b; }
}
}
Burger b = new Burger.Builder().addCheese().build();
Structural

Adapter

Allows incompatible interfaces to work together.

interface Target { void request(); }
class Adaptee {
void specificRequest() { System.out.println("Called"); }
}
class Adapter implements Target {
private Adaptee adaptee = new Adaptee();
public void request() {
adaptee.specificRequest();
}
}
Structural

Decorator

Dynamically adds behavior to an object without affecting other objects.

interface Coffee { double cost(); }
class SimpleCoffee implements Coffee {
public double cost() { return 5.0; }
}
class MilkDecorator implements Coffee {
private Coffee c;
public MilkDecorator(Coffee c) { this.c = c; }
public double cost() { return c.cost() + 2.0; }
}
Structural

Facade

Provides a simplified interface to a complex system of classes.

class Computer {
void start() {
cpu.freeze();
ram.load();
cpu.execute();
}
}
// Client only needs one line:
new Computer().start();
Behavioral

Observer

Notify multiple objects about events happening to the object they're observing.

class Channel {
List<Observer> subs = new ArrayList<>();
void subscribe(Observer o) { subs.add(o); }
void notify(String msg) {
for(Observer o : subs) o.update(msg);
}
}
Behavioral

Strategy

Defines a family of algorithms and makes them interchangeable.

interface PayStrategy { void pay(int amt); }
class CreditCard implements PayStrategy {
public void pay(int amt) { /* ... */ }
}
class Cart {
void checkout(PayStrategy algo) {
algo.pay(100);
}
}
Behavioral

Command

Encapsulate a request as an object, allowing parameterization and queuing.

interface Command { void execute(); }
class LightOn implements Command {
Light light;
public void execute() { light.on(); }
}
class Remote {
void press(Command c) { c.execute(); }
}