Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: David L.   on Nov 29 In Java Category.

  
Question Answered By: Debbie Reyes   on Nov 29

The singleton  is use to asegurate that only one instance of a class  is
instantiated
Why ? Because is esential not have two or because there arent reason
for have two for example Factories, Loggers , ....

Example:
****************************************************
public class Registry {
//----------------------------------------------------------------------------
private Registry(){}
//----------------------------------------------------------------------------
private static Registry registry;
public static Registry getInstance(){
if (Registry.registry==null) Registry.registry = new Registry();
return Registry.registry;
}

public String doSomething(){.....}
// methods publics....
}

****************************************************
public class UseSingleton{
public UseSingleton(){
Registry.getInstance().doSomething();
}

}

Share: 

 

This Question has 2 more answer(s). View Complete Question Thread

 


Tagged: