Solution: Singleton (Flexible)¶
Main Program¶
#include "social-insurance-svs.h"
#include "social-insurance-oegk.h"
#include <iostream>
#include <string>
#include <memory>
int main()
{
// setup phase, next to program startup
SocialInsurance::set_instance(std::make_unique<SVS>());
std::string id("1037190666");
SocialInsurance::instance().charge(id, 1254.60);
SocialInsurance::instance().charge(id, 231.34);
std::cout << id << " owes \"" << SocialInsurance::instance().name() << "\" " << SocialInsurance::instance().debt(id) << " Euros" << std::endl;
// can only be a runtime error: trying to overwrite singleton
// instance
SocialInsurance::set_instance(std::make_unique<OEGK>());
return 0;
}
Two Concrete Implementations¶
OEGK¶
#pragma once
#include "social-insurance-flexible.h"
#include <iostream>
#include <map>
class OEGK : public SocialInsurance
{
public:
OEGK() : SocialInsurance("OEGK") {}
virtual void charge(const std::string& id, double euros);
virtual double debt(const std::string& id) const;
private:
std::map<std::string, double> _database;
};
#include "social-insurance-oegk.h"
void OEGK::charge(const std::string& id, double euros)
{
_database[id] += euros;
}
double OEGK::debt(const std::string& id) const
{
auto found = _database.find(id);
if (found == _database.end())
throw std::runtime_error("not found");
return found->second;
}
SVS¶
#pragma once
#include "social-insurance-flexible.h"
#include <iostream>
#include <map>
class SVS : public SocialInsurance
{
public:
SVS() : SocialInsurance("SVS") {}
virtual void charge(const std::string& id, double euros);
virtual double debt(const std::string& id) const;
private:
std::map<std::string, double> _database;
};
#include "social-insurance-svs.h"
void SVS::charge(const std::string& id, double euros)
{
_database[id] += euros * (110.0L / 100);
}
double SVS::debt(const std::string& id) const
{
auto found = _database.find(id);
if (found == _database.end())
throw std::runtime_error("not found");
return found->second;
}
SocialInsurance
Interface¶/trainings/material/soup/cxx-code/design-patterns-singleton/social-insurance-flexible.h
¶/trainings/material/soup/cxx-code/design-patterns-singleton/social-insurance-flexible.cpp
¶