Add java documentation #324

This commit is contained in:
Joseph McCarthy 2015-12-28 19:23:00 +00:00
parent 6d516d5124
commit c842f88eb7
7 changed files with 82 additions and 1 deletions

View File

@ -1,13 +1,33 @@
package com.iluwatar.delegation.simple;
public abstract class AbstractPrinterController<T extends Printer> implements Printer{
/**
* Extra layer of abstraction for the controller to allow the controller in this example {@link PrinterController} to
* be as clean as possible. This just provides the default constructor and a simple getter method. The generic of T allows
* any implementation of {@link Printer}
*
* @param <T> Printer
* @see Printer
* @see PrinterController
*/
public abstract class AbstractPrinterController<T extends Printer> implements Printer {
private T printer;
/**
* @param printer instance of T {@link Printer} this instance is the delegate
*/
public AbstractPrinterController(T printer) {
this.printer = printer;
}
/**
* Helper method to return the current instance of T {@link Printer} in order for
* the controller to call operations on the {@link Printer}
*
* @return instance of Printer
* @see Printer
*/
protected T getPrinter() {
return printer;
}

View File

@ -4,10 +4,22 @@ import com.iluwatar.delegation.simple.printers.CanonPrinter;
import com.iluwatar.delegation.simple.printers.EpsonPrinter;
import com.iluwatar.delegation.simple.printers.HPPrinter;
/**
* In this example the delegates are {@link EpsonPrinter}, {@link HPPrinter} and {@link CanonPrinter} they all implement
* {@link Printer}. The {@link AbstractPrinterController} and through inheritance {@link PrinterController} also implement
* {@link Printer}. However neither provide the functionality of {@link Printer} by printing to the screen, they actually
* call upon the instance of {@link Printer} that they were instantiated with. Therefore delegating the behaviour to
* another class.
*/
public class App {
public static final String MESSAGE_TO_PRINT = "hello world";
/**
* Program entry point
*
* @param args command line args
*/
public static void main(String[] args) {
AbstractPrinterController hpPrinterController = new PrinterController(new HPPrinter());
AbstractPrinterController canonPrinterController = new PrinterController(new CanonPrinter());

View File

@ -1,6 +1,20 @@
package com.iluwatar.delegation.simple;
/**
* Interface that both the Controller and the Delegate will implement.
*
* @see com.iluwatar.delegation.simple.printers.CanonPrinter
* @see com.iluwatar.delegation.simple.printers.EpsonPrinter
* @see com.iluwatar.delegation.simple.printers.HPPrinter
* @see AbstractPrinterController
*/
public interface Printer {
/**
* Method that takes a String to print to the screen. This will be implemented on both the
* controller and the delegate allowing the controller to call the same method on the delegate class.
*
* @param message to be printed to the screen
*/
void print(final String message);
}

View File

@ -6,6 +6,14 @@ public class PrinterController extends AbstractPrinterController {
super(printer);
}
/**
* This method is implemented from {@link Printer} however instead on providing an
* implementation, it instead calls upon the class passed through the constructor. This is the delegate,
* hence the pattern. Therefore meaning that the caller does not care of the implementing class only the owning
* controller.
*
* @param message to be printed to the screen
*/
@Override
public void print(String message) {
getPrinter().print(message);

View File

@ -2,8 +2,17 @@ package com.iluwatar.delegation.simple.printers;
import com.iluwatar.delegation.simple.Printer;
/**
* Specialised Implementation of {@link Printer} for a Canon Printer, in
* this case the message to be printed is appended to "Canon Printer : "
*
* @see Printer
*/
public class CanonPrinter implements Printer {
/**
* {@inheritDoc}
*/
@Override
public void print(String message) {
System.out.println("Canon Printer : " + message);

View File

@ -2,8 +2,17 @@ package com.iluwatar.delegation.simple.printers;
import com.iluwatar.delegation.simple.Printer;
/**
* Specialised Implementation of {@link Printer} for a Epson Printer, in
* this case the message to be printed is appended to "Epson Printer : "
*
* @see Printer
*/
public class EpsonPrinter implements Printer{
/**
* {@inheritDoc}
*/
@Override
public void print(String message) {
System.out.println("Epson Printer : " + message);

View File

@ -2,8 +2,17 @@ package com.iluwatar.delegation.simple.printers;
import com.iluwatar.delegation.simple.Printer;
/**
* Specialised Implementation of {@link Printer} for a HP Printer, in
* this case the message to be printed is appended to "HP Printer : "
*
* @see Printer
*/
public class HPPrinter implements Printer {
/**
* {@inheritDoc}
*/
@Override
public void print(String message) {
System.out.println("HP Printer : " + message);