-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #330 from mikulucky/master
Delegation Pattern
- Loading branch information
Showing
13 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
![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** | ||
|
||
* [Delegate Pattern: Wikipedia ](https://en.wikipedia.org/wiki/Delegation_pattern) | ||
* [Proxy Pattern: Wikipedia ](https://en.wikipedia.org/wiki/Proxy_pattern) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
38 changes: 38 additions & 0 deletions
38
delegation/src/main/java/com/iluwatar/delegation/simple/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
delegation/src/main/java/com/iluwatar/delegation/simple/Printer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
23 changes: 23 additions & 0 deletions
23
delegation/src/main/java/com/iluwatar/delegation/simple/PrinterController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
delegation/src/main/java/com/iluwatar/delegation/simple/printers/CanonPrinter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
delegation/src/main/java/com/iluwatar/delegation/simple/printers/EpsonPrinter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
delegation/src/main/java/com/iluwatar/delegation/simple/printers/HpPrinter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
delegation/src/test/java/com/iluwatar/delegation/simple/AppTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
delegation/src/test/java/com/iluwatar/delegation/simple/DelegateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters