Created
December 10, 2019 08:39
-
-
Save ayushi24041992/ed098efcf686df31367665e39699bc7f to your computer and use it in GitHub Desktop.
This file contains 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 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