473,378 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Object Creation

SK
i have a class A and I have a singleton class B.
i am accessing the object of B from A and is using B's
method from most of the methods of A.
Is it good practice to create the singleton Bs instance in
the constructor of A or in some methods of A?

Jul 21 '05 #1
3 1274
pm
I have been not doing this in the methods themselves but will be interested
in what others have to say.

I worry about doing it in constructors for maintenance reasons.

"SK" wrote:
i have a class A and I have a singleton class B.
i am accessing the object of B from A and is using B's
method from most of the methods of A.
Is it good practice to create the singleton Bs instance in
the constructor of A or in some methods of A?

Jul 21 '05 #2
It seems to me that if B is a singleton, then the logic for creating and
making sure there is only one instance belongs to the singleton. If you
push that logic to A, you could create more problems. For example, what
happens if A inadvertently creates more than one instance of the singleton?
Anyways, the design pattern from "Design Patterns" by the Gang of Four
describe a structure for a singleton class. In C# it would look like:

public class Singleton
{
private static Singleton _instance;

protected Singleton() { }

public static Singleton Instance()
{
if (_instance == null)
_instance = new Singleton();

return _instance;
}
}

Hope this helps.

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

"pm" <pm@discussions.microsoft.com> wrote in message
news:F0**********************************@microsof t.com...
I have been not doing this in the methods themselves but will be interested in what others have to say.

I worry about doing it in constructors for maintenance reasons.

"SK" wrote:
i have a class A and I have a singleton class B.
i am accessing the object of B from A and is using B's
method from most of the methods of A.
Is it good practice to create the singleton Bs instance in
the constructor of A or in some methods of A?

Jul 21 '05 #3
The instance creation should be invisible to the client of the singleton
class. i.e the class that is consuming the singleton should not be aware
if it is actually creating the first instance or using an already
exisitng instance.

http://msdn.microsoft.com/library/de...tondespatt.asp

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

SK wrote:
i have a class A and I have a singleton class B.
i am accessing the object of B from A and is using B's
method from most of the methods of A.
Is it good practice to create the singleton Bs instance in
the constructor of A or in some methods of A?

Jul 21 '05 #4

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

Similar topics

7
by: Richard | last post by:
Hi all, I am looking for some help on understanding the overhead associated with object creation in Java. I am writing an application where I have written a class to encapsulate some text...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
3
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
8
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
3
by: Nick Dreyer | last post by:
I was quite surprised to notice that Sub New() gets called twice, once at declaration time and once at creation time. I can't figure out why it would be called at declaration if there is no class...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
by: Dhananjay | last post by:
Hi All, I want to develop one application in vb.net for exchange 2000. I tried to add one contact with the code snippet below. The same logic is there for appointment on Microsoft's site. (I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.