Skip to content

A simple implementation of the service locator design pattern in C++

License

Notifications You must be signed in to change notification settings

Seng3694/ServiceLocator

Repository files navigation

C++ Simple ServiceLocator

A simple implementation of the service locator design pattern in C++.

Usage

Single Instance

Register an instance:

ServiceLocator locator;
locator.registerInstance<IMathService>(new MathService());

And resolve it somewhere else:

auto svc1 = locator.resolve<IMathService>();
auto svc2 = locator.resolve<IMathService>(); // <= same instance

Lazy Initialization

Register a delegate which creates a shared pointer to the corresponding instance:

ServiceLocator locator;
locator.registerCreator<IMathService>([]() { return std::make_shared<MathService>(); });

Everytime you call resolve, the service locator will call the delegate and return the pointer:

auto svc1 = locator.resolve<IMathService>();
auto svc2 = locator.resolve<IMathService>(); // <= not the same instance

Clearing all registrations

You can clear all registrations manually (will be called in destructor automatically):

locator.clear();

License

You can redistribute it and/or modify it under the terms of the MIT license. See LICENSE for details.

About

A simple implementation of the service locator design pattern in C++

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published