Click here to Skip to main content
15,911,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to save data in database in that i want to implement using Singleton pattern with base class can any body know please help me
Posted
Comments
Albin Abel 14-Mar-11 6:14am    
I hope you question is not "How to create a singleton class". Then explain how you want to use the singleton class. Without knowing this how we can answer?. Also have you tried anything
Rupa1 14-Mar-11 6:15am    
yes i don't know please tell me sir
pankajupadhyay29 14-Mar-11 6:21am    
what you don't know? To Create singleton class? else specify your scenario.
Rupa1 14-Mar-11 6:22am    
because i searched in Google but i am not able to understand........

In a simple way singleton object is the one which can have only one instance of it in the memory.

Look at the example below

C#
 public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    private static Person person;
    private Person()
    {
    }
    public static Person GetPerson()
    {
        if (person == null)
        {
            person = new Person();
        }
        return person;
    }
}


This Person class you can't instantiate in the normal way like Person person=new Person() because of the private constructor. But you need to access it like Person person=Person.GetPerson() which always return the same instance till the static Person object in the memory.



It is the simplest form. Now how you want to use this pattern?.
 
Share this answer
 
Comments
Manas Bhardwaj 14-Mar-11 12:14pm    
+5...nicely explained.
Rupa1 15-Mar-11 2:05am    
thx sir
When you define a class, it means you can create a large number of its object at runtime. It is because of public constructor.
class Box {
public Box()
{
}
...
...
...
}
Box b1 = new Box();
Box b2 = new Box();
..
// so on
Box b100 = new Box();

but if you apply singleton pattern. it means you can create ONLY ONE object with it. In this pattern constructor is private it means you can't use like
Box b1 = new Box();
Box b2 = new Box(); // invalid ..

So singleton pattern is "Defining a Class with private constructor so that the code which is outside the class cannot create it".
following code creates a singleton class.
class Box{	
    private static Box instance ;	
    private Box()	
      {		...	
      }	
 public static Box getInstance()	
      {		
         if (instance == null)
 	     instance = new Singleton();
         return instance;	
      }		
 public void doSomething()	
     {		...		
     }
}

With the above defination you can access the object of class Box by
Box b = Box.getInstance();
b.doSomething();


But it is important to understand that where do we use singleton and we we not.
If we want a single gateway to common set of functionalities we use singleton class. like Logging, Configuration of Application, Server Client achitecture, Resource access, Global/shared values/variables ( company email, address etc.). common Data store acess point. If wisely used it saves lots of memory and increase performance.

How to determine the singleton behaviour of a Class.

In the simplest form,

If a entity in your application co-exists with different property values then it is not a candidate for singleton. for e.g. In school System , "Student" : Student-1 , Student-2 has a common property / behaviour but property values are different. (their name,age,address class are different).

If a Entity exists in System that is common and its property values are same,its a candidate for Singleton class. for e.g. Playground, Auditorium, School Notice Board. these are single instance object throughout the School system.


A in-depth analysis is required to get a list of classes that we can make singleton.

please refer http://www.oodesign.com/singleton-pattern.html[^] for more detail
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900