C++ Programming - OOPS Concepts - Discussion
Discussion Forum : OOPS Concepts - General Questions (Q.No. 1)
1.
Which of the following type of class allows only one object of it to be created?
Discussion:
104 comments Page 1 of 11.
Yogesh Dahake said:
5 years ago
Singleton design pattern is a software design principle that is used to restrict the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. For example, if you are using a logger, that writes logs to a file, you can use a singleton class to create such a logger. You can create a singleton class using the following code.
Example
#include <iostream>
using namespace std;
class Singleton
{
static Singleton *instance;
int data;
// Private constructor so that no objects can be created.
Singleton()
{
data = 0;
}
public:
static Singleton *getInstance()
{
if (!instance)
instance = new Singleton;
return instance;
}
int getData()
{
return this -> data;
}
void setData(int data)
{
this -> data = data;
}
};
Initialize pointer to zero so that it can be initialized in the first call to getInstance.
Singleton *Singleton::instance = 0;
int main(){
Singleton *s = s->getInstance();
cout << s->getData() << endl;
s->setData(100);
cout << s->getData() << endl;
return 0;
}
output
This will give the output:
0
100
Example
#include <iostream>
using namespace std;
class Singleton
{
static Singleton *instance;
int data;
// Private constructor so that no objects can be created.
Singleton()
{
data = 0;
}
public:
static Singleton *getInstance()
{
if (!instance)
instance = new Singleton;
return instance;
}
int getData()
{
return this -> data;
}
void setData(int data)
{
this -> data = data;
}
};
Initialize pointer to zero so that it can be initialized in the first call to getInstance.
Singleton *Singleton::instance = 0;
int main(){
Singleton *s = s->getInstance();
cout << s->getData() << endl;
s->setData(100);
cout << s->getData() << endl;
return 0;
}
output
This will give the output:
0
100
(17)
LoveToProgram said:
1 decade ago
A Singleton class is a design pattern, which creates only a single instance at any time.
When is it required:
Based on the requirement.
Example : Singletons are often used to control access to resources such as database connections or sockets.
Suppose u have licence for only one connection for database, A singleton connection obj makes sure that only one connection can be made at any time.
How this can be implemented:
1. Have a static method, creates the instance if the instance does not exists. If the instance already there then returns the reference to the existing obj.
2. Have a private constructor.
Why Static method:
No obj is required to call static method and memory is used only when required.
private constructor is to make sure that object is not created in any other way.
I explained as per my knowledge. Hope this would be useful.
When is it required:
Based on the requirement.
Example : Singletons are often used to control access to resources such as database connections or sockets.
Suppose u have licence for only one connection for database, A singleton connection obj makes sure that only one connection can be made at any time.
How this can be implemented:
1. Have a static method, creates the instance if the instance does not exists. If the instance already there then returns the reference to the existing obj.
2. Have a private constructor.
Why Static method:
No obj is required to call static method and memory is used only when required.
private constructor is to make sure that object is not created in any other way.
I explained as per my knowledge. Hope this would be useful.
Rajeesh said:
1 decade ago
Singleton class is useful in real time application. Take the example of a real time application of Hospital administration system. Whenever you register in the Hospital database, your object is created and you will be provided with a unique number. This ID is only refers to you created only once in lifetime. Just imagine that you are going to a Hospital first time and to see a Physician. Then you are registering your name in the database only once. Next time, if you plan to visit a Cardiologist in the same hospital. Do you think that your ID needs to be created again? Not all right. Because you have already registered before there is no need to create another object again.
Here comes the singleton class concept. We can create only instance that serves the purpose in every time.
Hope you understand the concept well.
Here comes the singleton class concept. We can create only instance that serves the purpose in every time.
Hope you understand the concept well.
(1)
Rahul Thete said:
9 years ago
The Singleton's purpose is to control object creation, limiting the number of obejcts to one only. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources such as database connections or sockets.
For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multithreading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time.
For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multithreading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time.
Vedanti said:
9 years ago
@Kalaivani.
Singleton class is one whose only one instance (object) can be created. The object is declared globally so that we can use it anywhere in the program.
But this should be avoided as per the fundamental principles like encapsulation or data hiding. Singleton class can also be created by using all static methods which require no instance to call a method.
Eg:-
In the case of an atm machine when we insert our card, an instance is created for our account and all the transactions are created using that single instance of that card.
Singleton class is one whose only one instance (object) can be created. The object is declared globally so that we can use it anywhere in the program.
But this should be avoided as per the fundamental principles like encapsulation or data hiding. Singleton class can also be created by using all static methods which require no instance to call a method.
Eg:-
In the case of an atm machine when we insert our card, an instance is created for our account and all the transactions are created using that single instance of that card.
Deep said:
1 decade ago
A class whose number of instances that can be instantiated is limited to one is called a singleton class. Thus, at any given time only one instance can exist, no more.
The singleton design pattern is used whenever the design requires only one instance of a class. Some examples:.
Application classes. There should only be one application class. (Note: Please bear in mind, MFC class 'CWinApp' is not implemented as a singleton class).
Logger classes. For logging purposes of an application there is usually one logger instance required.
The singleton design pattern is used whenever the design requires only one instance of a class. Some examples:.
Application classes. There should only be one application class. (Note: Please bear in mind, MFC class 'CWinApp' is not implemented as a singleton class).
Logger classes. For logging purposes of an application there is usually one logger instance required.
Aparna said:
10 years ago
Singleton set is used for only one instance of the class.
Singleton means only one instance or object at a time can be created.
Singleton set example:
public class singleton();
{
private static singleton instance;
private singleton()
{
}
Public static singleton instance (this is one and only one way to obtain any instance of the singleton set).
{
get
{
if(instance==null)
{
instanece = new singleton(); (memory will be allocated only on first call to the 'instance' property)
}
return instance;
}
}
Singleton means only one instance or object at a time can be created.
Singleton set example:
public class singleton();
{
private static singleton instance;
private singleton()
{
}
Public static singleton instance (this is one and only one way to obtain any instance of the singleton set).
{
get
{
if(instance==null)
{
instanece = new singleton(); (memory will be allocated only on first call to the 'instance' property)
}
return instance;
}
}
M.SIva Mounika said:
9 years ago
Singleton class is a designed pattern that restricts the instantiation of a class to one object.
Ex: By giving double click on the Application of any video player (Windows,VLC...etc) it will open the Application. if we try to open new application by minimizing previous one, it will display minimized one only, it wont create another new application. Because, It's restricted to the instantiation of a class to only one object.
Ex: By giving double click on the Application of any video player (Windows,VLC...etc) it will open the Application. if we try to open new application by minimizing previous one, it will display minimized one only, it wont create another new application. Because, It's restricted to the instantiation of a class to only one object.
(2)
BinaryWorld-PowerofEverything said:
1 decade ago
Everyone just wants to understand that if the Singleton class is a concept like 'virtual class' - for e.g. then how you will implement that singleton class?
- Any keyword is used?
- Any methods declaration inside that class will tell us that it is a singleton one? (e.g. Static methods).
- When the object of that class is created, will the compiler warn us not to do so?
- And some other questions.
- Any keyword is used?
- Any methods declaration inside that class will tell us that it is a singleton one? (e.g. Static methods).
- When the object of that class is created, will the compiler warn us not to do so?
- And some other questions.
Animesh priyadarshi said:
1 decade ago
In object-oriented programming , a singleton class is a class that can have only one object (an instance of the class) at a time. For example, using Visual C++ , the "Application" class is an example of a singleton class. You can only create only one object of an Application class.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers