
On Jan 17, 2008 6:06 PM, Gennadiy Rozental <rogeeff@gmail.com> wrote:
Anthony Williams <anthony_w.geo <at> yahoo.com> writes:
A singleton enforces that there is only one global instance with global accessibility. If you just create one (and pass it down by dependency injection) then the decision to just create one is made at the top level (where it belongs), and the client code doesn't have to be fussed with how many instances there are --- just that it can use the one provided.
void some_function(Logger& logger) { logger.log("something"); }
int main() { Logger myLogger; // ooh look, just one some_function(myLogger); }
This design has obvios drawback, right: you need to pass the instance around.
Some consider this a feature.
Global free function is as bad as global variables IMO and as bad as singlton for what it worth.
How is a global free function is as bad as a global variable? You cannot mutate a function, while you can certainly mutate a variable. What is bad is not the visibility, but the mutability. (I do not think anybody has problems with global constants). I agree that a global variable is as bad as a singleton, though. -- gpd