Skip to content

Commit 5b47d48

Browse files
authored
Merge pull request iluwatar#573 from SrdjanPaunovic/extension-objects
iluwatar#541 Extension objects pattern
2 parents f87249e + a0916aa commit 5b47d48

25 files changed

+676
-0
lines changed

extension-objects/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
layout: pattern
3+
title: Extension objects
4+
folder: extension-objects
5+
permalink: /patterns/extension-objects/
6+
categories: Behavioral
7+
tags:
8+
- Java
9+
- Difficulty-Intermediate
10+
---
11+
12+
## Intent
13+
Anticipate that an object’s interface needs to be extended in the future. Additional
14+
interfaces are defined by extension objects.
15+
16+
![Extension_objects](./etc/extension_obj.png "Extension objects")
17+
18+
## Applicability
19+
Use the Extension Objects pattern when:
20+
21+
* you need to support the addition of new or unforeseen interfaces to existing classes and you don't want to impact clients that don't need this new interface. Extension Objects lets you keep related operations together by defining them in a separate class
22+
* a class representing a key abstraction plays different roles for different clients. The number of roles the class can play should be open-ended. There is a need to preserve the key abstraction itself. For example, a customer object is still a customer object even if different subsystems view it differently.
23+
* a class should be extensible with new behavior without subclassing from it.
24+
25+
## Real world examples
26+
27+
* [OpenDoc](https://en.wikipedia.org/wiki/OpenDoc)
28+
* [Object Linking and Embedding](https://en.wikipedia.org/wiki/Object_Linking_and_Embedding)
37.1 KB
Loading
+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.2.0" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3+
realizations="true" associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="concreteextensions.Soldier" project="extension-objects"
5+
file="/extension-objects/src/main/java/concreteextensions/Soldier.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="483" y="339"/>
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+
</class>
13+
<class id="2" language="java" name="units.Unit" project="extension-objects"
14+
file="/extension-objects/src/main/java/units/Unit.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="192" y="115"/>
16+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</class>
22+
<interface id="3" language="java" name="abstractextensions.SoldierExtension" project="extension-objects"
23+
file="/extension-objects/src/main/java/abstractextensions/SoldierExtension.java" binary="false"
24+
corner="BOTTOM_RIGHT">
25+
<position height="-1" width="-1" x="510" y="229"/>
26+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
27+
sort-features="false" accessors="true" visibility="true">
28+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
29+
<operations public="true" package="true" protected="true" private="true" static="true"/>
30+
</display>
31+
</interface>
32+
<interface id="4" language="java" name="abstractextensions.UnitExtension" project="extension-objects"
33+
file="/extension-objects/src/main/java/abstractextensions/UnitExtension.java" binary="false" corner="BOTTOM_RIGHT">
34+
<position height="-1" width="-1" x="510" y="116"/>
35+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
36+
sort-features="false" accessors="true" visibility="true">
37+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
38+
<operations public="true" package="true" protected="true" private="true" static="true"/>
39+
</display>
40+
</interface>
41+
<class id="5" language="java" name="units.SoldierUnit" project="extension-objects"
42+
file="/extension-objects/src/main/java/units/SoldierUnit.java" binary="false" corner="BOTTOM_RIGHT">
43+
<position height="-1" width="-1" x="157" y="355"/>
44+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
45+
sort-features="false" accessors="true" visibility="true">
46+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
47+
<operations public="true" package="true" protected="true" private="true" static="true"/>
48+
</display>
49+
</class>
50+
<class id="6" language="java" name="concreteextensions.Sergeant" project="extension-objects"
51+
file="/extension-objects/src/main/java/concreteextensions/Sergeant.java" binary="false" corner="BOTTOM_RIGHT">
52+
<position height="-1" width="-1" x="650" y="375"/>
53+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
54+
sort-features="false" accessors="true" visibility="true">
55+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
56+
<operations public="true" package="true" protected="true" private="true" static="true"/>
57+
</display>
58+
</class>
59+
<interface id="7" language="java" name="abstractextensions.SergeantExtension" project="extension-objects"
60+
file="/extension-objects/src/main/java/abstractextensions/SergeantExtension.java" binary="false"
61+
corner="BOTTOM_RIGHT">
62+
<position height="-1" width="-1" x="672" y="230"/>
63+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
64+
sort-features="false" accessors="true" visibility="true">
65+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
66+
<operations public="true" package="true" protected="true" private="true" static="true"/>
67+
</display>
68+
</interface>
69+
<class id="8" language="java" name="units.SergeantUnit" project="extension-objects"
70+
file="/extension-objects/src/main/java/units/SergeantUnit.java" binary="false" corner="BOTTOM_RIGHT">
71+
<position height="-1" width="-1" x="315" y="460"/>
72+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
73+
sort-features="false" accessors="true" visibility="true">
74+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
75+
<operations public="true" package="true" protected="true" private="true" static="true"/>
76+
</display>
77+
</class>
78+
<class id="9" language="java" name="units.CommanderUnit" project="extension-objects"
79+
file="/extension-objects/src/main/java/units/CommanderUnit.java" binary="false" corner="BOTTOM_RIGHT">
80+
<position height="99" width="210" x="429" y="476"/>
81+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
82+
sort-features="false" accessors="true" visibility="true">
83+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
84+
<operations public="true" package="true" protected="true" private="true" static="true"/>
85+
</display>
86+
</class>
87+
<class id="10" language="java" name="concreteextensions.Commander" project="extension-objects"
88+
file="/extension-objects/src/main/java/concreteextensions/Commander.java" binary="false" corner="BOTTOM_RIGHT">
89+
<position height="-1" width="-1" x="823" y="477"/>
90+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
91+
sort-features="false" accessors="true" visibility="true">
92+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
93+
<operations public="true" package="true" protected="true" private="true" static="true"/>
94+
</display>
95+
</class>
96+
<interface id="11" language="java" name="abstractextensions.CommanderExtension" project="extension-objects"
97+
file="/extension-objects/src/main/java/abstractextensions/CommanderExtension.java" binary="false"
98+
corner="BOTTOM_RIGHT">
99+
<position height="-1" width="-1" x="827" y="217"/>
100+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
101+
sort-features="false" accessors="true" visibility="true">
102+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
103+
<operations public="true" package="true" protected="true" private="true" static="true"/>
104+
</display>
105+
</interface>
106+
<realization id="12">
107+
<end type="SOURCE" refId="1"/>
108+
<end type="TARGET" refId="3"/>
109+
</realization>
110+
<realization id="13">
111+
<end type="SOURCE" refId="10"/>
112+
<end type="TARGET" refId="11"/>
113+
</realization>
114+
<generalization id="14">
115+
<end type="SOURCE" refId="9"/>
116+
<end type="TARGET" refId="2"/>
117+
</generalization>
118+
<association id="15">
119+
<end type="SOURCE" refId="1" navigable="false">
120+
<attribute id="16" name="unit"/>
121+
<multiplicity id="17" minimum="0" maximum="1"/>
122+
</end>
123+
<end type="TARGET" refId="5" navigable="true"/>
124+
<display labels="true" multiplicity="true"/>
125+
</association>
126+
<association id="18">
127+
<end type="SOURCE" refId="10" navigable="false">
128+
<attribute id="19" name="unit"/>
129+
<multiplicity id="20" minimum="0" maximum="1"/>
130+
</end>
131+
<end type="TARGET" refId="9" navigable="true"/>
132+
<display labels="true" multiplicity="true"/>
133+
</association>
134+
<association id="21">
135+
<end type="SOURCE" refId="2" navigable="false">
136+
<attribute id="22" name="unitExtension"/>
137+
<multiplicity id="23" minimum="0" maximum="1"/>
138+
</end>
139+
<end type="TARGET" refId="4" navigable="true"/>
140+
<display labels="true" multiplicity="true"/>
141+
</association>
142+
<realization id="24">
143+
<end type="SOURCE" refId="6"/>
144+
<end type="TARGET" refId="7"/>
145+
</realization>
146+
<generalization id="25">
147+
<end type="SOURCE" refId="3"/>
148+
<end type="TARGET" refId="4"/>
149+
</generalization>
150+
<generalization id="26">
151+
<end type="SOURCE" refId="5"/>
152+
<end type="TARGET" refId="2"/>
153+
</generalization>
154+
<generalization id="27">
155+
<end type="SOURCE" refId="7"/>
156+
<end type="TARGET" refId="4"/>
157+
</generalization>
158+
<generalization id="28">
159+
<end type="SOURCE" refId="8"/>
160+
<end type="TARGET" refId="2"/>
161+
</generalization>
162+
<association id="29">
163+
<end type="SOURCE" refId="6" navigable="false">
164+
<attribute id="30" name="unit"/>
165+
<multiplicity id="31" minimum="0" maximum="1"/>
166+
</end>
167+
<end type="TARGET" refId="8" navigable="true"/>
168+
<display labels="true" multiplicity="true"/>
169+
</association>
170+
<generalization id="32">
171+
<end type="SOURCE" refId="11"/>
172+
<end type="TARGET" refId="4"/>
173+
</generalization>
174+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
175+
sort-features="false" accessors="true" visibility="true">
176+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
177+
<operations public="true" package="true" protected="true" private="true" static="true"/>
178+
</classifier-display>
179+
<association-display labels="true" multiplicity="true"/>
180+
</class-diagram>

extension-objects/pom.xml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-design-patterns</artifactId>
7+
<groupId>com.iluwatar</groupId>
8+
<version>1.16.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>extension-objects</artifactId>
13+
<dependencies>
14+
<dependency>
15+
<groupId>junit</groupId>
16+
<artifactId>junit</artifactId>
17+
<scope>test</scope>
18+
</dependency>
19+
</dependencies>
20+
21+
</project>
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import abstractextensions.CommanderExtension;
2+
import abstractextensions.SergeantExtension;
3+
import abstractextensions.SoldierExtension;
4+
import units.CommanderUnit;
5+
import units.SergeantUnit;
6+
import units.SoldierUnit;
7+
import units.Unit;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
/**
12+
* Anticipate that an object’s interface needs to be extended in the future.
13+
* Additional interfaces are defined by extension objects.
14+
*/
15+
public class App {
16+
17+
/**
18+
* Program entry point
19+
*
20+
* @param args command line args
21+
*/
22+
public static void main(String[] args) {
23+
24+
//Create 3 different units
25+
Unit soldierUnit = new SoldierUnit("SoldierUnit1");
26+
Unit sergeantUnit = new SergeantUnit("SergeantUnit1");
27+
Unit commanderUnit = new CommanderUnit("CommanderUnit1");
28+
29+
//check for each unit to have an extension
30+
checkExtensionsForUnit(soldierUnit);
31+
checkExtensionsForUnit(sergeantUnit);
32+
checkExtensionsForUnit(commanderUnit);
33+
34+
}
35+
36+
private static void checkExtensionsForUnit(Unit unit) {
37+
final Logger logger = LoggerFactory.getLogger(App.class);
38+
39+
SoldierExtension soldierExtension = (SoldierExtension) unit.getUnitExtension("SoldierExtension");
40+
SergeantExtension sergeantExtension = (SergeantExtension) unit.getUnitExtension("SergeantExtension");
41+
CommanderExtension commanderExtension = (CommanderExtension) unit.getUnitExtension("CommanderExtension");
42+
43+
//if unit have extension call the method
44+
if (soldierExtension != null) {
45+
soldierExtension.soldierReady();
46+
} else {
47+
logger.info(unit.getName() + " without SoldierExtension");
48+
}
49+
50+
if (sergeantExtension != null) {
51+
sergeantExtension.sergeantReady();
52+
} else {
53+
logger.info(unit.getName() + " without SergeantExtension");
54+
}
55+
56+
if (commanderExtension != null) {
57+
commanderExtension.commanderReady();
58+
} else {
59+
logger.info(unit.getName() + " without CommanderExtension");
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package abstractextensions;
2+
3+
/**
4+
* Interface with their method
5+
*/
6+
public interface CommanderExtension extends UnitExtension {
7+
8+
void commanderReady();
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package abstractextensions;
2+
3+
/**
4+
* Interface with their method
5+
*/
6+
public interface SergeantExtension extends UnitExtension {
7+
8+
void sergeantReady();
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package abstractextensions;
2+
3+
/**
4+
* Interface with their method
5+
*/
6+
public interface SoldierExtension extends UnitExtension {
7+
void soldierReady();
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package abstractextensions;
2+
3+
/**
4+
* Other Extensions will extend this interface
5+
*/
6+
public interface UnitExtension {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package concreteextensions;
2+
3+
import abstractextensions.CommanderExtension;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import units.CommanderUnit;
7+
8+
/**
9+
* Class defining Commander
10+
*/
11+
public class Commander implements CommanderExtension {
12+
13+
private CommanderUnit unit;
14+
15+
public Commander(CommanderUnit commanderUnit) {
16+
this.unit = commanderUnit;
17+
}
18+
19+
final Logger logger = LoggerFactory.getLogger(Commander.class);
20+
21+
@Override
22+
public void commanderReady() {
23+
logger.info("[Commander] " + unit.getName() + " is ready!");
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package concreteextensions;
2+
3+
import abstractextensions.SergeantExtension;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import units.SergeantUnit;
7+
8+
/**
9+
* Class defining Sergeant
10+
*/
11+
public class Sergeant implements SergeantExtension {
12+
13+
private SergeantUnit unit;
14+
15+
public Sergeant(SergeantUnit sergeantUnit) {
16+
this.unit = sergeantUnit;
17+
}
18+
19+
final Logger logger = LoggerFactory.getLogger(Sergeant.class);
20+
21+
@Override
22+
public void sergeantReady() {
23+
logger.info("[Sergeant] " + unit.getName() + " is ready! ");
24+
}
25+
}

0 commit comments

Comments
 (0)