5
MAR
2011

Game Service Broker

When you write games, there is often a need to access the different subsystems of the game engine from within various objects. If you pass along pointers to all the subsystems all the time you are going to get messy code and tight object couplings.

For this reason, my game engine provides access to the subsystems via a service broker that is a singleton object. This way, the subsystems can be accessed from anywhere with the benefits of loose couplings, but without most of the problems related to truly global objects.

Each service must implement a protocol interface that I have named GameService. The interface consists of one single property; the name of the service type. The object that requests a service has the responsibility to use the service correctly by casting it to the correct class, or at least only using selectors that it responds to.

As always, you can do whatever you want with code that I put on this blog, but I take no responsibility for it or what you do with it. :-)

Header file for game service protocol (GameService.h):

#import <Foundation/Foundation.h>
 
// A GameService is an object that provides a service to other objects.
@protocol GameService
 
// Each service should have a unique name identifying
// the type of service it provides.
@property(nonatomic, readonly) NSString *serviceName;
 
@end

The service broker class is named GameServiceBroker and it keeps track of one service for each unique service type.

Header file for service broker (GameServiceBroker.h):

#import <Foundation/Foundation.h>
#import "GameService.h"
 
// A game service broker is a singleton object that keeps track of
// game services and provides access to them. 
@interface GameServiceBroker : NSObject
{
@private
    // Holds the services.
    NSMutableDictionary *services;
}
 
// Get the global instance of the service broker.
+ (GameServiceBroker *)sharedGameServiceBroker;
 
// Gets the object providing the specified type of service.
+ (id)serviceOfType:(NSString *)serviceType;
 
// Adds a service to the set of game services.
- (void)addService:(id<GameService>)service;
 
// Gets the object providing the specified type of service.
- (id)serviceOfType:(NSString *)serviceType;
 
// Removes and destroys the object providing the specified service.
- (void)removeService:(NSString *)serviceType;
 
@end

Implementation file (GameServiceBroker.m):

#import "GameServiceBroker.h"
 
@implementation GameServiceBroker
 
+ (GameServiceBroker *)sharedGameServiceBroker
{
    static GameServiceBroker *sharedInstance = nil;
 
    if (sharedInstance)
    {
        return sharedInstance;
    }
 
    @synchronized(self)
    {
        if (sharedInstance == nil)
        {
            sharedInstance = [[self alloc] init];
        }
    }
 
    return sharedInstance;
}
 
+ (id)serviceOfType:(NSString *)serviceType
{
    return [[GameServiceBroker sharedGameServiceBroker] 
            serviceOfType:serviceType];
}
 
- (id)init
{
    if (self = [super init])
    {
        services = [[NSMutableDictionary alloc] init];
    }
 
    return self;
}
 
- (void)dealloc
{
    [services release];
    [super dealloc];
}
 
- (void)addService:(id <GameService>)service
{
    [services setObject:service forKey:[service serviceName]];
}
 
- (id)serviceOfType:(NSString *)serviceType
{
    return [services objectForKey:serviceType];
}
 
- (void)removeService:(NSString *)serviceType
{
    [services removeObjectForKey:serviceType];
}
 
@end


 
Name:





CAPTCHA Image
Reload Image
(Surround code with --code-- and --/code--)


Comments are subject to review and will not be shown until they have been approved, for the purpose of keeping spammers and morons away.


 

About This Site

Hello, my name is Martin Johannesson and this is my home on the web. I live in Stockholm, Sweden, where I work as a software engineer at a software company.

Ever since I was a kid and discovered the art of programming on my C64, I've been tinkering with my own little software projects and experiments. This site is one such experiment.
more...

Recent Entries RSS Feed

Tags

Amiga blog C Cocoa game GLGX GLSL iOS iPad iPhone Java jQuery Mac Mac OS X Objective-C OpenAL OpenGL Programming REBOL Shaders Vertex Shader web

Blog Archive

2011: 01 02 03 04 05 06 07 08 09 10 11 12
2010: 01 02 03 04 05 06 07 08 09 10 11 12
2009: 01 02 03 04 05 06 07 08 09 10 11 12

Random Images Load new images

loading
loading
loading
loading
loading
loading
loading
loading
loading
loading
loading
loading