473,807 Members | 2,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

the wise use of singleton

phl
hello,

My project in a web project. I choose to use singleton in one of my
projects. Towards the end I realise that I seemed to have refered to
the fields singleton in my other classes in my business logic a little
precariously. I access my the fields in the singlton in my BLL classes
directly. Is this a really bad violation of OOP rules? It's almost liek
I have use the singlton as one big global variable. Is there any decent
way of using singltons, how do most people use them?

Why I choose singleton to represent some of my data is that, I need to
have say a class that represent user information, that other classes
can access, without having to instaniate a user info class everytime(as
this is a web app in .net, users settings die every post back if you
don't explicitly hold it in memory. I know I could have a base class
with userinfo instead but it would have to be instaniated everytime or
have userobject held in a session variable some where). Using singltons
reminds me of programming in the old days (liek C) where you pass whole
structures to functions, except singltons are lot easier.

So Have I completely abuse the singlton, should I have at least pass
the values of the singlton as my class constructor parameters, instead
of accessing them directly? is there a better way of doing this?
Cheers

Nov 16 '05 #1
7 2833
phl wrote:
hello,

My project in a web project. I choose to use singleton in one of my
projects. Towards the end I realise that I seemed to have refered to
the fields singleton in my other classes in my business logic a little
precariously. I access my the fields in the singlton in my BLL classes
directly. Is this a really bad violation of OOP rules? It's almost liek
I have use the singlton as one big global variable. Is there any decent
way of using singltons, how do most people use them?

Why I choose singleton to represent some of my data is that, I need to
have say a class that represent user information, that other classes
can access, without having to instaniate a user info class everytime(as
this is a web app in .net, users settings die every post back if you
don't explicitly hold it in memory. I know I could have a base class
with userinfo instead but it would have to be instaniated everytime or
have userobject held in a session variable some where). Using singltons
reminds me of programming in the old days (liek C) where you pass whole
structures to functions, except singltons are lot easier.

So Have I completely abuse the singlton, should I have at least pass
the values of the singlton as my class constructor parameters, instead
of accessing them directly? is there a better way of doing this?
Cheers


Most of the oppinons on how you have used your single, have been
discussed in the 'Singletons' thread within this NG.

Nov 16 '05 #2
There are two issues here.

First, should you access fields directly, rather than using properties.
In the .NET world, the answer is "no". You should always wrap your
fields in get/set properties. You can read about why in other threads
in this newsgroup, and on MSDN.

Second, should you use a singleton as a space in which to store global
information, or some other construct? The answer is that you probably
want a singleton. The only alternative I know of is to use a static
class (not a base class as you mentioned), but using a static class
introduces some restrictions: you can never pass the class as an
argument, among other things. Singletons create the equivalent of
static classes that can be treated like normal objects, which all
really depends upon whether you will ever need to treat them like
normal objects. In your case, for storing user information, a static
class might have done just as well, but there's nothing wrong with
using a singleton.

Yes, it does look like good ol' C, but then don't be swept up in the
"everything must be an object" frenzy that new converts to OOP so
frequently contract. There are some things in the OOP world that simply
don't need to be objects.

Nov 16 '05 #3
phl wrote:
hello,

My project in a web project. [snip]
Why I choose singleton to represent some of my data is that, I need to
have say a class that represent user information ...

[snip]

Is this "user information" specific for the user actually accessing
the site or global to all users?
If various users should have their own version of this "user info"
then a singleton is the wrong choice! A web application is a single
application (having just a single singleton) that can be accessed
by multiple users at the same time.

If that information applies to all users, then you can use a singleton.

--
Hans Kesting
Nov 16 '05 #4
phl wrote:
hello,

My project in a web project. I choose to use singleton in one of my
projects. Towards the end I realise that I seemed to have refered to
the fields singleton in my other classes in my business logic a little
precariously. I access my the fields in the singlton in my BLL classes
directly. Is this a really bad violation of OOP rules? It's almost liek
I have use the singlton as one big global variable. Is there any decent
way of using singltons, how do most people use them?

Why I choose singleton to represent some of my data is that, I need to
have say a class that represent user information, that other classes
can access, without having to instaniate a user info class everytime(as
this is a web app in .net, users settings die every post back if you
don't explicitly hold it in memory. I know I could have a base class
with userinfo instead but it would have to be instaniated everytime or
have userobject held in a session variable some where). Using singltons
reminds me of programming in the old days (liek C) where you pass whole
structures to functions, except singltons are lot easier.

So Have I completely abuse the singlton, should I have at least pass
the values of the singlton as my class constructor parameters, instead
of accessing them directly? is there a better way of doing this?
Cheers


Hi, Microsoft offer several techniques in which to maintain state in an
ASP.NET application, here:
http://msdn.microsoft.com/msdnmag/is...e/default.aspx

The cache object is quite handy when used with the callback mechanism to
refresh it.
Nov 16 '05 #5
phl
The user information is specific to each user only. My singleton lasts
for the duration of a particular user session. Can you explain why it's
wrong choice and if so what could I have done to make it better? Should
I have instead created a class which deals with saving a user
information object to the session object instead?
Hans Kesting wrote:
Is this "user information" specific for the user actually accessing
the site or global to all users?
If various users should have their own version of this "user info"
then a singleton is the wrong choice! A web application is a single
application (having just a single singleton) that can be accessed
by multiple users at the same time.

If that information applies to all users, then you can use a singleton.
--
Hans Kesting


Nov 16 '05 #6
Responding to Phl...
My project in a web project. I choose to use singleton in one of my
projects. Towards the end I realise that I seemed to have refered to
the fields singleton in my other classes in my business logic a little
precariously. I access my the fields in the singlton in my BLL classes
directly. Is this a really bad violation of OOP rules? It's almost liek
I have use the singlton as one big global variable. Is there any decent
way of using singltons, how do most people use them?


There are two quite orthogonal issues here: whether to use a Singleton
design pattern and how knowledge is accessed during collaborations.

Use a Singleton pattern when both of the following conditions are true
in the particular problem solution context:

(1) There should never be more than one instance of the class.

(2) An instance can be created in multiple contexts (by different
objects) or the instantiation logic can be invoked multiple times.

IOW, if the normal instantiation of solution objects precludes the
possibility of creating more than one instance, then you don't need to
control instantiation via a Singleton. However, if both conditions are
true than Singleton is one of the cleaner and more generally understood
mechanisms for ensuring only one instance at a time exists.

The Singleton object may have a need to access information in other
objects. Being a Singleton does not affect that. IOW, collaboration is
exactly the same whether there are no other instances or there are a
thousand other instances.

Note that Singleton is about instantiating objects while accessing
information is about collaboration among objects that are already
instantiated. In the OO paradigm one employs relationship navigation
for accessing knowledge or sending behavior messages. During
collaboration there is an implicit assumption that one gets to the right
object via the relationship path. IOW, it is up to whoever instantiates
the relationships in the path to make sure the right objects are
communicating.

Usually (though not always in dynamic situations where relationships may
be conditional) instantiating relationships is the responsibility of
whoever creates one of the participating instances. Thus whoever
creates the Singleton's object will usually be responsible for
instantiating relationships between it an other objects.

[Note that the instance() behavior in Singleton just creates the
instance. The instance itself should have relationships to other
objects once it is created. Those relationships are navigated for
collaboration rather than invoking the static instance() method. Using
the instance() method to access the instance during collaboration is a
very bad practice because it bypasses all the safeguards inherent in the
business rules and policies that limit which particular instances should
be collaborating. That abuse is, indeed, akin to the unlimited accesses
to global variables in the procedural days.]
*************
There is nothing wrong with me that could
not be cured by a capful of Drano.

H. S. Lahman
hs*@pathfinderm da.com
Pathfinder Solutions -- Put MDA to Work
http://www.pathfindermda.com
blog (under constr): http://pathfinderpeople.blogs.com/hslahman
(888)-OOA-PATH

Nov 16 '05 #7

"H. S. Lahman" <h.******@veriz on.net> wrote in message
news:zmfId.1307 4$qu2.1244@trnd ny08...
Use a Singleton pattern when both of the following conditions are true
in the particular problem solution context:

(1) There should never be more than one instance of the class.

(2) An instance can be created in multiple contexts (by different
objects) or the instantiation logic can be invoked multiple times.


Here's another solution (tongue in cheek):

Use a singleton when the cost of creating another instance is higher than
the cost of replacing someone who's been told not to.

--
Michael Feathers
author, Working Effectively with Legacy Code (Prentice Hall 2005)
www.objectmentor.com
Nov 16 '05 #8

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

Similar topics

7
12481
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
2659
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
2454
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
2503
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
3289
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
3930
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
5313
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.
3
18258
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
1808
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
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9599
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
10624
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
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
10374
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
10111
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...
0
6877
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();...
1
4330
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
2
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.