RenderHeads ©2021
Author / Maintainer: Ross Borchers
"The service locator pattern is a design pattern used in software development to encapsulate the processes involved in obtaining a service with a strong abstraction layer. This pattern uses a central registry known as the "service locator", which on request returns the information necessary to perform a certain task." - Wikipedia
Used for storing, and retrieving services through AddService
, GetService
and RemoveService
the base interface all services must inherit. Serves to signal intent and has no exposed members.
You could extend this interface to introduce an abstraction layer on retrival of a service if required. If a MonoBehaviour is required you would need to create a new version of MonoService extending the new interfacee.
A MonoBehaviour service.
OnEnable
adds and OnDisable
removes the service from the ServiceLocator. Remember to call these base implementations if OnEnable
or OnDisable
are called.
A wrapper for services that allows for lazy retrival of services so you dont have to assing them manually.
eg.
class MyGameManager : MonoBehaviour
{
//No explicit assignment required
private LazyService<MyMonoService> _service;
private void Start()
{
_service.Value.Foo();
}
}
the alternative approach would be
_service = ServiceLocator.GetService<MyMonoService>();