Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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