Pattern

Flame on

A design pattern is a general solution to a commonly occurring problem. They work in any language but the details might be different. Patterns are useful when talking about problems and trying to start flame wars on programming forums. There are two ways to use patterns, to communicate or to enforce them. The first one is useful, the second one is a waste of time. Saying that something is a singleton is good as it makes it easy for others to quickly understand how it is used. Writing books of code to make sure no one ever create more then one is bad as it will only make the code harder to follow.

Creation

Singleton

A singleton is a single global object that can be accessed by a wide number of class and/or modules. One example is a Input object used to read from the keyboard and mouse. We only need one of that object and we might use it from anywhere in the code.

Object pool

Instead of creating a new object each time one is needed create a pool of objects and reuse them. If you are writing a top down shooter in unity you will have many projectiles active at the same time. When a projectile is about to be destroyed move it outside the world and disable it. When you need a new projectile move it back, enable it and restart it. That way you avoid allocating memory and any other setup cost for the object.

Factory

A factory is a object that is responsible for creating other objects. The objects created are often from a common base class or the same class with different settings. One example would be a ParticleSystem factory that have a create function that takes the name of the particle system to create.

Structural

Facade

A facade is a manager class that provide a single interface to a large number of related objects. One example would be a Audio manager that keep track of all the waves, mp3's and sounds playing. When you play a sound you do not need to know about the magic that happens behind the facade. It is common for a Facade to also be a Singleton.

Behavioral

State

A state machine makes it possible to have an object move between a number of states. One example would be a MainMenu in a game that can have states for all the submenus. Also used in AI as FSM.

Reference