Design patterns
Proven answers to problems that keep coming back in code. Each pattern has the idea in plain words, when to use it, its trade-offs and a small Java example you can run.
Creational · 4
How objects get created, so the code that uses them doesn’t depend on how.
- SingletonA class that makes sure only one instance of itself exists, and hands everyone that same instance. A shared database connection
- FactoryOne place decides which concrete class to create, so the rest of the code only asks for what it needs. A pizza shop that makes margherita and pepperoni
- BuilderBuild a complex object step by step, setting only the parts you need, then get the finished object in one call. A car with maker, model, year, colour and gearbox
- PrototypeCreate a new object by copying an existing one, then change only what differs. Copies of a Ford Mustang
Structural · 7
How classes and objects fit together into larger structures.
- AdapterA thin wrapper that lets a class work with code that expects a different interface. A USB drive used through a memory card interface
- BridgeSplit a class into two parts that can change on their own: what it does, and how it gets done. An application that runs on Windows or Mac
- CompositeTreat a single object and a group of objects the same way, by building them into a tree. Files and directories in a file system
- DecoratorWrap an object in another object that adds behaviour, without changing the original class. Booking plans that add to the price
- FacadeOne simple class in front of a complex system, so callers talk to it instead of to every part. Buying and selling shares through one class
- FlyweightShare the common part of many similar objects, so large numbers of them cost little memory. Trees in a forest that share their shape and colour
- ProxyA stand-in with the same interface as the real object, which controls when and how the real one is used. A database that connects only when asked
Behavioral · 8
How objects share the work and talk to each other.
- Chain of ResponsibilityPass a request along a chain of handlers, where each one can deal with it or hand it to the next. Middleware that checks a web request
- CommandTurn a request into an object, so it can be passed around, queued, logged or undone. Commands that turn a light on and off
- IteratorWalk through the items of a collection one by one, without knowing how the collection stores them. A small collection backed by an array
- ObserverOne object announces a change, and every object that subscribed to it hears about it. A tweet and its followers
- StateAn object changes its behaviour when its internal state changes, as if it had changed its class. A transaction that moves from pending to approved
- StrategyPut each way of doing a task in its own class, and choose which one to use while the program runs. A logger that writes to a file or the console
- Template MethodA base class fixes the steps of a task, and subclasses fill in the steps that differ. Importing CSV, Excel and Word files
- VisitorAdd new operations to a set of classes without changing them, by moving each operation into its own visitor. A payroll for full-time and part-time staff