PDA

View Full Version : Tying MVC together with AS3


NickZA
06-10-2008, 02:29 PM
Hi

I'm building the framework of a game at the moment and am using the MVC approach. The State DP is integrated into this structure to define gamestates. Also, there are a numerous singletons in the class structure including 2 discrete data models, the logger and the console.

While broadcasting events out from these objects enables loose coupling, the array of controllers (states) need direct access to the data models. For MVC, this is typical.

So, my problem is getting a data model reference out to the various states that perform the game logic. My problem is essentially twofold:

1. I want to maintain loose coupling to keep my codebase extensible.
2. I want to use the singleton pattern for singleton objects, so that there is a single, accessible point of access to the model for all objects (such as states) to reference each singleton. (I am not worried about enforcing single instantiation, only about referencing a single instance.)
3. I want to give my controller objects an implicit way of referencing the data models (preferably in keeping with point 1), eg. I want to avoid passing arguments, using globals, etc.


When the model is created, I can do one of the following to get it's reference to the individual states:

--Create the singleton objects and have any states call a static getInstance method of the Singleton superclass -- except static functions aren't inherited. This is my preferred route but I can't get it working, see here:
http://www.kirupa.com/forum/showthread.php?t=223798&page=35
(In spite of my response there, I later realised I was no better off with the given "solution".)

--Create semi-singleton objects (no instantiation enforcement, but still have non-static getInstance() methods referencing a static instance variable). I like this option, but can anyone clarify why Singleton's need a static getInstance() method? Am I safe in dropping it?

--Every time a new state is called, pass the data model ref into the new state, from either the old state or the StateManager (which holds a permanent reference to it), as a parameter. I don't like this: it just seems ridiculous to be passing params when in reality every state should, by it's very nature, be able to access the model. In fact, the model(s) should be able to be accessed by pretty much any object in my program that wishes to access it.

--Don't enforce singleton behaviour, instead store the ref in global variable. I smell rubbish...!


Long post, I know. Any ideas? :confused:

-Nick

creynders
06-16-2008, 10:38 AM
I'll answer you questions in a different, but probably more understandable order:
Create semi-singleton objects (no instantiation enforcement, but still have non-static getInstance() methods referencing a static instance variable). I like this option, but can anyone clarify why Singleton's need a static getInstance() method? Am I safe in dropping it?
You need some kind of static access point. Whether it's a property or method is irrelevant, but it will have to be static. Why? Because the whole point of the singleton class is to leave the responsibility of instantiation out of the hands of the user of the class. If you create a non-static method ( aka a INSTANCE method ) you already need an instance to ... access the instance. As you can see that's a bit like the chicken and the egg.
Create the singleton objects and have any states call a static getInstance method of the Singleton superclass -- except static functions aren't inherited. This is my preferred route but I can't get it working
And you won't get it working. There's a reason why you haven't found a extendable singleton class online, because it's not possible. For each singleton class you'll have to write a getInstance method and and a instance property, there's no escaping that.
For a singleton class you need a static property which stores the single instance of the class and a static method to access that static property. Since static functions and variables aren't a part of the inheritance chain, what you're trying to do is impossible.

So, the real solution is:
drop the idea of a super singleton class.
Implement the singleton pattern in each class that needs it.
Let the various states access the singletons directly.

NickZA
06-16-2008, 12:16 PM
Hi Creynders

Remember receiving useful help from you in the past, good to see you again.

I didn't think that there was a way around it and since writing this had started to come around to your way of thinking -- thanks for the shove, however :p. I think it was needed.

As you say, there are a ton of different methods for implementing faux singletons in AS3, and not one of them can satisfy all criteria of a true singleton, obviously.

Thanks again.

-Nick

swivelmaster
08-26-2008, 08:27 PM
Why not use an MVC framework that has already been built for you?

We've been using PureMVC at my company since before I started working here and it solves all of those problems.

creynders
08-27-2008, 08:18 AM
Yep, puremvc is magnificent. The learning curve might be a bit steep if you're not familiar with mvc, but it's worth the trouble. And the most excellent part of the framework is the huge amount of documentation, tutorials and info found in the forums.

swivelmaster
09-01-2008, 11:32 PM
I'd actually say the documentation for PureMVC both hurts and helps. When I started here, my boss explained a little bit about MVC in general, a bit about PureMVC, then showed me their incredibly complicated chart which demonstrates the structure.

My eyes glazed over.

Then he said, "Yeah this chart isn't very good. Ignore this. I'll just show you what we're doing with it."

Unfortunately, PureMVC doesn't solve *every* question about design, so we've been able to code some pretty rickety stuff that started out as a quick fix and then sat around for months before it was cleaned up. This is also a byproduct of just how flexible Flex and AS3 are and the fact that it's possible to put business logic in your view components (generally a bad thing to do) in Script tags. So be careful!