|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Class based on singleton pattern and instance methods in that clasHello 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 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? 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. Show quote "KJ" wrote: > 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? > > 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*. 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) Show quote "Diffident" <Diffid***@discussions.microsoft.com> wrote in message news:7D4E6395-DD9B-4982-B7E1-84B186CC35BC@microsoft.com... > 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. > > "KJ" wrote: > >> 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? >> >> 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 -- Show quoteCo-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com "Diffident" wrote: > 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 |
|||||||||||||||||||||||