Skip to content

Commit fcadb22

Browse files
committed
Merge branch 'add-delegation-pattern'
* add-delegation-pattern: Add java documentation iluwatar#324 Populate the index.md for the delegate module iluwatar#324 Move App.java to correct Package iluwatar#324 Move App.java to correct Package iluwatar#324 Generic For AbstractPrinterController iluwatar#324 Generate UML for delegation pattern Add simple tests for delegate pattern iluwatar#324 Make AppTest.java match other patterns and update AppTest.java to match other patterns iluwatar#324 3am Code is starting to show. Get this build working. iluwatar#324 Created a unit test for build, added junit to pom for delegation maven module. iluwatar#324 Add template index.md for population later iluwatar#324 Added skeleton code for delegation pattern iluwatar#324 Create maven module for delegation pattern iluwatar#324
2 parents f59f180 + c842f88 commit fcadb22

14 files changed

Lines changed: 402 additions & 0 deletions

File tree

delegation/etc/delegation.png

22.3 KB
Loading

delegation/etc/delegation.ucls

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.9" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3+
realizations="true" associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<interface id="1" language="java" name="com.iluwatar.delegation.simple.Printer" project="delegation"
5+
file="/delegation/src/main/java/com/iluwatar/delegation/simple/Printer.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="362" y="84"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</interface>
13+
<class id="2" language="java" name="com.iluwatar.delegation.simple.AbstractPrinterController" project="delegation"
14+
file="/delegation/src/main/java/com/iluwatar/delegation/simple/AbstractPrinterController.java" binary="false"
15+
corner="BOTTOM_RIGHT">
16+
<position height="97" width="191" x="41" y="37"/>
17+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
18+
sort-features="false" accessors="true" visibility="true">
19+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
20+
<operations public="true" package="true" protected="true" private="true" static="true"/>
21+
</display>
22+
</class>
23+
<class id="3" language="java" name="com.iluwatar.delegation.simple.PrinterController" project="delegation"
24+
file="/delegation/src/main/java/com/iluwatar/delegation/simple/PrinterController.java" binary="false"
25+
corner="BOTTOM_RIGHT">
26+
<position height="-1" width="-1" x="136" y="214"/>
27+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
28+
sort-features="false" accessors="true" visibility="true">
29+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
30+
<operations public="true" package="true" protected="true" private="true" static="true"/>
31+
</display>
32+
</class>
33+
<class id="4" language="java" name="com.iluwatar.delegation.simple.printers.HPPrinter" project="delegation"
34+
file="/delegation/src/main/java/com/iluwatar/delegation/simple/printers/HPPrinter.java" binary="false"
35+
corner="BOTTOM_RIGHT">
36+
<position height="-1" width="-1" x="187" y="333"/>
37+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
38+
sort-features="false" accessors="true" visibility="true">
39+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
40+
<operations public="true" package="true" protected="true" private="true" static="true"/>
41+
</display>
42+
</class>
43+
<class id="5" language="java" name="com.iluwatar.delegation.simple.printers.EpsonPrinter" project="delegation"
44+
file="/delegation/src/main/java/com/iluwatar/delegation/simple/printers/EpsonPrinter.java" binary="false"
45+
corner="BOTTOM_RIGHT">
46+
<position height="-1" width="-1" x="362" y="333"/>
47+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
48+
sort-features="false" accessors="true" visibility="true">
49+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
50+
<operations public="true" package="true" protected="true" private="true" static="true"/>
51+
</display>
52+
</class>
53+
<class id="6" language="java" name="com.iluwatar.delegation.simple.printers.CanonPrinter" project="delegation"
54+
file="/delegation/src/main/java/com/iluwatar/delegation/simple/printers/CanonPrinter.java" binary="false"
55+
corner="BOTTOM_RIGHT">
56+
<position height="-1" width="-1" x="537" y="333"/>
57+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
58+
sort-features="false" accessors="true" visibility="true">
59+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
60+
<operations public="true" package="true" protected="true" private="true" static="true"/>
61+
</display>
62+
</class>
63+
<generalization id="7">
64+
<end type="SOURCE" refId="3"/>
65+
<end type="TARGET" refId="2"/>
66+
</generalization>
67+
<realization id="8">
68+
<end type="SOURCE" refId="5"/>
69+
<end type="TARGET" refId="1"/>
70+
</realization>
71+
<realization id="9">
72+
<end type="SOURCE" refId="4"/>
73+
<end type="TARGET" refId="1"/>
74+
</realization>
75+
<association id="10">
76+
<end type="SOURCE" refId="2" navigable="false">
77+
<attribute id="11" name="printer"/>
78+
<multiplicity id="12" minimum="0" maximum="1"/>
79+
</end>
80+
<end type="TARGET" refId="1" navigable="true"/>
81+
<display labels="true" multiplicity="true"/>
82+
</association>
83+
<realization id="13">
84+
<end type="SOURCE" refId="6"/>
85+
<end type="TARGET" refId="1"/>
86+
</realization>
87+
<realization id="14">
88+
<end type="SOURCE" refId="2"/>
89+
<end type="TARGET" refId="1"/>
90+
</realization>
91+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
92+
sort-features="false" accessors="true" visibility="true">
93+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
94+
<operations public="true" package="true" protected="true" private="true" static="true"/>
95+
</classifier-display>
96+
<association-display labels="true" multiplicity="true"/>
97+
</class-diagram>

delegation/index.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
layout: pattern
3+
title: Delegation
4+
folder: delegation
5+
permalink: /patterns/delegation/
6+
categories: Behavioral
7+
tags:
8+
- Java
9+
- Difficulty-Beginner
10+
---
11+
12+
**Also known as:** Proxy Chains
13+
14+
**Intent:** It is a technique where an object expresses certain behavior to the outside but in
15+
reality delegates responsibility for implementing that behaviour to an associated object.
16+
17+
![alt text](./etc/delegation.png "Delegate")
18+
19+
**Applicability:** Use the Delegate pattern in order to achieve the following
20+
21+
* Reduce the coupling of methods to their class
22+
* Components that behave identically, but realize that this situation can change in the future.
23+
24+
**Credits**
25+
26+
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)

delegation/pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ The MIT License (MIT)
4+
~
5+
~ Copyright (c) 2015 Orange Foundry
6+
~
7+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
8+
~ of this software and associated documentation files (the "Software"), to deal
9+
~ in the Software without restriction, including without limitation the rights
10+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
~ copies of the Software, and to permit persons to whom the Software is
12+
~ furnished to do so, subject to the following conditions:
13+
~
14+
~ The above copyright notice and this permission notice shall be included in
15+
~ all copies or substantial portions of the Software.
16+
~
17+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
~ THE SOFTWARE.
24+
-->
25+
26+
<project xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<parent>
30+
<artifactId>java-design-patterns</artifactId>
31+
<groupId>com.iluwatar</groupId>
32+
<version>1.9.0-SNAPSHOT</version>
33+
</parent>
34+
<modelVersion>4.0.0</modelVersion>
35+
36+
<artifactId>delegation</artifactId>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.github.stefanbirkner</groupId>
46+
<artifactId>system-rules</artifactId>
47+
<version>1.14.0</version>
48+
<scope>test</scope>
49+
</dependency>
50+
</dependencies>
51+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.iluwatar.delegation.simple;
2+
3+
/**
4+
* Extra layer of abstraction for the controller to allow the controller in this example {@link PrinterController} to
5+
* be as clean as possible. This just provides the default constructor and a simple getter method. The generic of T allows
6+
* any implementation of {@link Printer}
7+
*
8+
* @param <T> Printer
9+
* @see Printer
10+
* @see PrinterController
11+
*/
12+
public abstract class AbstractPrinterController<T extends Printer> implements Printer {
13+
14+
15+
private T printer;
16+
17+
/**
18+
* @param printer instance of T {@link Printer} this instance is the delegate
19+
*/
20+
public AbstractPrinterController(T printer) {
21+
this.printer = printer;
22+
}
23+
24+
/**
25+
* Helper method to return the current instance of T {@link Printer} in order for
26+
* the controller to call operations on the {@link Printer}
27+
*
28+
* @return instance of Printer
29+
* @see Printer
30+
*/
31+
protected T getPrinter() {
32+
return printer;
33+
}
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.iluwatar.delegation.simple;
2+
3+
import com.iluwatar.delegation.simple.printers.CanonPrinter;
4+
import com.iluwatar.delegation.simple.printers.EpsonPrinter;
5+
import com.iluwatar.delegation.simple.printers.HPPrinter;
6+
7+
/**
8+
* In this example the delegates are {@link EpsonPrinter}, {@link HPPrinter} and {@link CanonPrinter} they all implement
9+
* {@link Printer}. The {@link AbstractPrinterController} and through inheritance {@link PrinterController} also implement
10+
* {@link Printer}. However neither provide the functionality of {@link Printer} by printing to the screen, they actually
11+
* call upon the instance of {@link Printer} that they were instantiated with. Therefore delegating the behaviour to
12+
* another class.
13+
*/
14+
public class App {
15+
16+
public static final String MESSAGE_TO_PRINT = "hello world";
17+
18+
/**
19+
* Program entry point
20+
*
21+
* @param args command line args
22+
*/
23+
public static void main(String[] args) {
24+
AbstractPrinterController hpPrinterController = new PrinterController(new HPPrinter());
25+
AbstractPrinterController canonPrinterController = new PrinterController(new CanonPrinter());
26+
AbstractPrinterController epsonPrinterController = new PrinterController(new EpsonPrinter());
27+
28+
hpPrinterController.print(MESSAGE_TO_PRINT);
29+
canonPrinterController.print(MESSAGE_TO_PRINT);
30+
epsonPrinterController.print(MESSAGE_TO_PRINT);
31+
}
32+
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.iluwatar.delegation.simple;
2+
3+
/**
4+
* Interface that both the Controller and the Delegate will implement.
5+
*
6+
* @see com.iluwatar.delegation.simple.printers.CanonPrinter
7+
* @see com.iluwatar.delegation.simple.printers.EpsonPrinter
8+
* @see com.iluwatar.delegation.simple.printers.HPPrinter
9+
* @see AbstractPrinterController
10+
*/
11+
public interface Printer {
12+
13+
/**
14+
* Method that takes a String to print to the screen. This will be implemented on both the
15+
* controller and the delegate allowing the controller to call the same method on the delegate class.
16+
*
17+
* @param message to be printed to the screen
18+
*/
19+
void print(final String message);
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar.delegation.simple;
2+
3+
public class PrinterController extends AbstractPrinterController {
4+
5+
public PrinterController(Printer printer) {
6+
super(printer);
7+
}
8+
9+
/**
10+
* This method is implemented from {@link Printer} however instead on providing an
11+
* implementation, it instead calls upon the class passed through the constructor. This is the delegate,
12+
* hence the pattern. Therefore meaning that the caller does not care of the implementing class only the owning
13+
* controller.
14+
*
15+
* @param message to be printed to the screen
16+
*/
17+
@Override
18+
public void print(String message) {
19+
getPrinter().print(message);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar.delegation.simple.printers;
2+
3+
import com.iluwatar.delegation.simple.Printer;
4+
5+
/**
6+
* Specialised Implementation of {@link Printer} for a Canon Printer, in
7+
* this case the message to be printed is appended to "Canon Printer : "
8+
*
9+
* @see Printer
10+
*/
11+
public class CanonPrinter implements Printer {
12+
13+
/**
14+
* {@inheritDoc}
15+
*/
16+
@Override
17+
public void print(String message) {
18+
System.out.println("Canon Printer : " + message);
19+
}
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.iluwatar.delegation.simple.printers;
2+
3+
import com.iluwatar.delegation.simple.Printer;
4+
5+
/**
6+
* Specialised Implementation of {@link Printer} for a Epson Printer, in
7+
* this case the message to be printed is appended to "Epson Printer : "
8+
*
9+
* @see Printer
10+
*/
11+
public class EpsonPrinter implements Printer{
12+
13+
/**
14+
* {@inheritDoc}
15+
*/
16+
@Override
17+
public void print(String message) {
18+
System.out.println("Epson Printer : " + message);
19+
}
20+
21+
}

0 commit comments

Comments
 (0)