File tree Expand file tree Collapse file tree
delegation/src/main/java/com/iluwatar/delegation/simple Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package com .iluwatar .delegation .simple ;
22
3- public abstract class AbstractPrinterController <T extends Printer > implements Printer {
3+ /**
4+ * Extra layer of abstraction for the controller to allow the controller in this example {@link PrinterController} to
5+ * be as clean as possible. This just provides the default constructor and a simple getter method. The generic of T allows
6+ * any implementation of {@link Printer}
7+ *
8+ * @param <T> Printer
9+ * @see Printer
10+ * @see PrinterController
11+ */
12+ public abstract class AbstractPrinterController <T extends Printer > implements Printer {
13+
414
515 private T printer ;
616
17+ /**
18+ * @param printer instance of T {@link Printer} this instance is the delegate
19+ */
720 public AbstractPrinterController (T printer ) {
821 this .printer = printer ;
922 }
1023
24+ /**
25+ * Helper method to return the current instance of T {@link Printer} in order for
26+ * the controller to call operations on the {@link Printer}
27+ *
28+ * @return instance of Printer
29+ * @see Printer
30+ */
1131 protected T getPrinter () {
1232 return printer ;
1333 }
Original file line number Diff line number Diff line change 44import com .iluwatar .delegation .simple .printers .EpsonPrinter ;
55import com .iluwatar .delegation .simple .printers .HPPrinter ;
66
7+ /**
8+ * In this example the delegates are {@link EpsonPrinter}, {@link HPPrinter} and {@link CanonPrinter} they all implement
9+ * {@link Printer}. The {@link AbstractPrinterController} and through inheritance {@link PrinterController} also implement
10+ * {@link Printer}. However neither provide the functionality of {@link Printer} by printing to the screen, they actually
11+ * call upon the instance of {@link Printer} that they were instantiated with. Therefore delegating the behaviour to
12+ * another class.
13+ */
714public class App {
815
916 public static final String MESSAGE_TO_PRINT = "hello world" ;
1017
18+ /**
19+ * Program entry point
20+ *
21+ * @param args command line args
22+ */
1123 public static void main (String [] args ) {
1224 AbstractPrinterController hpPrinterController = new PrinterController (new HPPrinter ());
1325 AbstractPrinterController canonPrinterController = new PrinterController (new CanonPrinter ());
Original file line number Diff line number Diff line change 11package com .iluwatar .delegation .simple ;
22
3+ /**
4+ * Interface that both the Controller and the Delegate will implement.
5+ *
6+ * @see com.iluwatar.delegation.simple.printers.CanonPrinter
7+ * @see com.iluwatar.delegation.simple.printers.EpsonPrinter
8+ * @see com.iluwatar.delegation.simple.printers.HPPrinter
9+ * @see AbstractPrinterController
10+ */
311public interface Printer {
412
13+ /**
14+ * Method that takes a String to print to the screen. This will be implemented on both the
15+ * controller and the delegate allowing the controller to call the same method on the delegate class.
16+ *
17+ * @param message to be printed to the screen
18+ */
519 void print (final String message );
620}
Original file line number Diff line number Diff line change @@ -6,6 +6,14 @@ public PrinterController(Printer printer) {
66 super (printer );
77 }
88
9+ /**
10+ * This method is implemented from {@link Printer} however instead on providing an
11+ * implementation, it instead calls upon the class passed through the constructor. This is the delegate,
12+ * hence the pattern. Therefore meaning that the caller does not care of the implementing class only the owning
13+ * controller.
14+ *
15+ * @param message to be printed to the screen
16+ */
917 @ Override
1018 public void print (String message ) {
1119 getPrinter ().print (message );
Original file line number Diff line number Diff line change 22
33import com .iluwatar .delegation .simple .Printer ;
44
5+ /**
6+ * Specialised Implementation of {@link Printer} for a Canon Printer, in
7+ * this case the message to be printed is appended to "Canon Printer : "
8+ *
9+ * @see Printer
10+ */
511public class CanonPrinter implements Printer {
612
13+ /**
14+ * {@inheritDoc}
15+ */
716 @ Override
817 public void print (String message ) {
918 System .out .println ("Canon Printer : " + message );
Original file line number Diff line number Diff line change 22
33import com .iluwatar .delegation .simple .Printer ;
44
5+ /**
6+ * Specialised Implementation of {@link Printer} for a Epson Printer, in
7+ * this case the message to be printed is appended to "Epson Printer : "
8+ *
9+ * @see Printer
10+ */
511public class EpsonPrinter implements Printer {
612
13+ /**
14+ * {@inheritDoc}
15+ */
716 @ Override
817 public void print (String message ) {
918 System .out .println ("Epson Printer : " + message );
Original file line number Diff line number Diff line change 22
33import com .iluwatar .delegation .simple .Printer ;
44
5+ /**
6+ * Specialised Implementation of {@link Printer} for a HP Printer, in
7+ * this case the message to be printed is appended to "HP Printer : "
8+ *
9+ * @see Printer
10+ */
511public class HPPrinter implements Printer {
612
13+ /**
14+ * {@inheritDoc}
15+ */
716 @ Override
817 public void print (String message ) {
918 System .out .println ("HP Printer : " + message );
You can’t perform that action at this time.
0 commit comments