Skip to content

Commit c842f88

Browse files
committed
Add java documentation iluwatar#324
1 parent 6d516d5 commit c842f88

7 files changed

Lines changed: 82 additions & 1 deletion

File tree

delegation/src/main/java/com/iluwatar/delegation/simple/AbstractPrinterController.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
package 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
}

delegation/src/main/java/com/iluwatar/delegation/simple/App.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@
44
import com.iluwatar.delegation.simple.printers.EpsonPrinter;
55
import 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+
*/
714
public 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());
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
package 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+
*/
311
public 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
}

delegation/src/main/java/com/iluwatar/delegation/simple/PrinterController.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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);

delegation/src/main/java/com/iluwatar/delegation/simple/printers/CanonPrinter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
import 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+
*/
511
public class CanonPrinter implements Printer {
612

13+
/**
14+
* {@inheritDoc}
15+
*/
716
@Override
817
public void print(String message) {
918
System.out.println("Canon Printer : " + message);

delegation/src/main/java/com/iluwatar/delegation/simple/printers/EpsonPrinter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
import 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+
*/
511
public class EpsonPrinter implements Printer{
612

13+
/**
14+
* {@inheritDoc}
15+
*/
716
@Override
817
public void print(String message) {
918
System.out.println("Epson Printer : " + message);

delegation/src/main/java/com/iluwatar/delegation/simple/printers/HPPrinter.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
import 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+
*/
511
public class HPPrinter implements Printer {
612

13+
/**
14+
* {@inheritDoc}
15+
*/
716
@Override
817
public void print(String message) {
918
System.out.println("HP Printer : " + message);

0 commit comments

Comments
 (0)