Class based on singleton pattern and instance methods in that clas

G

Guest

Hello All,

I am designing a class based on singleton pattern. Inside this class I have
multiple instance methods. My question is since there will be only one
instance of this class at any instance of time in the whole application there
is no use in having these methods as instance methods I can as well have
static methods....correct?

If I leave these methods as instance methods would they have any wrong
impact on my application?

Thank you for your clarification
 
K

KJ

If you mark the methods public static, than anyone can call them
without an instance, and then what good is the Singleton pattern? Am I
missing something here?
 
G

Guest

My question is if a class is marked as singleton how will the instance
methods in that class work?

Is it a good practice to have instance methods inside a singleton class?
Whats the use of having singleton class with instance methods if you are
going to have only one instance of the class?

Did I make sense?

Thank you.
 
K

KJ

The instance methods in the class will be called on the single instance
that is created, right? So if the methods are static, then you don't
really need an instance, and it defeats the purpose of using the
singleton pattern. Therefore, you MUST have *instance* methods, because
the point of singleton is single-*instance*.
 
G

Guest

Diffident,
you may be getting confused about what a class in Singleton pattern does -
it DOES return an instance, it's just that you can only ever have ONE
instance. So, instance methods make perfect sense.
Does this help?
Peter
 
B

Bruce Barker

you are a little confused. you don't mark a class as singleton, you
implement a class that follows the pattern.the singleton pattern is when a
instance of a class is created/requested, the same instance is always
returned.

static methods are owned by the class and will not have access to instance
members. they can be called without creating an instance of the (singleton)
class.

instance methods belong to the instance, can access instanse mebersm, and
can only be called from an instance reference.

note: a common singleton practice is to have static methods that create an
instance, then call the instance methods.

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top