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
|
* 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
|
* be as clean as possible. This just provides the default constructor and a simple getter method. The generic of
|
||||||
* any implementation of {@link Printer}
|
* T allows any implementation of {@link Printer}
|
||||||
*
|
*
|
||||||
* @param <T> Printer
|
* @param <T> Printer
|
||||||
* @see Printer
|
* @see Printer
|
||||||
@ -12,23 +12,23 @@ package com.iluwatar.delegation.simple;
|
|||||||
public abstract class AbstractPrinterController<T extends Printer> implements Printer {
|
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
|
* @param printer instance of T {@link Printer} this instance is the delegate
|
||||||
*/
|
*/
|
||||||
public AbstractPrinterController(T printer) {
|
public AbstractPrinterController(T printer) {
|
||||||
this.printer = printer;
|
this.printer = printer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper method to return the current instance of T {@link Printer} in order for
|
* Helper method to return the current instance of T {@link Printer} in order for
|
||||||
* the controller to call operations on the {@link Printer}
|
* the controller to call operations on the {@link Printer}
|
||||||
*
|
*
|
||||||
* @return instance of Printer
|
* @return instance of Printer
|
||||||
* @see Printer
|
* @see Printer
|
||||||
*/
|
*/
|
||||||
protected T getPrinter() {
|
protected T getPrinter() {
|
||||||
return printer;
|
return printer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,32 +2,32 @@ package com.iluwatar.delegation.simple;
|
|||||||
|
|
||||||
import com.iluwatar.delegation.simple.printers.CanonPrinter;
|
import com.iluwatar.delegation.simple.printers.CanonPrinter;
|
||||||
import com.iluwatar.delegation.simple.printers.EpsonPrinter;
|
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
|
* 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}. The {@link AbstractPrinterController} and through inheritance {@link PrinterController} also
|
||||||
* {@link Printer}. However neither provide the functionality of {@link Printer} by printing to the screen, they actually
|
* implement {@link Printer}. However neither provide the functionality of {@link Printer} by printing to the screen,
|
||||||
* call upon the instance of {@link Printer} that they were instantiated with. Therefore delegating the behaviour to
|
* they actually call upon the instance of {@link Printer} that they were instantiated with. Therefore delegating the
|
||||||
* another class.
|
* behaviour to another class.
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static final String MESSAGE_TO_PRINT = "hello world";
|
public static final String MESSAGE_TO_PRINT = "hello world";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point
|
||||||
*
|
*
|
||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
AbstractPrinterController hpPrinterController = new PrinterController(new HPPrinter());
|
AbstractPrinterController hpPrinterController = new PrinterController(new HpPrinter());
|
||||||
AbstractPrinterController canonPrinterController = new PrinterController(new CanonPrinter());
|
AbstractPrinterController canonPrinterController = new PrinterController(new CanonPrinter());
|
||||||
AbstractPrinterController epsonPrinterController = new PrinterController(new EpsonPrinter());
|
AbstractPrinterController epsonPrinterController = new PrinterController(new EpsonPrinter());
|
||||||
|
|
||||||
hpPrinterController.print(MESSAGE_TO_PRINT);
|
hpPrinterController.print(MESSAGE_TO_PRINT);
|
||||||
canonPrinterController.print(MESSAGE_TO_PRINT);
|
canonPrinterController.print(MESSAGE_TO_PRINT);
|
||||||
epsonPrinterController.print(MESSAGE_TO_PRINT);
|
epsonPrinterController.print(MESSAGE_TO_PRINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,22 @@
|
|||||||
package com.iluwatar.delegation.simple;
|
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.CanonPrinter
|
||||||
* @see com.iluwatar.delegation.simple.printers.EpsonPrinter
|
* @see com.iluwatar.delegation.simple.printers.EpsonPrinter
|
||||||
* @see com.iluwatar.delegation.simple.printers.HPPrinter
|
* @see HpPrinter
|
||||||
* @see AbstractPrinterController
|
* @see AbstractPrinterController
|
||||||
*/
|
*/
|
||||||
public interface Printer {
|
public interface Printer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method that takes a String to print to the screen. This will be implemented on both the
|
* 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.
|
* controller and the delegate allowing the controller to call the same method on the delegate class.
|
||||||
*
|
*
|
||||||
* @param message to be printed to the screen
|
* @param message to be printed to the screen
|
||||||
*/
|
*/
|
||||||
void print(final String message);
|
void print(final String message);
|
||||||
}
|
}
|
||||||
|
@ -2,20 +2,20 @@ package com.iluwatar.delegation.simple;
|
|||||||
|
|
||||||
public class PrinterController extends AbstractPrinterController {
|
public class PrinterController extends AbstractPrinterController {
|
||||||
|
|
||||||
public PrinterController(Printer printer) {
|
public PrinterController(Printer printer) {
|
||||||
super(printer);
|
super(printer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is implemented from {@link Printer} however instead on providing an
|
* 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,
|
* 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
|
* hence the pattern. Therefore meaning that the caller does not care of the implementing class only the owning
|
||||||
* controller.
|
* controller.
|
||||||
*
|
*
|
||||||
* @param message to be printed to the screen
|
* @param message to be printed to the screen
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void print(String message) {
|
public void print(String message) {
|
||||||
getPrinter().print(message);
|
getPrinter().print(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,12 @@ import com.iluwatar.delegation.simple.Printer;
|
|||||||
*/
|
*/
|
||||||
public class CanonPrinter implements Printer {
|
public class CanonPrinter implements Printer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void print(String message) {
|
public void print(String message) {
|
||||||
System.out.println("Canon Printer : " + message);
|
System.out.println("Canon Printer : " + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,14 +8,14 @@ import com.iluwatar.delegation.simple.Printer;
|
|||||||
*
|
*
|
||||||
* @see Printer
|
* @see Printer
|
||||||
*/
|
*/
|
||||||
public class EpsonPrinter implements Printer{
|
public class EpsonPrinter implements Printer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void print(String message) {
|
public void print(String message) {
|
||||||
System.out.println("Epson Printer : " + message);
|
System.out.println("Epson Printer : " + message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,14 +8,14 @@ import com.iluwatar.delegation.simple.Printer;
|
|||||||
*
|
*
|
||||||
* @see Printer
|
* @see Printer
|
||||||
*/
|
*/
|
||||||
public class HPPrinter implements Printer {
|
public class HpPrinter implements Printer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void print(String message) {
|
public void print(String message) {
|
||||||
System.out.println("HP Printer : " + 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.CanonPrinter;
|
||||||
import com.iluwatar.delegation.simple.printers.EpsonPrinter;
|
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.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.contrib.java.lang.system.SystemOutRule;
|
import org.junit.contrib.java.lang.system.SystemOutRule;
|
||||||
@ -26,7 +26,7 @@ public class DelegateTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHPPrinter() throws Exception {
|
public void testHPPrinter() throws Exception {
|
||||||
AbstractPrinterController abstractController = new PrinterController(new HPPrinter());
|
AbstractPrinterController abstractController = new PrinterController(new HpPrinter());
|
||||||
abstractController.print(MESSAGE);
|
abstractController.print(MESSAGE);
|
||||||
|
|
||||||
assertEquals("HP Printer : Test Message Printed\n", systemOutRule.getLog());
|
assertEquals("HP Printer : Test Message Printed\n", systemOutRule.getLog());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user