Forum Discussion

menghao's avatar
menghao
Visitor
9 years ago

Dose the singleton pattern work in Scene graph node?

Hi,

I have some problems in using Singleton pattern. First I defined a function as below. I stored the config from the remote server.
function getConfigFromRemote() as Object
    if m.configFromRemote = Invalid then
        m.configFromRemote = loadConfigFromServer()
    end if
    return m.configFromRemote
end function

I first called this function in main(), here it sends the request to remote server for the first time and returned the config data. Then in the Scene Graph Node I defined called homeScene.xml, I imported the file that contained this getConfigFromRemote() function, and called the function again for the config, and I find out that the m.configFromRemote is actually invalid and it has to request from remote again. I can assure that the getConfigFromRemote() in homeScene.xml is called after the function get called in main(). Its in key pressed event actually.

Does anybody have any idea of why is that? and how should I use Singleton in this case?
  • You are free to use whatever patterns you please, if you at all please. <shrug/>
    Just know "m" in main and a node are completely different variables. 
  • "EnTerr" wrote:
    You are free to use whatever patterns you please, if you at all please. <shrug/>
    Just know "m" in main and a node are completely different variables. 

    Maybe my expression is not clear, I believed the question I asked were why and how not can I use? no need for the mocking. Of course I know m in main and node are different, otherwise, the code I wrote should have worked. But thanks for your reply anyway.
  • "menghao" wrote:
    "EnTerr" wrote:
    You are free to use whatever patterns you please, if you at all please. <shrug/>
    Just know "m" in main and a node are completely different variables. 

    Maybe my expression is not clear, I believed the question I asked were why and how not can I use? no need for the mocking. Of course I know m in main and node are different, otherwise, the code I wrote should have worked. But thanks for your reply anyway.

    Not mocking you. The first sentence was trying to convey that software design patters are just "kata", choreographed patterns of "movements". It is up to you as developer to choose whether and which to use in a particular "fight", as appropriate. You cannot blindly expect the "forms" that they are to work everywhere.

    The second line was answering your why in "Does anybody have any idea of why is that?".

    Now if you meant to ask "why there is no visibility between different parts when programming for SG", i think the unequivocal answer is "bad design". Bad design in the sense that instead of dealing with the real issues, it was a cover-up where multithreading was rushedly painted over, leaving some dangerous structural issues under the stucco