-
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.
Add composite Pattern test including an anonymous class
- Loading branch information
1 parent
823622e
commit d2146af
Showing
9 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
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
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,71 @@ | ||
package samples.inheritance.interfaces.compositepattern; | ||
|
||
public class CompositePattern { | ||
public static void main(String[] args) { | ||
Composite outerComposite = new Composite(5); | ||
outerComposite.addUnit(new Zealot()); | ||
outerComposite.addUnit(new Zealot()); | ||
outerComposite.addUnit(new DarkTemplar()); | ||
outerComposite.addUnit(new DarkTemplar()); | ||
|
||
Composite innerComposite = new Composite(3); | ||
innerComposite.addUnit(new Zealot()); | ||
innerComposite.addUnit(new Unit() { | ||
@Override | ||
public int attack() { | ||
return 7; | ||
} | ||
}); | ||
innerComposite.addUnit(new DarkTemplar()); | ||
|
||
outerComposite.addUnit(innerComposite); | ||
|
||
int result = outerComposite.attack(); | ||
} | ||
} | ||
|
||
interface Unit { | ||
int attack(); | ||
} | ||
|
||
class Zealot implements Unit { | ||
@Override | ||
public int attack() { | ||
return 11; | ||
} | ||
} | ||
|
||
class DarkTemplar implements Unit { | ||
@Override | ||
public int attack() { | ||
return 220; | ||
} | ||
} | ||
|
||
class Composite implements Unit { | ||
|
||
private final Unit[] units; | ||
private int currentIndex; | ||
|
||
public Composite(int size) { | ||
units = new Unit[size]; | ||
currentIndex = 0; | ||
} | ||
|
||
public void addUnit(Unit unit) { | ||
units[currentIndex++] = unit; | ||
} | ||
|
||
@Override | ||
public int attack() { | ||
int attackPower = 0; | ||
|
||
for (Unit unit : units) { | ||
if (unit != null) { | ||
attackPower += unit.attack(); | ||
} | ||
} | ||
|
||
return attackPower; | ||
} | ||
} |
Binary file added
BIN
+768 Bytes
tests/test_data/samples/inheritance/interfaces/compositepattern/Composite.class
Binary file not shown.
Binary file added
BIN
+557 Bytes
tests/test_data/samples/inheritance/interfaces/compositepattern/CompositePattern$1.class
Binary file not shown.
Binary file added
BIN
+926 Bytes
tests/test_data/samples/inheritance/interfaces/compositepattern/CompositePattern.class
Binary file not shown.
Binary file added
BIN
+366 Bytes
tests/test_data/samples/inheritance/interfaces/compositepattern/DarkTemplar.class
Binary file not shown.
Binary file added
BIN
+172 Bytes
tests/test_data/samples/inheritance/interfaces/compositepattern/Unit.class
Binary file not shown.
Binary file added
BIN
+360 Bytes
tests/test_data/samples/inheritance/interfaces/compositepattern/Zealot.class
Binary file not shown.
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