We are going to implement a basic Service Locator pattern to expose three specific services, as follows:
- Logger: A service that acts as a facade to a centralized logging system
- Analytics: A service that sends custom analytical information to a backend to provide insight on player behavior
- Advertisement: A service that pulls video advertisements (ads) from a network and displays them to monetize the game's content at specific moments
We are adding these services to the registry of the Service Locator pattern because of their following characteristics:
- They offer a specific service.
- They need to be accessible from anywhere in the code base.
- They can be mocked or removed without causing any regression in the gameplay code.
As we are going to see from the following code example, implementing a basic Service Locator pattern is a straightforward process. These are the steps we'll take:
- Let's start by implementing the most...