https://github.com/seng3694/servicelocator
A simple implementation of the service locator design pattern in C++
https://github.com/seng3694/servicelocator
cpp servicelocator
Last synced: about 1 year ago
JSON representation
A simple implementation of the service locator design pattern in C++
- Host: GitHub
- URL: https://github.com/seng3694/servicelocator
- Owner: Seng3694
- License: mit
- Created: 2018-10-28T19:25:21.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-02-19T08:59:19.000Z (over 4 years ago)
- Last Synced: 2025-03-28T07:51:15.058Z (about 1 year ago)
- Topics: cpp, servicelocator
- Language: C++
- Size: 5.86 KB
- Stars: 21
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# C++ Simple ServiceLocator #
A simple implementation of the service locator design pattern in C++.
## Usage ##
### Single Instance ###
Register an instance:
```cpp
ServiceLocator locator;
locator.registerInstance(new MathService());
```
And resolve it somewhere else:
```cpp
auto svc1 = locator.resolve();
auto svc2 = locator.resolve(); // <= same instance
```
### Lazy Initialization ###
Register a delegate which creates a shared pointer to the corresponding instance:
```cpp
ServiceLocator locator;
locator.registerCreator([]() { return std::make_shared(); });
```
Everytime you call `resolve`, the service locator will call the delegate and return the pointer:
```cpp
auto svc1 = locator.resolve();
auto svc2 = locator.resolve(); // <= not the same instance
```
### Clearing all registrations
You can clear all registrations manually (will be called in destructor automatically):
```cpp
locator.clear();
```
## License ##
You can redistribute it and/or modify it under the terms of the MIT license. See [LICENSE][1] for details.
[1]:LICENSE