Fix CheckStyle #324
This commit is contained in:
parent
fcadb223ce
commit
0bc722f797
@ -2,8 +2,8 @@ package com.iluwatar.delegation.simple;
|
||||
|
||||
/**
|
||||
* 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}
|
||||
* 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
|
||||
@ -12,23 +12,23 @@ package com.iluwatar.delegation.simple;
|
||||
public abstract class AbstractPrinterController<T extends Printer> implements Printer {
|
||||
|
||||
|
||||
private T printer;
|
||||
private T printer;
|
||||
|
||||
/**
|
||||
* @param printer instance of T {@link Printer} this instance is the delegate
|
||||
*/
|
||||
public AbstractPrinterController(T printer) {
|
||||
this.printer = 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;
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
@ -2,32 +2,32 @@ 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;
|
||||
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.
|
||||
* 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";
|
||||
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());
|
||||
AbstractPrinterController epsonPrinterController = new PrinterController(new EpsonPrinter());
|
||||
/**
|
||||
* 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());
|
||||
AbstractPrinterController epsonPrinterController = new PrinterController(new EpsonPrinter());
|
||||
|
||||
hpPrinterController.print(MESSAGE_TO_PRINT);
|
||||
canonPrinterController.print(MESSAGE_TO_PRINT);
|
||||
epsonPrinterController.print(MESSAGE_TO_PRINT);
|
||||
}
|
||||
hpPrinterController.print(MESSAGE_TO_PRINT);
|
||||
canonPrinterController.print(MESSAGE_TO_PRINT);
|
||||
epsonPrinterController.print(MESSAGE_TO_PRINT);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,20 +1,22 @@
|
||||
package com.iluwatar.delegation.simple;
|
||||
|
||||
import com.iluwatar.delegation.simple.printers.HpPrinter;
|
||||
|
||||
/**
|
||||
* Interface that both the Controller and the Delegate will implement.
|
||||
* 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
|
||||
* @see com.iluwatar.delegation.simple.printers.CanonPrinter
|
||||
* @see com.iluwatar.delegation.simple.printers.EpsonPrinter
|
||||
* @see 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);
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
@ -2,20 +2,20 @@ package com.iluwatar.delegation.simple;
|
||||
|
||||
public class PrinterController extends AbstractPrinterController {
|
||||
|
||||
public PrinterController(Printer printer) {
|
||||
super(printer);
|
||||
}
|
||||
public PrinterController(Printer printer) {
|
||||
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);
|
||||
}
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
@ -10,12 +10,12 @@ import com.iluwatar.delegation.simple.Printer;
|
||||
*/
|
||||
public class CanonPrinter implements Printer {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void print(String message) {
|
||||
System.out.println("Canon Printer : " + message);
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void print(String message) {
|
||||
System.out.println("Canon Printer : " + message);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,14 +8,14 @@ import com.iluwatar.delegation.simple.Printer;
|
||||
*
|
||||
* @see Printer
|
||||
*/
|
||||
public class EpsonPrinter implements Printer{
|
||||
public class EpsonPrinter implements Printer {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void print(String message) {
|
||||
System.out.println("Epson Printer : " + message);
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void print(String message) {
|
||||
System.out.println("Epson Printer : " + message);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,14 +8,14 @@ import com.iluwatar.delegation.simple.Printer;
|
||||
*
|
||||
* @see Printer
|
||||
*/
|
||||
public class HPPrinter implements Printer {
|
||||
public class HpPrinter implements Printer {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void print(String message) {
|
||||
System.out.println("HP Printer : " + message);
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void print(String message) {
|
||||
System.out.println("HP Printer : " + message);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ 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;
|
||||
import com.iluwatar.delegation.simple.printers.HpPrinter;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.contrib.java.lang.system.SystemOutRule;
|
||||
@ -26,7 +26,7 @@ public class DelegateTest {
|
||||
|
||||
@Test
|
||||
public void testHPPrinter() throws Exception {
|
||||
AbstractPrinterController abstractController = new PrinterController(new HPPrinter());
|
||||
AbstractPrinterController abstractController = new PrinterController(new HpPrinter());
|
||||
abstractController.print(MESSAGE);
|
||||
|
||||
assertEquals("HP Printer : Test Message Printed\n", systemOutRule.getLog());
|
||||
|
Loading…
x
Reference in New Issue
Block a user