An open API service indexing awesome lists of open source software.

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++

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