I've been wondering if there was a clear design pattern when dealing with the following situation:
I have some data that is closely related to each other and I want some functions that modify and give behavior to this data. This seems like the perfect situation to use a class however I only need a single instance of the class.
Would it be better to keep the class and have a global instance or to store the variables in a namespace with the functions?
I'm currently doing the namespace approach when I encounter these situations however I don't like the loss of encapsulation yet I also don't like using global variables. I feel like there should already exist a well thought out solution that I've just never heard of before. Anyone can help on the matter?
class example:
class someclass {
public:
double somedouble = 0.0;
int someint = 0;
void somefunc() {
// do some stuff with the variables
somedouble = someint * 2;
++someint;
}
} someinstance;
vs
namespace example:
namespace mynamespace {
double somedouble = 0.0;
int someint = 0;
void somefunc() {
// do some stuff with the variables
somedouble = someint * 2;
++someint;
}
} // namespace mynamespace