Skip to content

Instantly share code, notes, and snippets.

@ayushi24041992
Created December 10, 2019 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayushi24041992/ed098efcf686df31367665e39699bc7f to your computer and use it in GitHub Desktop.
Save ayushi24041992/ed098efcf686df31367665e39699bc7f to your computer and use it in GitHub Desktop.
package Singleton;
public class Printer {
// Private - It should be accessed by only getInstance Methods()
private static Printer instance;
private Printer() {
// Private Constructor to make sure that no more than one instance is created at a given time.
}
// Double check Locking
public static Printer getInstance() {
if (instance == null) {
// This block creates instance so that min threads wait and only for first time.
synchronized (Printer.class) {
if (instance == null) {
instance = new Printer();
}
}
}
return instance;
}
void print(Object obj) {
if (obj instanceof MSWord) {
MSWord wordInstance = (MSWord) obj;
System.out.println("I am Printing " + wordInstance.page);
} else {
AcrobatReader pdfInstance = (AcrobatReader) obj;
System.out.println("I am Printing " + pdfInstance.page);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment