commit
f987c7253b
BIN
delegation/etc/delegation.png
Normal file
BIN
delegation/etc/delegation.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 25 KiB |
83
delegation/etc/delegation.ucls
Normal file
83
delegation/etc/delegation.ucls
Normal file
@ -0,0 +1,83 @@
|
||||
<?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">
|
||||
<class id="1" 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="764" y="272"/>
|
||||
<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="2" 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="511" y="270"/>
|
||||
<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.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="252" y="275"/>
|
||||
<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>
|
||||
<interface id="4" 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="524" y="89"/>
|
||||
<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="5" 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="112" y="90"/>
|
||||
<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>
|
||||
<realization id="6">
|
||||
<end type="SOURCE" refId="2"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</realization>
|
||||
<association id="7">
|
||||
<end type="SOURCE" refId="5" navigable="false">
|
||||
<attribute id="8" name="printer"/>
|
||||
<multiplicity id="9" minimum="0" maximum="1"/>
|
||||
</end>
|
||||
<end type="TARGET" refId="4" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="10">
|
||||
<end type="SOURCE" refId="5"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</realization>
|
||||
<realization id="11">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</realization>
|
||||
<realization id="12">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</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>
|
27
delegation/index.md
Normal file
27
delegation/index.md
Normal file
@ -0,0 +1,27 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Delegation
|
||||
folder: delegation
|
||||
permalink: /patterns/delegation/
|
||||
categories: Behavioral
|
||||
tags:
|
||||
- Java
|
||||
- Difficulty-Beginner
|
||||
---
|
||||
|
||||
**Also known as:** Proxy Pattern
|
||||
|
||||
**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.
|
||||
|
||||

|
||||
|
||||
**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**
|
||||
|
||||
* [Delegate Pattern: Wikipedia ](https://en.wikipedia.org/wiki/Delegation_pattern)
|
||||
* [Proxy Pattern: Wikipedia ](https://en.wikipedia.org/wiki/Proxy_pattern)
|
27
delegation/pom.xml
Normal file
27
delegation/pom.xml
Normal file
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<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>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,38 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* The delegate pattern provides a mechanism to abstract away the implementation and control of the desired action.
|
||||
* The class being called in this case {@link PrinterController} is not responsible for the actual desired action,
|
||||
* but is actually delegated to a helper class either {@link CanonPrinter}, {@link EpsonPrinter} or {@link HpPrinter}.
|
||||
* The consumer does not have or require knowledge of the actual class carrying out the action, only the
|
||||
* container on which they are calling.
|
||||
*
|
||||
* In this example the delegates are {@link EpsonPrinter}, {@link HpPrinter} and {@link CanonPrinter} they all implement
|
||||
* {@link Printer}. The {@link PrinterController} class also implements {@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) {
|
||||
PrinterController hpPrinterController = new PrinterController(new HpPrinter());
|
||||
PrinterController canonPrinterController = new PrinterController(new CanonPrinter());
|
||||
PrinterController epsonPrinterController = new PrinterController(new EpsonPrinter());
|
||||
|
||||
hpPrinterController.print(MESSAGE_TO_PRINT);
|
||||
canonPrinterController.print(MESSAGE_TO_PRINT);
|
||||
epsonPrinterController.print(MESSAGE_TO_PRINT);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* Interface that both the Controller and the Delegate will implement.
|
||||
*
|
||||
* @see CanonPrinter
|
||||
* @see EpsonPrinter
|
||||
* @see HpPrinter
|
||||
*/
|
||||
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);
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.iluwatar.delegation.simple;
|
||||
|
||||
public class PrinterController implements Printer {
|
||||
|
||||
private final Printer printer;
|
||||
|
||||
public PrinterController(Printer printer) {
|
||||
this.printer = 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) {
|
||||
printer.print(message);
|
||||
}
|
||||
}
|
@ -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.print("Canon Printer : " + message);
|
||||
}
|
||||
|
||||
}
|
@ -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.print("Epson Printer : " + message);
|
||||
}
|
||||
|
||||
}
|
@ -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.print("HP Printer : " + message);
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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 {
|
||||
PrinterController printerController = new PrinterController(new CanonPrinter());
|
||||
printerController.print(MESSAGE);
|
||||
|
||||
assertEquals("Canon Printer : Test Message Printed", systemOutRule.getLog());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHpPrinter() throws Exception {
|
||||
PrinterController printerController = new PrinterController(new HpPrinter());
|
||||
printerController.print(MESSAGE);
|
||||
|
||||
assertEquals("HP Printer : Test Message Printed", systemOutRule.getLog());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEpsonPrinter() throws Exception {
|
||||
PrinterController printerController = new PrinterController(new EpsonPrinter());
|
||||
printerController.print(MESSAGE);
|
||||
|
||||
assertEquals("Epson Printer : Test Message Printed", systemOutRule.getLog());
|
||||
}
|
||||
|
||||
}
|
8
pom.xml
8
pom.xml
@ -22,6 +22,7 @@
|
||||
<camel.version>2.15.3</camel.version>
|
||||
<log4j.version>1.2.17</log4j.version>
|
||||
<guava.version>18.0</guava.version>
|
||||
<systemrules.version>1.14.0</systemrules.version>
|
||||
</properties>
|
||||
<modules>
|
||||
<module>abstract-factory</module>
|
||||
@ -88,6 +89,7 @@
|
||||
<module>reactor</module>
|
||||
<module>caching</module>
|
||||
<module>publish-subscribe</module>
|
||||
<module>delegation</module>
|
||||
</modules>
|
||||
|
||||
<dependencyManagement>
|
||||
@ -154,6 +156,12 @@
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.stefanbirkner</groupId>
|
||||
<artifactId>system-rules</artifactId>
|
||||
<version>${systemrules.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user