Your implementation works on my end. The object is cached, see the output
below:

-------
samit@random:~/temp$ cat test.js
var a = require('./plugin-factory').PluginFactory,
  b = require('./plugin-factory').PluginFactory,
  c = (a === b);

console.log(c);
samit@random:~/temp$ node test.js
PluginFactory initialized
true
samit@random:~/temp$
-------

What version of node.js are you running?

Are you sure that the factory object isn't cached when you require() it?
How did you reach that conclusion? (Does it print "PluginFactory
initialized" multiple times?)

--

Finally, I'm not sure if changing PluginAction's "plugin" property (when
you serialize/deserialize it) is the best way to accomplish what you're
trying to do.

Logically speaking, when you serialize an object (ObjectA), you essentially
create a representation (ObjectB) of ObjectA. I would avoid modifying
ObjectA and instead make the necessary changes in ObjectB, since this is
when and where those changes are needed. For this reason (and others), I
think it's clearer and may help avoid issues but this is just my general
opinion.


On Tue, Mar 25, 2014 at 9:41 AM, <[email protected]> wrote:

> I realised the problem is completely different, because of my
> implementation of Singleton, the Factory is not saved/cached in the GLOBAL
> variable (which all required() variables usually are, it seems). So my
> guess now it has to do with my implementation of the Singleton pattern:
>
> var PluginFactory = (function() {
>     /**
>      * Actual implementation of the class.
>      * This is encapsulated because the class is a singleton.
>      */
>     function PluginFactory() {
>         // Implementation using prototype functions
>     }
>
>     /**
>      * This variable holds the singleton instance of the factory class.
>      */
>     var instance = null;
>
>     function initialize() {
>         console.log("PluginFactory initialized");
>     }
>
>     /**
>      * This method returns a singleton instance of the factory.
>      */
>     return {
>         getInstance: function() {
>             if (instance === null) {
>                 // Create a new instance and hide its constructor
>                 instance = new PluginFactory();
>                 instance.constructor = null;
>                 initialize();
>             }
>
>             return instance;
>         }
>     };
> })();
>
> // Export a singleton instance of the PluginFactory
> exports.PluginFactory = PluginFactory.getInstance();
>
>
> Any thoughts why this implementation isn't handled like i expect?
>
> Op dinsdag 25 maart 2014 13:50:27 UTC+1 schreef [email protected]:
>
>> Hi,
>>
>> So i'm having problems with my architecture of my node application, I
>> know the problem, I'm just trying to find the best solution.
>>
>> My problems is exactly the same as: https://groups.google.com/
>> forum/#!topic/nodejs/u_9IE2z_6rM
>>
>> Only I'm using a Factory class to create plugins of the type "IPlugin".
>> These plugins have certain actions, called PluginAction.
>>
>> A PluginAction has a field plugin, which refers to its parent plugin.
>> When serializing to JSON, i replace the plugin object with a string
>> identifier, pointing to the correct plugin.
>>
>> My PluginFactory can retrieve a plugin using retrieve(identifier). The
>> problem occurs when I try to deserialize a given PluginAction.
>>
>> At that point I would like to convert the plugin identifier back to the
>> plugin object, by retrieving it from the Factory. This is where it goes
>> wrong.
>>
>> I understand this could be a bit hard to follow, so I've included a part
>> of my class diagram, which might clarify it more. Short summary:
>>
>>
>>    1. PluginFactory.initialize() loads all plugins in a certain
>>    directory using require()
>>    2. PluginAction.toJSON() changes the plugin object to a string
>>    containing the plugin identifier
>>    3. PluginAction.fromJSON() when trying to retrieve the plugin object
>>    using the PluginFactory, I cannot load the PluginFactory file.
>>
>>  --
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to