Open main menu

CDOT Wiki β

Changes

OpenOffice temporary template

172 bytes added, 17:47, 20 June 2010
Singletons
class ProgramConfiguration{
public ProgramConfiguraiton(){
//default constructor code
}
}
class ProgramConfiguration{
private ProgramConfiguration(){
//default private constructor code
}
}
class ProgramConfiguration{
private static ProgramConfiguration _configObject;
private ProgramConfiguration(){
//default private constructor code
}
public getInstance(){
/*
if an instance exist return that instance otherwise
call the constructor to create an instance and return it.
*/
if(_configObject == null){
_configObject = ProgramConfiguration();
}
return _configObject;
}
}
main(){
/*
no access to default constructor. so if you did
ProgramConfiguration pc = new ProgramConfiguration();
you will get compilation error.
*/
ProgramConfiguration pc = ProgramConfiguration.getInstance();
ProgramConfiguration newpc = ProgramConfiguration.getInstance();
/*
in the above code pc and newpc both point to the same static object. when
getinstance() is called for the second time, it finds that _configObject is not null
anymore, so it doesn't call the constructor to create any new instance.
*/
}
1
edit