The practical value is the interface which defines how you log things.
If you use a global variable, then everywhere you want to log something you have to use that particular global variable. You've just tightly coupled all that code. If you decide to change how things work, you have to change a lot of code. With a method call, you don't have to know ahead of time what's you're going to get or how it's implemented. All of that stuff is hidden by the method call. Once you separate yourself from the implementation, you can change it without having to go through the rest of your code to maintain the tight coupling.
You still want to pass around references because you want every thing working with the same copy of things, and any changes apply to every thing that uses it. Since you want a reference, it's not too much of a leap to use one that's been blessed.
You don't need a slow or repititous call either. You make your own package that represents the logger. Behind that is all the gory details. When you decide to switch from L4p to something else, your code doesn't need to know that. The class name and the name of the logger aren't global variables: they are symbolic constants. They represent things you hard-coded (or determined) somewhere else so you don't have to know what it is ahead of time. Behind that package you figure out how to implement it, and are free to change the decision without rippling changes through the code base.
my $log = Log->new( SYMBOLIC_CONSTANT );
The Singleton is a way to control access without giving people direct access. When I use them, the actual object is a lexical variable in its package. Anyone who wants a reference to it has to go through the interface. They can't futz with package names. They get their reference to it, store it in whichever variable they like, and get on with life. You don't get that control with package variables.
In reply to Re^2: Log::Log4perl and singleton design issue
by brian_d_foy
in thread Log::Log4perl and singleton design issue
by BUU
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |