Created
August 27, 2018 09:44
-
-
Save IvoIvanov77/84e7d3c9c6f9bfc74fef7a58aed79bf1 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
public class Main { | |
public static void main(String[] args) { | |
PrinterController.connectToPrinter(PrinterName.PRINTER_A).turnOn(); | |
PrinterController.connectToPrinter(PrinterName.PRINTER_B).print(); | |
} | |
} |
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
public interface Printer { | |
void turnOn(); | |
void turnOf(); | |
void print(); | |
} |
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
public class PrinterA implements Printer { | |
@Override | |
public void turnOn() { | |
System.out.println("printer a turned on"); | |
} | |
@Override | |
public void turnOf() { | |
// TODO: implement me | |
} | |
@Override | |
public void print() { | |
// TODO: implement me | |
} | |
} |
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
public class PrinterB implements Printer { | |
@Override | |
public void turnOn() { | |
// TODO: implement me | |
} | |
@Override | |
public void turnOf() { | |
// TODO: implement me | |
} | |
@Override | |
public void print() { | |
System.out.println("Printer A printing"); | |
} | |
} |
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
public class PrinterC implements Printer { | |
@Override | |
public void turnOn() { | |
// TODO: implement me | |
} | |
@Override | |
public void turnOf() { | |
// TODO: implement me | |
} | |
@Override | |
public void print() { | |
// TODO: implement me | |
} | |
} |
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
public class PrinterController { | |
private PrinterController() { | |
} | |
public static Printer connectToPrinter(PrinterName printerName){ | |
if(printerName.equals(PrinterName.PRINTER_A)){ | |
return new PrinterA(); | |
} | |
if(printerName.equals(PrinterName.PRINTER_B)){ | |
return new PrinterB(); | |
} | |
return new PrinterC(); | |
} | |
} |
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
public enum PrinterName { | |
PRINTER_A, | |
PRINTER_B, | |
PRINTER_C | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment