473,699 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton

Ugo
Hi guys,

how do you make a singleton access class?

Do you know a better way of this one:

var singletonClass = (function( )
{
// Private variable
var instance = null;

// Private Constructor
function myClass( )
{
//...
}

return new function( )
{
this.constructo r = null;

this.getInstanc e = function( )
{
if( ! instance )
{
instance = new myClass( );
instance.constr uctor = null;
}

return instance;
}
}
})( );
Jun 27 '08 #1
29 1732
On Wed, 7 May 2008 10:42:51 +0200, Ugo <pr*****@nospam .itwrote:
>Hi guys,

how do you make a singleton access class?

Do you know a better way of this one:

var singletonClass = (function( )
{
// Private variable
var instance = null;

// Private Constructor
function myClass( )
{
//...
}

return new function( )
{
this.constructo r = null;

this.getInstanc e = function( )
{
if( ! instance )
{
instance = new myClass( );
instance.constr uctor = null;
}

return instance;
}
}
})( );
Pure qui? :-))
se ti vede ZERO...
ciao
Jun 27 '08 #2
Do you know a better way of this one:
Yes

change this:
* *return new function( )

to this:
return function( )

;-)
Jun 27 '08 #3
On May 7, 11:06*am, RoLo <roloswo...@gma il.comwrote:
Do you know a better way of this one:

Yes

change this:
** *return new function( )

to this:
* * return function( )

;-)
ok... sorry, I over read you code.. you can ignore my previous comment.
Jun 27 '08 #4
Ugo
>>Hi guys,
>>how do you make a singleton access class?
Do you know a better way of this one:
[cut]
Pure qui? :-))
ehh, già :)
Mi è tornato questo cruccio, e non so' fare nè trovare di meglio...
per tanto o provato a vedere cosa ne pensano qui...
se ti vede ZERO...
ssshhh, zitto, non dire niente che forse non se ne accorge
:P
ciao
Bye
Jun 27 '08 #5
Ugo wrote:
how do you make a singleton access class?
How do you define a "singleton access class"?
Do you know a better way of this one:
The best way of doing something depends at least in part on what it is
you are trying to do.
var singletonClass = (function( )
{
// Private variable
var instance = null;
There is little point in assigning null to this variable as its default
undefined value will be just as false as the null value when you test it
with - !instance -.
// Private Constructor
function myClass( )
{
//...
}

return new function( )
I can think of no circumstances under which it makes sense to use the -
new - operator with a function expression as its operand. In this case
it would be simpler (and more efficient) to have an object literal
returned at this point. That object could easily define - constructor -
and - getInstance - properties.
{
this.constructo r = null;

this.getInstanc e = function( )
{
if( ! instance )
{
instance = new myClass( );
instance.constr uctor = null;
As you are using a constructor to create this object instance it would
be possible to define the created object's - constructor property on the
prototype of the constructor. Though there may be no good reason for
using a contractor to create the object at all as you are only going to
be creating one instance of the object, and so again a literal could be
used instead.
}

return instance;
}
}
})( );
The result of those changes could look like:-

var singletonClass = (function( ){
// Private variable
var instance;
return ({
constructor:nul l,
getInstance:fun ction( ){
return (
instance||
(
instance = {
constructor:nul l
}
)
);
}
});
})();

- though that is almost certainly less than would be needed in any real
context, but you will always suffer that if you don't define what it is
exactly that you are trying to achieve.

Richard.

Jun 27 '08 #6
On May 7, 1:42 am, Ugo <priv...@nospam .itwrote:
Hi guys,

how do you make a singleton access class?

Do you know a better way of this one:

var singletonClass = (function( )
{
// Private variable
var instance = null;

// Private Constructor
function myClass( )
{
//...
}

return new function( )
{
this.constructo r = null;

this.getInstanc e = function( )
{
if( ! instance )
{
instance = new myClass( );
instance.constr uctor = null;
}

return instance;
}
}

})( );
This does not look like something a JavaScript programmer would write.
In JavaScript, assigning an object literal to a global variable would
likely achieve the purpose of a Singleton pattern in Java, for
example, for many situations. What is your real application?

Peter
Jun 27 '08 #7
Ugo
>how do you make a singleton access class?
How do you define a "singleton access class"?
i wanted a function/object which restricts the instantiation of one my
object one time and which it ables one global access
>Do you know a better way of this one:

The best way of doing something depends at least in part on what it is
you are trying to do.
A generic way to create a singleton object (and if it's possible I'd like
to adopt the GoF's "roles" - applyed to JS)
>var singletonClass = (function( )
{
// Private variable
var instance = null;

There is little point in assigning null to this variable as its default
undefined value will be just as false as the null value when you test it
with - !instance -.
IMHO, (new myClass()) can not be null|false|unde fined..
so that initialization was good
> // Private Constructor
function myClass( )
{
//...
}

return new function( )

I can think of no circumstances under which it makes sense to use the -
new - operator with a function expression as its operand. In this case
it would be simpler (and more efficient) to have an object literal
returned at this point. That object could easily define - constructor -
and - getInstance - properties.
ahh, in effect :P
> {
this.constructo r = null;

this.getInstanc e = function( )
{
if( ! instance )
{
instance = new myClass( );
instance.constr uctor = null;

As you are using a constructor to create this object instance it would
be possible to define the created object's - constructor property on the
prototype of the constructor. Though there may be no good reason for
using a contractor to create the object at all as you are only going to
be creating one instance of the object, and so again a literal could be
used instead.
Mmm
> }

return instance;
}
}
})( );

The result of those changes could look like:-
let's look at
var singletonClass = (function( ){
// Private variable
var instance;
return ({
constructor:nul l,
getInstance:fun ction( ){
return (
instance||
(
instance = {
constructor:nul l
}
)
);
}
});
})();
Ok, it's no much different of mine, you have converted my code with object
literal
- though that is almost certainly less than would be needed in any real
context, but you will always suffer that if you don't define what it is
exactly that you are trying to achieve.
see before

THKS
Jun 27 '08 #8
On May 23, 7:22 am, Ugo <priv...@nospam .itwrote:
how do you make a singleton access class?
How do you define a "singleton access class"?

i wanted a function/object which restricts the instantiation of one my
object one time and which it ables one global access
Do you know a better way of this one:
The best way of doing something depends at least in part on what it is
you are trying to do.

A generic way to create a singleton object

I doubt that is what Richard meant by "what it is you are trying to
do". You are more likely trying to "implement a tabbed pane widget" or
"send user data to the server". Unless this is just an academic
exercise.

A generic way to create a singleton object with global access is to
use a object literal.

var someSingleton = {
someProperty: function() {},
someOtherProper ty: 55
};

Peter
Jun 27 '08 #9
Ugo wrote:
IMHO, (new myClass()) can not be null|false|unde fined..
Yes, it can ;-)

function myClass()
{
this.toString = function() {
var a = [null, false, a];
return a[Math.floor(Math .random() * a.length)];
};
}

window.alert(ne w myClass());
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
12473
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
10
2645
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
1
2449
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h template <class T> class Singleton : public T { public: static T * Instance();
3
2489
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
7
3282
by: Ethan | last post by:
Hi, I have a class defined as a "Singleton" (Design Pattern). The codes are attached below. My questions are: 1. Does it has mem leak? If no, when did the destructor called? If yes, how can I avoid it? Purify does not show it has mem leak. // test if singleto class has mem leakage #include <iostream>
3
3921
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
5308
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++ Design" or similar books, but I feel there are full of clever guys here who could help me out.
6
2272
by: Manuel | last post by:
Consider the classic singleton (from Thinking in C++): ----------------------------------------------------- //: C10:SingletonPattern.cpp #include <iostream> using namespace std; class Singleton { static Singleton s; int i; Singleton(int x) : i(x) { }
3
18242
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
3
1799
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design constraints/environment are as follows: 1) Everything is single-threaded during static initialization (as in prior to the open brace of main) 2) The environment may be multi-threaded during nominal program execution (within {} of main) 3) I...
0
8630
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9055
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8944
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8899
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4638
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3075
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.