Object-oriented programming encapsulates data and functions in single objects. A new class that should have the functionality of another class can either have a pointer to an object of this class or can inherit its functionality through polymorphism. In some cases this can lead to a huge inheritance chain, making it nearly impossible to add changes in any base or child class. The programmer has to know the functionality of every class in the inheritance chain and changing one class can lead to a strange bug in another class. Additionally it is not always clear, what class to choose as proper base class. Having pointers to objects of another class decouples them and can make it easier to add changes. But some classes may implement some equal routines, which makes it hard to choose which routine shall be used in the class having pointers to these classses. Another important aspect is reusing already created classes in another context. This is not always possible without changing and adapting specific behaviour, which leads to code duplication and is kind of a bug magnet.
An Entity-Component-System is a programming technique that is capable to deal with such problems.

What is an Entity-Component-System

An Entity-Component-System uses composition of components, instead of object-oriented inheritance, to construct different objects. These objects are called entities. Every entity has an unique identifier which works like the primary key in databases. With their respective identifier, the components associated with an entity can be addressed. The components themselves are data-only classes or structs (POD) with no member methods to process their data. The data processing is done by the so called systems. Every system depends on a specific subset of components and processes all entities having these components. For example a system that should move an entity in the world depends on the position and the velocity of an entity. All entities having a position and a velocity component are processed by this movement system which updates the position component, depending on the initial position, the velocity and a given time step. The updated position component can then be used by other systems, for example to draw an entity at the new position on the screen.
To allow any component composition, ideally no component depends on another component or knows about the existence of other components. This makes it possible to add, remove or modify single components or systems without breaking down the functionality of other components or systems. Additionally unit testing becomes very easy and it also makes components and systems highly reusable in other projects.
To say it in few words, an entity-component-system breaks monolithic classes down in simpler domain-specific components. The original class becomes kind of a container holding only the required, decoupled data and gets processed by the respective domain-specific systems.

When to use an Entity-Component-System

Using an Entity-Component-System can save lots of time spent on enhancing and debugging a project, but it also adds some complexity in maintaining your entities to your project. It also is possible, that your project doesn't fit in the Entity-Component-System approach. So first make sure your project can be improved by using an Entity-Component-System. In many cases it is easier and better to just write your classes the object-oriented way. To check if an Entity-Component-System is worth the effort, check if any of the statements below is true:

- You have different classes that call the same routines which can be covered by single systems.

- You want to keep different domains decoupled from each other, but your classes should be able to share different capabilities and can cover more than one domain.

- Many classes have a huge inheritance chain and it gets hard to work with them.

- Inheritance doesn't let you pick just the parts you need to implement a specific behaviour.

Sample Code

You can find some sample code using the provided Entity-Component-System implementation in the section Quick Tutorial.


You can find some great articles about Entity-Component-Systems on the web. Below is a list of some of them on which this webpage and the provided Entity-Component-System implementation is based.

Evolve Your Hierarchy Refactoring Game Entities with Components