FAQ
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 nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscribe@googlegroups.com
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 nodejs+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Scrfhack at Mar 25, 2014 at 1:47 pm
    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 scrf...@avans.nl:
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    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 nodejs+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Sam IT at Mar 25, 2014 at 8:35 pm
    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, 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 scrf...@avans.nl:
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    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 nodejs+unsubscribe@googlegroups.com.
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    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 nodejs+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Scrfhack at Mar 27, 2014 at 9:36 am
    So i figured out what the problem was. In my initialize(), which is called
    when the factory is first loaded, it does a require() on Plugin, which does
    a require() on PluginAction, which does a require() on PluginFactory. So
    the PluginFactory is require()'d before the first require() is finished. So
    I made the initialize() a public method and call it in my app.js when the
    application starts. Now everything works fine.

    Regarding your advice about manipulating the JSON. I do not manipulate the
    original object but instead make a copy of it with the modified plugin
    variable. Ofcourse when converting back to JSON, I still need to change
    this variable back to the original object. In my opinion this was the
    only/best method to achieve the desired result. If you are curious I could
    post the toJOSN() and fromJSON() implementation here.

    Op dinsdag 25 maart 2014 21:35:25 UTC+1 schreef Sam IT:
    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, <scrf...@avans.nl <javascript:>> 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 scrf...@avans.nl:
    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 nod...@googlegroups.com<javascript:>
    To unsubscribe from this group, send email to
    nodejs+un...@googlegroups.com <javascript:>
    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 nodejs+un...@googlegroups.com <javascript:>.
    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    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 nodejs+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Bryan Donovan at Mar 26, 2014 at 3:42 am
    I might be missing the purpose of your factory, but maybe you can just
    implement it like this:

    function PluginFactory() {
         //
    }

    exports.PluginFactory = new PluginFactory();


    As long as you don't mess with the require cache, I think that would
    effectively be a singleton. Maybe there are other reasons to implement the
    singleton like you did though.

    -Bryan

    On Tuesday, March 25, 2014 6:50:27 AM UTC-6, scrf...@avans.nl wrote:

    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    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 nodejs+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Scrfhack at Mar 27, 2014 at 9:38 am
    This could indeed be a possible solution. To be honest I haven't thought of
    this approach. I chose my implementation because it was given as best
    practice on using Singleton's in Javascript, and it seemed to have the most
    in common with Singleton implementations in for example C# or Java. I've
    got my solution working (See reply on Bryan), otherwise I would have gone
    for your approach, which basically gives the same result.

    Op woensdag 26 maart 2014 04:42:03 UTC+1 schreef Bryan Donovan:
    I might be missing the purpose of your factory, but maybe you can just
    implement it like this:

    function PluginFactory() {
    //
    }

    exports.PluginFactory = new PluginFactory();


    As long as you don't mess with the require cache, I think that would
    effectively be a singleton. Maybe there are other reasons to implement the
    singleton like you did though.

    -Bryan

    On Tuesday, March 25, 2014 6:50:27 AM UTC-6, scrf...@avans.nl wrote:

    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 nodejs@googlegroups.com
    To unsubscribe from this group, send email to
    nodejs+unsubscribe@googlegroups.com
    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 nodejs+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupnodejs @
categoriesnodejs
postedMar 25, '14 at 12:50p
activeMar 27, '14 at 9:38a
posts6
users3
websitenodejs.org
irc#node.js

3 users in discussion

Scrfhack: 4 posts Bryan Donovan: 1 post Sam IT: 1 post

People

Translate

site design / logo © 2022 Grokbase