Merge branch 'add-delegation-pattern'

* add-delegation-pattern:
  Add java documentation #324
  Populate the index.md for the delegate module #324
  Move App.java to correct Package #324
  Move App.java to correct Package #324
  Generic For AbstractPrinterController #324
  Generate UML for delegation pattern
  Add simple tests for delegate pattern #324
  Make AppTest.java match other patterns and update AppTest.java to match other patterns #324
  3am Code is starting to show. Get this build working. #324
  Created a unit test for build, added junit to pom for delegation maven module. #324
  Add template index.md for population later #324
  Added skeleton code for delegation pattern #324
  Create maven module for delegation pattern #324
This commit is contained in:
Joseph McCarthy 2015-12-28 20:01:14 +00:00
commit fcadb223ce
14 changed files with 402 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.9" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
realizations="true" associations="true" dependencies="false" nesting-relationships="true" router="FAN">
<interface id="1" language="java" name="com.iluwatar.delegation.simple.Printer" project="delegation"
file="/delegation/src/main/java/com/iluwatar/delegation/simple/Printer.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="362" y="84"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<class id="2" language="java" name="com.iluwatar.delegation.simple.AbstractPrinterController" project="delegation"
file="/delegation/src/main/java/com/iluwatar/delegation/simple/AbstractPrinterController.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="97" width="191" x="41" y="37"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="3" language="java" name="com.iluwatar.delegation.simple.PrinterController" project="delegation"
file="/delegation/src/main/java/com/iluwatar/delegation/simple/PrinterController.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="136" y="214"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="4" language="java" name="com.iluwatar.delegation.simple.printers.HPPrinter" project="delegation"
file="/delegation/src/main/java/com/iluwatar/delegation/simple/printers/HPPrinter.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="187" y="333"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="5" language="java" name="com.iluwatar.delegation.simple.printers.EpsonPrinter" project="delegation"
file="/delegation/src/main/java/com/iluwatar/delegation/simple/printers/EpsonPrinter.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="362" y="333"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="6" language="java" name="com.iluwatar.delegation.simple.printers.CanonPrinter" project="delegation"
file="/delegation/src/main/java/com/iluwatar/delegation/simple/printers/CanonPrinter.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="537" y="333"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<generalization id="7">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="2"/>
</generalization>
<realization id="8">
<end type="SOURCE" refId="5"/>
<end type="TARGET" refId="1"/>
</realization>
<realization id="9">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="1"/>
</realization>
<association id="10">
<end type="SOURCE" refId="2" navigable="false">
<attribute id="11" name="printer"/>
<multiplicity id="12" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<realization id="13">
<end type="SOURCE" refId="6"/>
<end type="TARGET" refId="1"/>
</realization>
<realization id="14">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="1"/>
</realization>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>

26
delegation/index.md Normal file
View File

@ -0,0 +1,26 @@
---
layout: pattern
title: Delegation
folder: delegation
permalink: /patterns/delegation/
categories: Behavioral
tags:
- Java
- Difficulty-Beginner
---
**Also known as:** Proxy Chains
**Intent:** It is a technique where an object expresses certain behavior to the outside but in
reality delegates responsibility for implementing that behaviour to an associated object.
![alt text](./etc/delegation.png "Delegate")
**Applicability:** Use the Delegate pattern in order to achieve the following
* Reduce the coupling of methods to their class
* Components that behave identically, but realize that this situation can change in the future.
**Credits**
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)

51
delegation/pom.xml Normal file
View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ The MIT License (MIT)
~
~ Copyright (c) 2015 Orange Foundry
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in
~ all copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
~ THE SOFTWARE.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.9.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>delegation</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.14.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,34 @@
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}
*
* @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

@ -0,0 +1,33 @@
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;
/**
* 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());
AbstractPrinterController epsonPrinterController = new PrinterController(new EpsonPrinter());
hpPrinterController.print(MESSAGE_TO_PRINT);
canonPrinterController.print(MESSAGE_TO_PRINT);
epsonPrinterController.print(MESSAGE_TO_PRINT);
}
}

View File

@ -0,0 +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

@ -0,0 +1,21 @@
package com.iluwatar.delegation.simple;
public class PrinterController extends AbstractPrinterController {
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);
}
}

View File

@ -0,0 +1,21 @@
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

@ -0,0 +1,21 @@
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

@ -0,0 +1,21 @@
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);
}
}

View File

@ -0,0 +1,13 @@
package com.iluwatar.delegation.simple;
import org.junit.Test;
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
}

View File

@ -0,0 +1,43 @@
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 org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import static org.junit.Assert.assertEquals;
public class DelegateTest {
private static final String MESSAGE = "Test Message Printed";
@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
@Test
public void testCanonPrinter() throws Exception {
AbstractPrinterController abstractController = new PrinterController(new CanonPrinter());
abstractController.print(MESSAGE);
assertEquals("Canon Printer : Test Message Printed\n", systemOutRule.getLog());
}
@Test
public void testHPPrinter() throws Exception {
AbstractPrinterController abstractController = new PrinterController(new HPPrinter());
abstractController.print(MESSAGE);
assertEquals("HP Printer : Test Message Printed\n", systemOutRule.getLog());
}
@Test
public void testEpsonPrinter() throws Exception {
AbstractPrinterController abstractController = new PrinterController(new EpsonPrinter());
abstractController.print(MESSAGE);
assertEquals("Epson Printer : Test Message Printed\n", systemOutRule.getLog());
}
}

View File

@ -88,6 +88,7 @@
<module>reactor</module>
<module>caching</module>
<module>publish-subscribe</module>
<module>delegation</module>
</modules>
<dependencyManagement>