2
votes

I'm making a game in as3 using flash develop and flash cs5. Everything is object oriented. I was wondering, should I have one "gateway" class that has a property-reference to all instantiations of other classes, and I just pass this gateway class to new objects, so they have access to every class. Like so:

 var block:Block = new Block(gateway);

 //In the block class:
 this.gateway.player.setHealth(100);
 //Or:
 this.gateway.input.lock();

Is this like a singleton pattern or something? Should I do this?

3

3 Answers

2
votes

I've used this idea before. I used it as a model. the dataModel - or an effective backbone of which dumb stores your objects - or references to your objects. This is useful as you can always find something without walking through your classes.

I've used it as a singleton - therefore you have no fear of potentially creating a new version of your dataModel and references blank objects. Kinda stops you scratching your head for hours. It would look something like this:

package com.strangemother.models.DataModel
{
    private static var _dataModel:DataModel;
    /**
    an example of storing you items.
    */
    public var character:Character;

    public static function getInstance():DataModel
    {


     if(!_dataModel)
        {
  _dataModel= new DataModel(SingletonLock); 
  return _dataModel;

     }

 public function DataModel(lock:Class)
 {
  if(!lock is SingletonLock)
  {
   throw new Error("DataModel is a singleton. Use DataModel.getInstance() to use.");
  }
 }
}

Addition @AttackingHobo:

this is a one ( 1 ) this is the letter L lowercase ( l )
1
votes

May be a good approach to quickly try something, but on the long run that gateway object will become so gigantic and the relation between the different objects so many that it will fall under its own weight.

Also, one of the points of object orientation is to encapsulate the access of parts of the code to only those objects that require it.

0
votes

The concept is called inversion of control. It looks like you've created a service locator. There are alternative ways to implement inversion of control such as dependency injection. Martin Fowler has an excellent article on the whole concept, you should read it and then decide for yourself.