R – AS3 Object Oriented GUI Design

actionscript-3menuoopuser-interface

I'm trying to make a small Flash game that has a GUI, which is basically a menu where players can select certain items, sort of like the Tower Defense games.

Would it be a good idea to make the GUI a singleton? Because obviously there can only be 1 instance of the GUI class.

Is there a better way?

Best Solution

In my opinion, the rest of the system should not know about your GUI, so making it a singleton is pointless. The view (GUI) should bind to some kind of models to show the current state.

In general, it's a good idea to avoid Singletons altogether.

By binding to a model I mean something like that:

class MyGUI
{
    ...
    public function set game(g:Game):void {
        g.addEventListener('pointsChanged', function(e:Event):void {
            ptsLabel.text = g.points.toString() + " points";
        })
    }
}

class Game extends EventDispatcher
{
    private var _points:int = 0;

    public function get points():int {
        return _points;
    }

    public function set points(points:int):void {
        _points = points;
        dispatchEvent(new Event('pointsChanged'));
    }
}