In my base framework I like to have classes that I can reuse in several projects. Most often these are generic to allow for flexibility.
I have one tiny little header that I use to store data in and get the data easily.
#pragma once
#include <map>
#include <string>
namespace BaseFrameWork
{
template<class TValue, class TKey = std::string>
class Provider
{
public:
static TValue& Get(TKey const& key);
private:
static std::map<TKey, TValue> _dataMap;
};
}
Yeah it’s just a wrapper for a map, but I like it as sort of a central place to get Data from.
I can do Provider<Room>::Get("U-18") for example and get the object that was loaded inside from who cares where from.
I acknowledge how frowned upon it is to have global objects like this, but this is not part of a public API and only used in personal projects where the team is ok with using it.
What I want to know is if there is a name for this pattern, if it even is one. I always called it Provider-Pattern since thats what it does, provide stuff, but I saw that thats already taken.
And please refrain from telling just how bad this code is and how I should feel bad for it.
It’s a Singleton pattern, with the data item being a
map(effectively an opaque associative container when access is through a getter and presumably setter). An associative container isn’t generally considered a design pattern in and of itself.If you’re looking for a phrase that communicates this design succinctly and clearly, something like a “Singleton map initialised (when/how) and read-only thereafter”.