Skip to content

Instantly share code, notes, and snippets.

@IvoIvanov77
Created August 27, 2018 09:44
Show Gist options
  • Save IvoIvanov77/84e7d3c9c6f9bfc74fef7a58aed79bf1 to your computer and use it in GitHub Desktop.
Save IvoIvanov77/84e7d3c9c6f9bfc74fef7a58aed79bf1 to your computer and use it in GitHub Desktop.
public class Main {
public static void main(String[] args) {
PrinterController.connectToPrinter(PrinterName.PRINTER_A).turnOn();
PrinterController.connectToPrinter(PrinterName.PRINTER_B).print();
}
}
public interface Printer {
void turnOn();
void turnOf();
void print();
}
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
}
}
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");
}
}
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
}
}
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();
}
}
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