https://github.com/rsaz/servicelocator
Service Locator Pattern Header-Only Library
https://github.com/rsaz/servicelocator
cpp header-only service-locator-pattern slocator
Last synced: 7 months ago
JSON representation
Service Locator Pattern Header-Only Library
- Host: GitHub
- URL: https://github.com/rsaz/servicelocator
- Owner: rsaz
- License: mit
- Created: 2021-12-10T08:10:11.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-01-11T00:54:34.000Z (over 4 years ago)
- Last Synced: 2025-10-12T13:23:12.321Z (7 months ago)
- Topics: cpp, header-only, service-locator-pattern, slocator
- Language: C++
- Homepage:
- Size: 25.4 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Service Locator
Very fast, header-only C++ Service Locator Pattern library
### What is the Service Locator Pattern
The Service Locator Pattern is a design pattern that encapsulates the processes involved in obtaining a service with a strong abstraction layer. This pattern uses a central registry know as the "Service Locator" to request either Singleton instances or make use of a factory to generate instances of a particular type. This pattern simplifies component-based application where all applications dependencies are cleanly listed at the beggining of the application boostrap.
### Motivation
To simplify the classes relationships and dependencies, defining a single point of class registration and request. To reduce software development complexity
### Advantages
- Simplifies the use of singleton and static objects
- Centralize the use of objects in one class called locator
- The code is easy to test
### Install
##### Copy the includes [folder](https://github.com/rsaz/ServiceLocator/tree/master/include/slocator) to your build tree and use a C++ compilter >= 11
```
$ git clone https://github.com/rsaz/ServiceLocator.git
```
### Platforms
- Windows (msvc 2013+, cygwin)
### Package Manager
### Features
- Register service ```RegisterService()```
- Unregister service ```UnregisterService()```
- Request service ```Get()```
- Request service list ```ServicesList()```
- Register service factory ```RegisterServiceFactory()```
- Unregister service factory ```UnregisterServiceFactory()```
- Request service factory ```Get()```
- Request service factory list ```ServicesFactoryList()```
### Usage examples
```c++
// Class Examples
using Locator::ServiceLocator;
#pragma region Classes Examples
class ILogger
{
public:
virtual void Info(const std::string& message) = 0;
};
class Logger : public ILogger
{
public:
// Inherited via ILogger
virtual void Info(const std::string& message) override
{
std::cout << message << std::endl;
}
};
class IConfiguration
{
public:
virtual void Load() = 0;
};
class Configuration : public IConfiguration
{
public:
void Load()
{
std::cout << "Loading configuration\n";
}
};
#pragma endregion
auto main() -> int
{
// Service Locator initialization
auto locator = std::make_unique();
// Service Registration
locator->RegisterService();
locator->RegisterService();
// Check double registration
locator->RegisterService();
// Request the service
auto logger1 = locator->Get();
// Guarantee same instance
auto logger2 = locator->Get();
logger1->Info("information");
logger2->Info("information");
// Check Unregister Singleton Service
locator->ServicesList();
locator->UnregisterService();
locator->ServicesList();
// Clear all services (Singleton and Transients)
locator->Clear();
// Service Factory Creation
locator->RegisterServiceFactory();
// Get a new instance upon each request
auto config1 = locator->Get();
auto config2 = locator->Get();
config1->Load();
config2->Load();
// Check Unregister Factory Services
locator->ServicesFactoryList();
locator->UnregisterServiceFactory();
locator->ServicesFactoryList();
return 0;
}
```