Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was reading what a service locator pattern is from this article, and now I'm just wondering if I merge service and locator classes is it still a service locator pattern or not? And if not, do you think this implementation is usable enough as single or service locator pattern? (since the example was an audio manager I'll give same example here)

class IAudio{
    static IAudio* instance;
    static IAudio _NULLINSTANCE;        
        instance = &NULLINSTANCE;

    IAudio* getInstance() 
        return instance;

    void registerInstance(IAudio* pInstance) 
        instance = pInstance;

    virtual void load(const char* path) 
        //do nothing

    virtual void play(const char* path) 
        //do nothing

    virtual void stop(const char* path) 
        //do nothing

And then I'll inherit classes I need from AudioManager and call registerInstance in my initialization function.

share|improve this question
Why the char*? At least const it, if not even const std::string&. – The Communist Duck Jun 6 '11 at 17:40
Using a const char * rather than a const string & can do wonders to prevent memory fragmentation and allocator contention, especially on C++ standard libraries that don't use the small string optimization. – user744 Jun 6 '11 at 19:14
please don't be too hard on just an example. – Ali.S Jun 6 '11 at 19:18
why all downvote? it's a design that is usualy can used in games! – Ali.S Jun 6 '11 at 20:33
@Gajet sounds like something needs to be fixed in your team's communication, rather than in the code ;) – Maik Semder Jun 8 '11 at 10:45
up vote 3 down vote accepted

This is no longer the service locator pattern because anything that would like to talk to the audio service, now has to know about the class, it's location/package etc..

Take a good look at this quote from the site you linked:

These callsites, of course, have to be coupled to something in order to make a sound play, but letting them poke directly at the concrete audio implementation is like giving a hundred strangers directions to your house just so they can drop a letter on your doorstep. Not only is it a little bit too personal, it’s a real pain when you move and you have to tell each person the new directions.

In the final example any game object that can ask for the audio engine only has to know about the IAudio interface and where the locator is. Not where the actual AudioService class is. By putting the locator inside the AudioService you violate this idea, and you just get a weird singleton.

share|improve this answer
it's defenetly not a singleton since there is function registerInstance and by the way any one can see both IAudio and locator services. so didn't reveal any information about how ConsoleAudio is implemented. as i comment for communistduck my interface doesn't do anything at all, it's just representing as NULLAudio class as well as others. – Ali.S Jun 6 '11 at 20:17

First off, this is not gamedev related and as such is a more SO question. Also, why should it matter if you're using the service locator pattern? Why should it matter if your code is an observer, or a visitor? Design patterns are meant as structures that can be used for generic aim. It is unlikely you will need them in their raw form.

Anyway, it is not. The idea of a service pattern is to act as a messenger. You, person X, want to get an object Z. The service provider Y simply has a reference to Z, and passes you that. The idea is that you do not need to know anything about Z (other than what it is). It could be a red object, a blue object, a yellow object - the service provider is simply giving you the object without having to reference it exactly.

In this case, the load/play/stop functions are just showing this as a singleton. You want three classes; an interface IAudio which is what you program against, a AudioServiceProvider which has the IAudio pointer, and a number of Audio concrete classes that implement IAudio. You have missed out the middle step.

share|improve this answer
In my example the middle part is not forgotten, usually there has to be a number of audio concrete classes that Implement IAudio, it's only baseclass is also the NULL class. in that article there were 4 classes introduced : IAudio, ConsoleAudio, Locator, and NullAudio. The only thing I've done was mergeing Locator with IAudio and NullAudio to just orginise my code a little bit. and I'm asking cause i don't want to endup with some antipaterns inside my code. – Ali.S Jun 6 '11 at 18:44
@Gajet The fact you are using singletons indicates antipatterns don't seem to worry you. – The Communist Duck Jun 6 '11 at 20:31
@TheCommunistDuck : singletons aren't antipatterns, or at least as I read about that pattern in wikipedia it's categorised as a pattern! and I myself didn't ever use them in the past conversations I just mentions it's widly used in many GameEngines. at the end I said i use global pointers myself. – Ali.S Jun 6 '11 at 20:39
@Gajet Singletons are widely regarded as an anti-pattern, since they are used widely even though their actual use case is tiny. And Wikipedia isn't always right. – The Communist Duck Jun 7 '11 at 10:18
Actually Wikipedia mentions that "some consider it an Anti-Pattern" – Maik Semder Jun 7 '11 at 12:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.