From f049c96df7d20f7f01bd8a1884d899f765d6d00f Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 24 Jun 2013 09:00:50 +1000 Subject: [PATCH] Switched from the service locator pattern to singleton pattern --- client/in_world.hpp | 10 +++++----- client/lobby.hpp | 10 +++++----- client/main_menu.hpp | 4 ++-- client/option_screen.hpp | 4 ++-- client/scene_manager.cpp | 18 +++--------------- client/scene_manager.hpp | 10 +++++----- client/splash_screen.hpp | 6 +++--- libs/common/network_queue.cpp | 6 +++--- .../{service_locator.hpp => singleton.hpp} | 16 ++++++---------- server/server_application.cpp | 14 +++----------- server/server_application.hpp | 6 +++--- test/foobar.cpp | 6 +----- 12 files changed, 41 insertions(+), 69 deletions(-) rename libs/common/{service_locator.hpp => singleton.hpp} (81%) diff --git a/client/in_world.hpp b/client/in_world.hpp index 1c90f41..4682a3f 100644 --- a/client/in_world.hpp +++ b/client/in_world.hpp @@ -25,7 +25,7 @@ #include "base_scene.hpp" #include "utilities.hpp" #include "defines.hpp" -#include "service_locator.hpp" +#include "singleton.hpp" #include "packet_type.hpp" #include "network_queue.hpp" #include "information_manager.hpp" @@ -65,10 +65,10 @@ protected: void HandleDisconnection(::Disconnect&); //services - ConfigUtility* configUtil = ServiceLocator::Get(); - SurfaceManager* surfaceMgr = ServiceLocator::Get(); - UDPNetworkUtility* netUtil = ServiceLocator::Get(); - InformationManager* infoMgr = ServiceLocator::Get(); + ConfigUtility* configUtil = Singleton::Get(); + SurfaceManager* surfaceMgr = Singleton::Get(); + UDPNetworkUtility* netUtil = Singleton::Get(); + InformationManager* infoMgr = Singleton::Get(); //members RasterFont font; diff --git a/client/lobby.hpp b/client/lobby.hpp index dfb80bb..76b9b91 100644 --- a/client/lobby.hpp +++ b/client/lobby.hpp @@ -25,7 +25,7 @@ #include "base_scene.hpp" #include "utilities.hpp" #include "defines.hpp" -#include "service_locator.hpp" +#include "singleton.hpp" #include "packet_type.hpp" #include "network_queue.hpp" #include "information_manager.hpp" @@ -72,10 +72,10 @@ protected: void BeginGame(JoinResponse&); //services - ConfigUtility* configUtil = ServiceLocator::Get(); - SurfaceManager* surfaceMgr = ServiceLocator::Get(); - UDPNetworkUtility* netUtil = ServiceLocator::Get(); - InformationManager* infoMgr = ServiceLocator::Get(); + ConfigUtility* configUtil = Singleton::Get(); + SurfaceManager* surfaceMgr = Singleton::Get(); + UDPNetworkUtility* netUtil = Singleton::Get(); + InformationManager* infoMgr = Singleton::Get(); //members Button refreshButton; diff --git a/client/main_menu.hpp b/client/main_menu.hpp index 74e00a5..82c8d37 100644 --- a/client/main_menu.hpp +++ b/client/main_menu.hpp @@ -23,7 +23,7 @@ #define MAINMENU_HPP_ #include "base_scene.hpp" -#include "service_locator.hpp" +#include "singleton.hpp" #include "surface_manager.hpp" #include "button.hpp" @@ -47,7 +47,7 @@ protected: void MouseButtonUp(SDL_MouseButtonEvent const&); void KeyDown(SDL_KeyboardEvent const&); - SurfaceManager* surfaceMgr = ServiceLocator::Get(); + SurfaceManager* surfaceMgr = Singleton::Get(); Button startButton; Button optionsButton; diff --git a/client/option_screen.hpp b/client/option_screen.hpp index 5375820..5561f92 100644 --- a/client/option_screen.hpp +++ b/client/option_screen.hpp @@ -23,7 +23,7 @@ #define OPTIONSCREEN_HPP_ #include "base_scene.hpp" -#include "service_locator.hpp" +#include "singleton.hpp" #include "surface_manager.hpp" #include "button.hpp" @@ -44,7 +44,7 @@ protected: void MouseButtonUp(SDL_MouseButtonEvent const&); void KeyDown(SDL_KeyboardEvent const&); - SurfaceManager* surfaceMgr = ServiceLocator::Get(); + SurfaceManager* surfaceMgr = Singleton::Get(); Button backButton; }; diff --git a/client/scene_manager.cpp b/client/scene_manager.cpp index a0e6294..dfc2c6c 100644 --- a/client/scene_manager.cpp +++ b/client/scene_manager.cpp @@ -54,7 +54,7 @@ SceneManager::~SceneManager() { * operations. * Important things to note: * The APIs are initiated here. - * The global objects are created here. + * The global objects are initialized here. * The game's screen is created here, based on information loaded from the config file. * The ConfigUtility's call to Load() also ensures that the "rsc\" folder is in the directory. It's easy to forget it. */ @@ -62,7 +62,6 @@ SceneManager::~SceneManager() { void SceneManager::Init() { //load the config file try { - configUtil = ServiceLocator::Set(new ConfigUtility()); configUtil->Load("rsc/config.cfg"); } catch(std::runtime_error& e) { @@ -89,12 +88,7 @@ void SceneManager::Init() { SDL_GetVideoInfo()->vfmt->BitsPerPixel, flags); - //instanciate the remaining services - surfaceMgr = ServiceLocator::Set(new SurfaceManager()); - netUtil = ServiceLocator::Set(new UDPNetworkUtility()); - infoMgr = ServiceLocator::Set(new InformationManager()); - - //initiate the remaining services + //initiate the remaining singletons netUtil->Open(0, sizeof(Packet)); } @@ -137,16 +131,10 @@ void SceneManager::Proc() { } void SceneManager::Quit() { - //clean up the services + //clean up the singletons netUtil->Close(); surfaceMgr->FreeAll(); - //delete the services - configUtil = ServiceLocator::Set(nullptr); - surfaceMgr = ServiceLocator::Set(nullptr); - netUtil = ServiceLocator::Set(nullptr); - infoMgr = ServiceLocator::Set(nullptr); - //clean up the scene UnloadScene(); diff --git a/client/scene_manager.hpp b/client/scene_manager.hpp index cfcd532..d2d4310 100644 --- a/client/scene_manager.hpp +++ b/client/scene_manager.hpp @@ -24,7 +24,7 @@ #include "scene_list.hpp" #include "base_scene.hpp" -#include "service_locator.hpp" +#include "singleton.hpp" #include "packet_type.hpp" #include "information_manager.hpp" @@ -51,10 +51,10 @@ private: BaseScene* activeScene = nullptr; - ConfigUtility* configUtil = nullptr; - SurfaceManager* surfaceMgr = nullptr; - UDPNetworkUtility* netUtil = nullptr; - InformationManager* infoMgr = nullptr; + ConfigUtility* configUtil = Singleton::Get(); + SurfaceManager* surfaceMgr = Singleton::Get(); + UDPNetworkUtility* netUtil = Singleton::Get(); + InformationManager* infoMgr = Singleton::Get(); }; #endif diff --git a/client/splash_screen.hpp b/client/splash_screen.hpp index 919c82c..ccbe6ee 100644 --- a/client/splash_screen.hpp +++ b/client/splash_screen.hpp @@ -23,7 +23,7 @@ #define SPLASHSCREEN_HPP_ #include "base_scene.hpp" -#include "service_locator.hpp" +#include "singleton.hpp" #include "config_utility.hpp" #include "surface_manager.hpp" @@ -44,8 +44,8 @@ protected: void LoadResources(); bool loaded = false; - ConfigUtility* configUtil = ServiceLocator::Get(); - SurfaceManager* surfaceMgr = ServiceLocator::Get(); + ConfigUtility* configUtil = Singleton::Get(); + SurfaceManager* surfaceMgr = Singleton::Get(); Image logo; std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); }; diff --git a/libs/common/network_queue.cpp b/libs/common/network_queue.cpp index 4ac1126..2c3dbaf 100644 --- a/libs/common/network_queue.cpp +++ b/libs/common/network_queue.cpp @@ -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 queue; static bool running = false; static int networkQueue(void*) { - UDPNetworkUtility* netUtil = ServiceLocator::Get(); + UDPNetworkUtility* netUtil = Singleton::Get(); while(running) { SDL_SemWait(lock); while(netUtil->Receive()) { @@ -102,7 +102,7 @@ Packet popNetworkPacket() { } void flushNetworkQueue() { - UDPNetworkUtility* netUtil = ServiceLocator::Get(); + UDPNetworkUtility* netUtil = Singleton::Get(); SDL_SemWait(lock); while(netUtil->Receive()); queue.clear(); diff --git a/libs/common/service_locator.hpp b/libs/common/singleton.hpp similarity index 81% rename from libs/common/service_locator.hpp rename to libs/common/singleton.hpp index 157b453..e6062a2 100644 --- a/libs/common/service_locator.hpp +++ b/libs/common/singleton.hpp @@ -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 -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 -T* ServiceLocator::service = nullptr; +T Singleton::instance; #endif diff --git a/server/server_application.cpp b/server/server_application.cpp index 197b512..d8ee983 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -44,14 +44,13 @@ ServerApplication::~ServerApplication() { * operations. * Important things to note: * The APIs are initiated here. - * The global objects are created here. + * The global objects are initialized here. * The ConfigUtility's call to Load() also ensures that the "rsc\" folder is in the directory. It's easy to forget it. */ void ServerApplication::Init() { //load the config file try { - configUtil = ServiceLocator::Set(new ConfigUtility()); configUtil->Load("rsc/config.cfg"); } catch(std::runtime_error& e) { @@ -70,10 +69,7 @@ void ServerApplication::Init() { throw(runtime_error("Failed to initialize SDL_net")); } - //instanciate the remaining services - netUtil = ServiceLocator::Set(new UDPNetworkUtility()); - - //initiate the remaining services + //initiate the remaining singletons netUtil->Open(configUtil->Int("server.port"), sizeof(Packet)); //create the threads @@ -107,13 +103,9 @@ void ServerApplication::Quit() { //close the threads endQueueThread(); - //clean up the services + //clean up the singletons netUtil->Close(); - //delete the services - configUtil = ServiceLocator::Set(nullptr); - netUtil = ServiceLocator::Set(nullptr); - //deinitialize the APIs SDLNet_Quit(); } diff --git a/server/server_application.hpp b/server/server_application.hpp index 1f65ed3..02c849e 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -24,7 +24,7 @@ #include "utilities.hpp" #include "packet_type.hpp" -#include "service_locator.hpp" +#include "singleton.hpp" #include "network_queue.hpp" #include "config_Utility.hpp" @@ -79,8 +79,8 @@ private: void HandleDisconnection(Disconnect&); //services - ConfigUtility* configUtil = nullptr; - UDPNetworkUtility* netUtil = nullptr; + ConfigUtility* configUtil = Singleton::Get(); + UDPNetworkUtility* netUtil = Singleton::Get(); //members Clock::time_point lastTick = Clock::now(); diff --git a/test/foobar.cpp b/test/foobar.cpp index 2f53595..abbd4b3 100644 --- a/test/foobar.cpp +++ b/test/foobar.cpp @@ -1,5 +1 @@ -#include "service_locator.hpp" - -int FooBar() { - return *ServiceLocator::Get(); -} \ No newline at end of file +#include "foobar.hpp"