Unity Design Patterns
C# game programming patterns, explained with interactive animations you can poke at — and production-style Unity code you can drop into your project.
Behavioral
How objects talk, react and hand off responsibility.
Command
Wrap an action in an object so it can be queued, logged, replayed or undone.
Mediator
Route all communication between peers through one coordinator so they never reference each other.
Memento
Capture an object’s state in a sealed snapshot so it can be restored later without exposing its internals.
Observer
Let one object broadcast changes to any number of listeners without knowing who they are.
State
Give each mode of an object its own class so behaviour changes cleanly when the mode changes.
Strategy
Swap out an algorithm at runtime by hiding each variant behind a common interface.
Creational
Controlled, flexible ways to bring objects into the world.
Singleton
Guarantee a class has exactly one instance and give everyone a single, well-known way to reach it.
Builder
Assemble a complex object one readable step at a time, then hand back the finished product.
Factory
Hide which concrete class gets created behind a single method that returns an interface.
Prototype
Create new objects by copying a configured example instead of building each one from scratch.
Structural
Composing classes and objects into larger shapes.
Adapter
Wrap an incompatible API so it looks exactly like the interface your game code already expects.
Composite
Treat a single object and a whole tree of objects through the same interface.
Decorator
Stack extra behaviour onto an object at runtime by wrapping it in layers that share its interface.
Facade
Give a tangle of subsystems one simple front door so the rest of the game only makes one call.
Proxy
Stand in for an expensive or sensitive object and control when, whether and how it is actually used.
Architectural
Wiring systems together without tangling them.
Dependency Injection
Hand objects the services they need from the outside instead of letting them find or build their own.
MVP
Split UI code into a dumb View, a pure-data Model and a Presenter that mediates between them.
Service Locator
Give game code one well-known registry to ask for services, so it never needs to know where they came from.
Optimization
Game-specific tricks for keeping frame times low.
Dirty Flag
Remember that derived data is out of date and recompute it only when someone actually needs it.
Object Pooling
Recycle a fixed set of objects instead of creating and destroying them, so spawning never stalls the frame.
Spatial Partition
Bucket objects by where they are so "what is near me?" only looks at the neighbourhood, not the whole world.