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
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