Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting rid of Singletons

Status
Not open for further replies.

santir

Programmer
Apr 7, 2010
1
UY
Hi! I'm a game developer working on an engine in Flash ActionScript 3.0.
The engine features some global instances of classes, which are used much like a Singleton would.
For example, the SpriteManager is used whenever a game sprite needs to be created, so each class that uses a sprite accesses the SpriteManager globally.
One problem this presents is in the case where we have several games running inside the same application, and we want a different SpriteManager for each one of them.
A possible solution is to pass the SpriteManager (or any other of the current Singletons) around as arguments, but that seems hard to mantain and not very elegant - we'd have to pass it through *many* classes.
What would be a better / more OOP-friendly solution?
Any comment would be appreciated!
 
I'm not familiar with flash, but in languages like .net and java dependency injection is the preferred OO method of supplying an object with external functionality. The dependency is usually provided 1 of 2 ways.
1. as a constructor argument (considered required)
2. as a member argument (considered optional)

here is an example of constructor injection in C#
Code:
class Foo
{
   private Bar b;
   public Foo(Bar b)
   {
      this.b = b;
   }
}
and some examples of member injection
Code:
class Foo
{
   private Bar b;
   public Foo()
   {
   }

   public Bar B {get{return b;} set {b = value;}}

   public void SetBar(Bar b)
   {
       this.b = b;
   }

   public void DoSomethingWithBar(string s, Bar b)
   {
      //do something with this, b and s
   }
}
this can lead to rather lengthy objects graphs and writing
Code:
foo = new Foo(new Bar(new FuBar()));
can become tedious. one approach is to use a factory object which becomes responsible for building Foo
Code:
class FooFactory
{
   public Foo Create()
   {
       return new Foo(new Bar(new FuBar()));
   }
}
abstracting this idea to a higher level is the concept of dependency injection containers. where you can tell the container what types of objects you have and it's smart enough to figure out how to put them together
Code:
singleton = new Container()
   .Register(typeof(Foo))
   .Register(typeof(Bar))
   .Register(typeof(FuBar));
Code:
foo = singleton.Resolve(typeof(Foo));
again this is how OOP languages like .net and java have solved the dependency resolution problem. I don't know what it would look like for flash, or if this type of overhead is acceptable for a video game.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top