Switched from the service locator pattern to singleton pattern

This commit is contained in:
Kayne Ruse
2013-06-24 09:00:50 +10:00
parent 7ad855348f
commit f049c96df7
12 changed files with 41 additions and 69 deletions
+3 -3
View File
@@ -20,7 +20,7 @@
* distribution.
*/
#include "network_queue.hpp"
#include "service_locator.hpp"
#include "singleton.hpp"
#include "udp_network_utility.hpp"
@@ -37,7 +37,7 @@ static std::deque<Packet> queue;
static bool running = false;
static int networkQueue(void*) {
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
while(running) {
SDL_SemWait(lock);
while(netUtil->Receive()) {
@@ -102,7 +102,7 @@ Packet popNetworkPacket() {
}
void flushNetworkQueue() {
UDPNetworkUtility* netUtil = ServiceLocator<UDPNetworkUtility>::Get();
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();
SDL_SemWait(lock);
while(netUtil->Receive());
queue.clear();
@@ -19,24 +19,20 @@
* 3. This notice may not be removed or altered from any source
* distribution.
*/
#ifndef SERVICELOCATOR_HPP_
#define SERVICELOCATOR_HPP_
#ifndef SINGLETON_HPP_
#define SINGLETON_HPP_
template<typename T>
class ServiceLocator {
class Singleton {
public:
static T* Set(T* t) {
delete service;
return service = t;
}
static T* Get() {
return service;
return &instance;
}
private:
static T* service;
static T instance;
};
template<typename T>
T* ServiceLocator<T>::service = nullptr;
T Singleton<T>::instance;
#endif