Signals: Event-Driven Programming in Unity | Learning Zenject [7]

Event-driven programming has never been easier thanks to Zenject Signals.

Event-driven programming is an approach to writing code in which the flow of your program is controlled by events. Some objects are responsible for raising events, while other objects are responsible for listening and reacting to those events.

If you’ve every done event-driven programming then you’re probably already familiar with the Observer Pattern. The Observer Pattern is a design pattern in which an observer, called the subject, is responsible for notifying a list of listeners, called observers, of any state changes.

This is a great paradigm because it allows us to write code that is completely decoupled. Two classes can communicate with one another without ever knowing about each other’s existence. These classes can be written and tested in complete isolation.

Zenject Signals

Zenject Signals is a Zenject specific implementation of the Observer Pattern. A Signal is an object that keep track of a list of observers, and can be invoked by any class that has a reference to it.

We can see how easy they make writing event-driven code in this example found in Zenject’s documentation:

Example of Zenject Signals

If you’d like to learn more about Zenject, please check out my series¬†Learning Zenject, starting with Getting Started with Zenject.