Quantcast

Singleton getInstance with parameters

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Singleton getInstance with parameters

dimitri
Hello,

I'm french so sorry for my english !

I would like to create a singleton class with parameters.

For example : general.Form1.getInstance(param1, param2);

In the constructor of Form1 i have :

        construct : function(param1, param2) {
                this.base(arguments, param1, param2);
        },

But param1 and param2 are already empty.

How can i pass paramaters when i create or get my singleton ???

Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Singleton getInstance with parameters

MartinWittemann
Administrator
Hey,
having parameters in a singleton is against the pattern in general. Imagine a simple scenario:

// some place
var a = Singleton.getInstance(1,2);  // returns a configured singleton with 1 and 2

// ...
// some other place
var b = Singleton.getInstance(2,1); // what should it return? A new instance configured with the new parameter or the old one and ignore the parameter?

You see, it is some kind of a problem here because you would either have more than one instance or ignore the second set of parameters.

If you think that one of the both suggested ways is exactly what you need, you can still configure your class the way you want with a own getInstance method.
But from a software development point of view, I would really not recommend that.

Regards,
Martin


Am 05.01.2011 um 17:41 schrieb dimitri:

>
> Hello,
>
> I'm french so sorry for my english !
>
> I would like to create a singleton class with parameters.
>
> For example : general.Form1.getInstance(param1, param2);
>
> In the constructor of Form1 i have :
>
> construct : function(param1, param2) {
> this.base(arguments, param1, param2);
> },
>
> But param1 and param2 are already empty.
>
> How can i pass paramaters when i create or get my singleton ???
>
> Thanks
> --
> View this message in context: http://qooxdoo.678.n2.nabble.com/Singleton-getInstance-with-parameters-tp5892532p5892532.html
> Sent from the qooxdoo mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> Learn how Oracle Real Application Clusters (RAC) One Node allows customers
> to consolidate database storage, standardize their database environment, and,
> should the need arise, upgrade to a full multi-node Oracle RAC database
> without downtime or disruption
> http://p.sf.net/sfu/oracle-sfdevnl
> _______________________________________________
> qooxdoo-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel


------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and,
should the need arise, upgrade to a full multi-node Oracle RAC database
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Singleton getInstance with parameters

Gregory Beaver
Hi,

Just a quick note: there are many real-life situations that one would want singletons to be different based on the arguments.  In pattern-speak, this is usually referred to as an object registry, and qooxdoo does this to garbage collect its objects, but a hybrid singleton/object registry makes sense sometimes.

For a real-life example, a program I wrote in PHP uses a configuration object that is a singleton per-directory, so that if one requests a configuration object that is for an existing configuration, it retrieves the in-memory configuration if it has already been loaded, and otherwise creates a new object and loads the configuration from disk.  To differentiate from a singleton, I used a different method name, however, because it would be confusing to those who would read "singleton" and assume it operates as a normal singleton.

Of course, one needs to look long and hard at ANY use of a singleton, there are all kinds of hidden pitfalls.  If possible, it's far better to redesign to avoid singleton at all.  Most knee-jerk examples of a singleton are simply not good design and can be handled instead through some form of composition or delegation instead.

Greg

------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and,
should the need arise, upgrade to a full multi-node Oracle RAC database
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Singleton getInstance with parameters

Derrell Lipman
On Thu, Jan 6, 2011 at 15:46, Greg Beaver <[hidden email]> wrote:
For a real-life example, a program I wrote in PHP uses a configuration object that is a singleton per-directory, so that if one requests a configuration object that is for an existing configuration, it retrieves the in-memory configuration if it has already been loaded, and otherwise creates a new object and loads the configuration from disk.  To differentiate from a singleton, I used a different method name, however, because it would be confusing to those who would read "singleton" and assume it operates as a normal singleton.

Cool! A multiton! :-)

Actually I've done something very similar. There are occasional uses for a series of singletons, identified by something. Coming up with how that identification works, in a generic fashion, is likely prone to error. Is something a single parameter? Two? Three? An array? An object? You could treat the series of parameters as an array and require that all values have to match (the OP's scenario, using integers, I think) but what if some of those parameters are themselves objects or arrays? Does it require that a deep comparison return true, or more strictly that the reference be the same?

I think this pattern is probably best devised uniquely for each application that requires it.

Derrell


------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and,
should the need arise, upgrade to a full multi-node Oracle RAC database
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Singleton getInstance with parameters

Petr Kobalíček
In reply to this post by dimitri
Hi Dimitri,

you can create this function yourself and simply remove the
"singleton" type of class in a declaration, for example:

qx.Class.define(...

  statics: {
    getInstance: function(param1, param2)
    {
      // Your initializer...
    }
  }

It's not a singleton anymore.

Hope that helps
Petr

On Wed, Jan 5, 2011 at 5:41 PM, dimitri <[hidden email]> wrote:

>
> Hello,
>
> I'm french so sorry for my english !
>
> I would like to create a singleton class with parameters.
>
> For example : general.Form1.getInstance(param1, param2);
>
> In the constructor of Form1 i have :
>
>        construct : function(param1, param2) {
>                this.base(arguments, param1, param2);
>        },
>
> But param1 and param2 are already empty.
>
> How can i pass paramaters when i create or get my singleton ???
>
> Thanks
> --
> View this message in context: http://qooxdoo.678.n2.nabble.com/Singleton-getInstance-with-parameters-tp5892532p5892532.html
> Sent from the qooxdoo mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> Learn how Oracle Real Application Clusters (RAC) One Node allows customers
> to consolidate database storage, standardize their database environment, and,
> should the need arise, upgrade to a full multi-node Oracle RAC database
> without downtime or disruption
> http://p.sf.net/sfu/oracle-sfdevnl
> _______________________________________________
> qooxdoo-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
>

------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and,
should the need arise, upgrade to a full multi-node Oracle RAC database
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
qooxdoo-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Loading...