Added skeleton code for delegation pattern #324

This commit is contained in:
Joseph McCarthy
2015-12-26 22:20:53 +00:00
parent 30363cbb7f
commit bdacfe30c1
7 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package com.iluwatar.delegation.simple;
public abstract class Controller<T extends Printer> {
private Printer printer;
public Controller(Printer printer) {
this.printer = printer;
}
protected Printer getPrinter() {
return printer;
}
}

View File

@ -0,0 +1,6 @@
package com.iluwatar.delegation.simple;
public interface Printer {
void print(final String message);
}

View File

@ -0,0 +1,13 @@
package com.iluwatar.delegation.simple;
public class PrinterController extends Controller implements Printer {
public PrinterController(Printer printer) {
super(printer);
}
@Override
public void print(String message) {
getPrinter().print(message);
}
}

View File

@ -0,0 +1,12 @@
package com.iluwatar.delegation.simple.printers;
import com.iluwatar.delegation.simple.Printer;
public class CanonPrinter implements Printer {
@Override
public void print(String message) {
System.out.println("Canon Printer : " + message);
}
}

View File

@ -0,0 +1,12 @@
package com.iluwatar.delegation.simple.printers;
import com.iluwatar.delegation.simple.Printer;
public class EpsonPrinter implements Printer{
@Override
public void print(String message) {
System.out.println("Epson Printer : " + message);
}
}

View File

@ -0,0 +1,12 @@
package com.iluwatar.delegation.simple.printers;
import com.iluwatar.delegation.simple.Printer;
public class HPPrinter implements Printer {
@Override
public void print(String message) {
System.out.println("HP Printer : " + message);
}
}

View File

@ -0,0 +1,24 @@
package com.iluwatar.delegation.simple;
import com.iluwatar.delegation.simple.printers.CanonPrinter;
import com.iluwatar.delegation.simple.printers.EpsonPrinter;
import com.iluwatar.delegation.simple.printers.HPPrinter;
public class AppTest {
public static final String MESSAGE_TO_PRINT = "hello world";
public static void main(String args[]) {
Printer hpPrinter = new HPPrinter();
Printer canonPrinter = new CanonPrinter();
Printer epsonPrinter = new EpsonPrinter();
PrinterController hpPrinterController = new PrinterController(hpPrinter);
PrinterController canonPrinterController = new PrinterController(canonPrinter);
PrinterController epsonPrinterController = new PrinterController(epsonPrinter);
hpPrinterController.print(MESSAGE_TO_PRINT);
canonPrinterController.print(MESSAGE_TO_PRINT);
epsonPrinterController.print(MESSAGE_TO_PRINT);
}
}