SINGLETON PATTERN EXAMPLE IN JAVA (Static Class Approache)

 /**  
 Singleton Pattern: One instance of a class or one value accessible globally in an application.   
 There are some instances in the application where we have to use just one instance.   
 In this Example:   
 We use Static class to control the instance of class.   
 */  
 final class Logger {  
 //a static class implementation of Singleton pattern  
 static public void logMessage(String s) {  
 System.out.println(s);  
 }  
 }// End of class  
 //==============================  
 public class StaticLogger {  
 public static void main(String args[]) {  
 Logger.logMessage("This is SINGLETON");  
 }  
 }// End of class  
 /**The advantage of this static approach is that it’s easier to use. The disadvantage   
 of course is that if in future you do not want the class to be static anymore, you will  
 have to do a lot of recoding.*/  

Post a Comment

0 Comments