Created
April 28, 2021 11:09
-
-
Save BT-ICD/750a584ac99e709712cfb7360795495a to your computer and use it in GitHub Desktop.
Example to learn about Singleton Pattern in JAVA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package SingletonPatternDemo; | |
/* | |
*Example of Eager Initialization | |
* */ | |
public class SingletonOptionOne { | |
private static final SingletonOptionOne INSTANCE = new SingletonOptionOne(); | |
private SingletonOptionOne(){} | |
private String messageData; | |
/** | |
* To get singleton instance of SingletonOptionOne class | |
* */ | |
public static SingletonOptionOne getInstance(){ | |
return INSTANCE; | |
} | |
public String getMessageData() { | |
return messageData; | |
} | |
public void setMessageData(String messageData) { | |
this.messageData = messageData; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package SingletonPatternDemo; | |
/** | |
* To use object of SingletonOptionOne class - which is singleton | |
* Example to consume single class instance | |
* */ | |
public class SingletonOptionOneDemo { | |
public static void main(String[] args) { | |
SingletonOptionOne obj1 = SingletonOptionOne.getInstance(); | |
obj1.setMessageData("Hello From obj1"); | |
System.out.println(obj1.getMessageData()); | |
SingletonOptionOne obj2 = SingletonOptionOne.getInstance(); | |
obj2.setMessageData("Hello From obj2"); | |
System.out.println(obj2.getMessageData()); | |
System.out.println(obj1.getMessageData()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package SingletonPatternDemo; | |
/** | |
* Learning Example: Singleton pattern - Lazy Initialization Method | |
* */ | |
public class SingletonOptionTwo { | |
private static SingletonOptionTwo INSTANCE =null; | |
private SingletonOptionTwo(){} | |
private String messageData; | |
/** | |
* To get singleton instance of SingletonOptionTwo class | |
* */ | |
public synchronized static SingletonOptionTwo getInstance(){ | |
if(INSTANCE==null){ | |
INSTANCE = new SingletonOptionTwo(); | |
} | |
return INSTANCE; | |
} | |
public String getMessageData() { | |
return messageData; | |
} | |
public void setMessageData(String messageData) { | |
this.messageData = messageData; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package SingletonPatternDemo; | |
public class SingletonOptionTwoDemo { | |
public static void main(String[] args) { | |
SingletonOptionTwo obj1; | |
obj1 = SingletonOptionTwo.getInstance(); | |
obj1.setMessageData("Hello from obj1 "); | |
System.out.println(obj1.getMessageData()); | |
SingletonOptionTwo obj2; | |
obj2 = SingletonOptionTwo.getInstance(); | |
System.out.println(obj2.getMessageData()); | |
obj2.setMessageData("Hello from obj2"); | |
System.out.println(obj1.getMessageData()); | |
System.out.println(obj2.getMessageData()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment