TEJAS J GHALSASI
TEIT
SINGLETON PATTERN IN JAVA
*/
import java.util.*;
public class NoticeBoard {
private static NoticeBoard a;
String message;
/**
* Create private constructor
*/
private NoticeBoard(){
}
/**
* Create a static method to get instance.
*/
public static NoticeBoard getInstance(){
if(a == null){
a = new NoticeBoard();
}
return a;
}
public void getMessage(String msg){
message=msg;
System.out.println("message updated \n");
}
public void postMessage(){
System.out.println("Current message is: "+message);
}
public static void main(String a[]){
Scanner sc=new Scanner(System.in);
NoticeBoard nb = NoticeBoard.getInstance();
NoticeBoard nb1 = NoticeBoard.getInstance();
System.out.println("enter message");
String msg=sc.nextLine();
nb.getMessage(msg);
System.out.println("message stored in nb");
nb.postMessage();
System.out.println("message stored in nb1");
nb1.postMessage();
System.out.println("enter a new message ");
msg=sc.nextLine();
nb.getMessage(msg);
System.out.println("message stored in nb");
nb.postMessage();
System.out.println("message stored in nb1");
nb1.postMessage();
}
}
/*
THIS PROGRAM IS CODED BY TG
*/
No comments:
Post a Comment