Merge pull request #619 from iluwatar/bridge
Improved bridge example and added explanation
This commit is contained in:
166
bridge/README.md
166
bridge/README.md
@ -15,10 +15,170 @@ tags:
|
||||
Handle/Body
|
||||
|
||||
## Intent
|
||||
Decouple an abstraction from its implementation so that the two can
|
||||
vary independently.
|
||||
Decouple an abstraction from its implementation so that the two can vary independently.
|
||||
|
||||

|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
|
||||
> Consider you have a weapon with different enchantments and you are supposed to allow mixing different weapons with different enchantments. What would you do? Create multiple copies of each of the weapons for each of the enchantments or would you just create separate enchantment and set it for the weapon as needed? Bridge pattern allows you to do the second.
|
||||
|
||||
In Plain Words
|
||||
|
||||
> Bridge pattern is about preferring composition over inheritance. Implementation details are pushed from a hierarchy to another object with a separate hierarchy.
|
||||
|
||||
Wikipedia says
|
||||
|
||||
> The bridge pattern is a design pattern used in software engineering that is meant to "decouple an abstraction from its implementation so that the two can vary independently"
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
Translating our weapon example from above. Here we have the `Weapon` hierarchy
|
||||
|
||||
```
|
||||
public interface Weapon {
|
||||
void wield();
|
||||
void swing();
|
||||
void unwield();
|
||||
Enchantment getEnchantment();
|
||||
}
|
||||
|
||||
public class Sword implements Weapon {
|
||||
|
||||
private final Enchantment enchantment;
|
||||
|
||||
public Sword(Enchantment enchantment) {
|
||||
this.enchantment = enchantment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wield() {
|
||||
LOGGER.info("The sword is wielded.");
|
||||
enchantment.onActivate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swing() {
|
||||
LOGGER.info("The sword is swinged.");
|
||||
enchantment.apply();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwield() {
|
||||
LOGGER.info("The sword is unwielded.");
|
||||
enchantment.onDeactivate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enchantment getEnchantment() {
|
||||
return enchantment;
|
||||
}
|
||||
}
|
||||
|
||||
public class Hammer implements Weapon {
|
||||
|
||||
private final Enchantment enchantment;
|
||||
|
||||
public Hammer(Enchantment enchantment) {
|
||||
this.enchantment = enchantment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wield() {
|
||||
LOGGER.info("The hammer is wielded.");
|
||||
enchantment.onActivate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swing() {
|
||||
LOGGER.info("The hammer is swinged.");
|
||||
enchantment.apply();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwield() {
|
||||
LOGGER.info("The hammer is unwielded.");
|
||||
enchantment.onDeactivate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enchantment getEnchantment() {
|
||||
return enchantment;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
And the separate enchantment hierarchy
|
||||
|
||||
```
|
||||
public interface Enchantment {
|
||||
void onActivate();
|
||||
void apply();
|
||||
void onDeactivate();
|
||||
}
|
||||
|
||||
public class FlyingEnchantment implements Enchantment {
|
||||
|
||||
@Override
|
||||
public void onActivate() {
|
||||
LOGGER.info("The item begins to glow faintly.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply() {
|
||||
LOGGER.info("The item flies and strikes the enemies finally returning to owner's hand.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeactivate() {
|
||||
LOGGER.info("The item's glow fades.");
|
||||
}
|
||||
}
|
||||
|
||||
public class SoulEatingEnchantment implements Enchantment {
|
||||
|
||||
@Override
|
||||
public void onActivate() {
|
||||
LOGGER.info("The item spreads bloodlust.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply() {
|
||||
LOGGER.info("The item eats the soul of enemies.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeactivate() {
|
||||
LOGGER.info("Bloodlust slowly disappears.");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
And both the hierarchies in action
|
||||
|
||||
```
|
||||
Sword enchantedSword = new Sword(new SoulEatingEnchantment());
|
||||
enchantedSword.wield();
|
||||
enchantedSword.swing();
|
||||
enchantedSword.unwield();
|
||||
// The sword is wielded.
|
||||
// The item spreads bloodlust.
|
||||
// The sword is swinged.
|
||||
// The item eats the soul of enemies.
|
||||
// The sword is unwielded.
|
||||
// Bloodlust slowly disappears.
|
||||
|
||||
Hammer hammer = new Hammer(new FlyingEnchantment());
|
||||
hammer.wield();
|
||||
hammer.swing();
|
||||
hammer.unwield();
|
||||
// The hammer is wielded.
|
||||
// The item begins to glow faintly.
|
||||
// The hammer is swinged.
|
||||
// The item flies and strikes the enemies finally returning to owner's hand.
|
||||
// The hammer is unwielded.
|
||||
// The item's glow fades.
|
||||
```
|
||||
|
||||
## Applicability
|
||||
Use the Bridge pattern when
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 52 KiB |
@ -1,157 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<class id="1" language="java" name="com.iluwatar.bridge.FlyingMagicWeaponImpl" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/FlyingMagicWeaponImpl.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="515" y="591"/>
|
||||
<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.bridge.SoulEatingMagicWeaponImpl" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/SoulEatingMagicWeaponImpl.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="791" y="605"/>
|
||||
<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.bridge.Stormbringer" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/Stormbringer.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="791" y="788"/>
|
||||
<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="4" language="java" name="com.iluwatar.bridge.MagicWeaponImpl" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/MagicWeaponImpl.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="791" y="433"/>
|
||||
<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="5" language="java" name="com.iluwatar.bridge.BlindingMagicWeaponImpl" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/BlindingMagicWeaponImpl.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="1105" y="593"/>
|
||||
<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="6" language="java" name="com.iluwatar.bridge.SoulEatingMagicWeapon" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/SoulEatingMagicWeapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="380" y="21"/>
|
||||
<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="7" language="java" name="com.iluwatar.bridge.Excalibur" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/Excalibur.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="1105" y="782"/>
|
||||
<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="8" language="java" name="com.iluwatar.bridge.Mjollnir" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/Mjollnir.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="515" y="788"/>
|
||||
<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="9" language="java" name="com.iluwatar.bridge.BlindingMagicWeapon" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/BlindingMagicWeapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="791" y="14"/>
|
||||
<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="10" language="java" name="com.iluwatar.bridge.MagicWeapon" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/MagicWeapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="791" y="237"/>
|
||||
<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="11" language="java" name="com.iluwatar.bridge.FlyingMagicWeapon" project="bridge"
|
||||
file="/bridge/src/main/java/com/iluwatar/bridge/FlyingMagicWeapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="-1" width="-1" x="1144" y="12"/>
|
||||
<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>
|
||||
<generalization id="12">
|
||||
<end type="SOURCE" refId="8"/>
|
||||
<end type="TARGET" refId="1"/>
|
||||
</generalization>
|
||||
<generalization id="13">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</generalization>
|
||||
<generalization id="14">
|
||||
<end type="SOURCE" refId="9"/>
|
||||
<end type="TARGET" refId="10"/>
|
||||
</generalization>
|
||||
<generalization id="15">
|
||||
<end type="SOURCE" refId="2"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</generalization>
|
||||
<association id="16">
|
||||
<end type="SOURCE" refId="10" navigable="false">
|
||||
<attribute id="17" name="imp">
|
||||
<position height="0" width="0" x="478" y="284"/>
|
||||
</attribute>
|
||||
<multiplicity id="18" minimum="0" maximum="1">
|
||||
<position height="0" width="0" x="478" y="284"/>
|
||||
</multiplicity>
|
||||
</end>
|
||||
<end type="TARGET" refId="4" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<generalization id="19">
|
||||
<end type="SOURCE" refId="5"/>
|
||||
<end type="TARGET" refId="4"/>
|
||||
</generalization>
|
||||
<generalization id="20">
|
||||
<end type="SOURCE" refId="6"/>
|
||||
<end type="TARGET" refId="10"/>
|
||||
</generalization>
|
||||
<generalization id="21">
|
||||
<end type="SOURCE" refId="7"/>
|
||||
<end type="TARGET" refId="5"/>
|
||||
</generalization>
|
||||
<generalization id="22">
|
||||
<end type="SOURCE" refId="3"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</generalization>
|
||||
<generalization id="23">
|
||||
<end type="SOURCE" refId="11"/>
|
||||
<end type="TARGET" refId="10"/>
|
||||
</generalization>
|
||||
<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>
|
@ -1,92 +0,0 @@
|
||||
@startuml
|
||||
package com.iluwatar.bridge {
|
||||
class App {
|
||||
+ App()
|
||||
+ main(args : String[]) {static}
|
||||
}
|
||||
class BlindingMagicWeapon {
|
||||
+ BlindingMagicWeapon(imp : BlindingMagicWeaponImpl)
|
||||
+ blind()
|
||||
+ getImp() : BlindingMagicWeaponImpl
|
||||
+ swing()
|
||||
+ unwield()
|
||||
+ wield()
|
||||
}
|
||||
abstract class BlindingMagicWeaponImpl {
|
||||
+ BlindingMagicWeaponImpl()
|
||||
+ blindImp() {abstract}
|
||||
}
|
||||
class Excalibur {
|
||||
- LOGGER : Logger {static}
|
||||
+ Excalibur()
|
||||
+ blindImp()
|
||||
+ swingImp()
|
||||
+ unwieldImp()
|
||||
+ wieldImp()
|
||||
}
|
||||
class FlyingMagicWeapon {
|
||||
+ FlyingMagicWeapon(imp : FlyingMagicWeaponImpl)
|
||||
+ fly()
|
||||
+ getImp() : FlyingMagicWeaponImpl
|
||||
+ swing()
|
||||
+ unwield()
|
||||
+ wield()
|
||||
}
|
||||
abstract class FlyingMagicWeaponImpl {
|
||||
+ FlyingMagicWeaponImpl()
|
||||
+ flyImp() {abstract}
|
||||
}
|
||||
abstract class MagicWeapon {
|
||||
# imp : MagicWeaponImpl
|
||||
+ MagicWeapon(imp : MagicWeaponImpl)
|
||||
+ getImp() : MagicWeaponImpl
|
||||
+ swing() {abstract}
|
||||
+ unwield() {abstract}
|
||||
+ wield() {abstract}
|
||||
}
|
||||
abstract class MagicWeaponImpl {
|
||||
+ MagicWeaponImpl()
|
||||
+ swingImp() {abstract}
|
||||
+ unwieldImp() {abstract}
|
||||
+ wieldImp() {abstract}
|
||||
}
|
||||
class Mjollnir {
|
||||
- LOGGER : Logger {static}
|
||||
+ Mjollnir()
|
||||
+ flyImp()
|
||||
+ swingImp()
|
||||
+ unwieldImp()
|
||||
+ wieldImp()
|
||||
}
|
||||
class SoulEatingMagicWeapon {
|
||||
+ SoulEatingMagicWeapon(imp : SoulEatingMagicWeaponImpl)
|
||||
+ eatSoul()
|
||||
+ getImp() : SoulEatingMagicWeaponImpl
|
||||
+ swing()
|
||||
+ unwield()
|
||||
+ wield()
|
||||
}
|
||||
abstract class SoulEatingMagicWeaponImpl {
|
||||
+ SoulEatingMagicWeaponImpl()
|
||||
+ eatSoulImp() {abstract}
|
||||
}
|
||||
class Stormbringer {
|
||||
- LOGGER : Logger {static}
|
||||
+ Stormbringer()
|
||||
+ eatSoulImp()
|
||||
+ swingImp()
|
||||
+ unwieldImp()
|
||||
+ wieldImp()
|
||||
}
|
||||
}
|
||||
MagicWeapon --> "-imp" MagicWeaponImpl
|
||||
BlindingMagicWeapon --|> MagicWeapon
|
||||
BlindingMagicWeaponImpl --|> MagicWeaponImpl
|
||||
Excalibur --|> BlindingMagicWeaponImpl
|
||||
FlyingMagicWeapon --|> MagicWeapon
|
||||
FlyingMagicWeaponImpl --|> MagicWeaponImpl
|
||||
Mjollnir --|> FlyingMagicWeaponImpl
|
||||
SoulEatingMagicWeapon --|> MagicWeapon
|
||||
SoulEatingMagicWeaponImpl --|> MagicWeaponImpl
|
||||
Stormbringer --|> SoulEatingMagicWeaponImpl
|
||||
@enduml
|
@ -22,40 +22,42 @@
|
||||
*/
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* The Bridge pattern can also be thought of as two layers of abstraction. With Bridge, you can
|
||||
* decouple an abstraction from its implementation so that the two can vary independently.
|
||||
* Composition over inheritance. The Bridge pattern can also be thought of as two layers of abstraction.
|
||||
* With Bridge, you can decouple an abstraction from its implementation so that the two can vary independently.
|
||||
* <p>
|
||||
* In Bridge pattern both abstraction ({@link MagicWeapon}) and implementation (
|
||||
* {@link MagicWeaponImpl}) have their own class hierarchies. The interface of the implementations
|
||||
* In Bridge pattern both abstraction ({@link Weapon}) and implementation (
|
||||
* {@link Enchantment}) have their own class hierarchies. The interface of the implementations
|
||||
* can be changed without affecting the clients.
|
||||
* <p>
|
||||
* In this example we have two class hierarchies. One of weapons and another one of enchantments. We can easily
|
||||
* combine any weapon with any enchantment using composition instead of creating deep class hierarchy.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(new Excalibur());
|
||||
blindingMagicWeapon.wield();
|
||||
blindingMagicWeapon.blind();
|
||||
blindingMagicWeapon.swing();
|
||||
blindingMagicWeapon.unwield();
|
||||
LOGGER.info("The knight receives an enchanted sword.");
|
||||
Sword enchantedSword = new Sword(new SoulEatingEnchantment());
|
||||
enchantedSword.wield();
|
||||
enchantedSword.swing();
|
||||
enchantedSword.unwield();
|
||||
|
||||
FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(new Mjollnir());
|
||||
flyingMagicWeapon.wield();
|
||||
flyingMagicWeapon.fly();
|
||||
flyingMagicWeapon.swing();
|
||||
flyingMagicWeapon.unwield();
|
||||
|
||||
SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(new Stormbringer());
|
||||
soulEatingMagicWeapon.wield();
|
||||
soulEatingMagicWeapon.swing();
|
||||
soulEatingMagicWeapon.eatSoul();
|
||||
soulEatingMagicWeapon.unwield();
|
||||
LOGGER.info("The valkyrie receives an enchanted hammer.");
|
||||
Hammer hammer = new Hammer(new FlyingEnchantment());
|
||||
hammer.wield();
|
||||
hammer.swing();
|
||||
hammer.unwield();
|
||||
}
|
||||
}
|
||||
|
@ -24,11 +24,14 @@ package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* FlyingMagicWeaponImpl
|
||||
*
|
||||
* Enchantment
|
||||
*
|
||||
*/
|
||||
public abstract class FlyingMagicWeaponImpl extends MagicWeaponImpl {
|
||||
public interface Enchantment {
|
||||
|
||||
public abstract void flyImp();
|
||||
void onActivate();
|
||||
|
||||
void apply();
|
||||
|
||||
void onDeactivate();
|
||||
}
|
@ -27,30 +27,25 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Excalibur
|
||||
* FlyingEnchantment
|
||||
*
|
||||
*/
|
||||
public class Excalibur extends BlindingMagicWeaponImpl {
|
||||
public class FlyingEnchantment implements Enchantment {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Excalibur.class);
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(FlyingEnchantment.class);
|
||||
|
||||
@Override
|
||||
public void wieldImp() {
|
||||
LOGGER.info("wielding Excalibur");
|
||||
public void onActivate() {
|
||||
LOGGER.info("The item begins to glow faintly.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swingImp() {
|
||||
LOGGER.info("swinging Excalibur");
|
||||
public void apply() {
|
||||
LOGGER.info("The item flies and strikes the enemies finally returning to owner's hand.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwieldImp() {
|
||||
LOGGER.info("unwielding Excalibur");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void blindImp() {
|
||||
LOGGER.info("bright light streams from Excalibur blinding the enemy");
|
||||
public void onDeactivate() {
|
||||
LOGGER.info("The item's glow fades.");
|
||||
}
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* FlyingMagicWeapon
|
||||
*
|
||||
*/
|
||||
public class FlyingMagicWeapon extends MagicWeapon {
|
||||
|
||||
public FlyingMagicWeapon(FlyingMagicWeaponImpl imp) {
|
||||
super(imp);
|
||||
}
|
||||
|
||||
public FlyingMagicWeaponImpl getImp() {
|
||||
return (FlyingMagicWeaponImpl) imp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wield() {
|
||||
getImp().wieldImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swing() {
|
||||
getImp().swingImp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwield() {
|
||||
getImp().unwieldImp();
|
||||
}
|
||||
|
||||
public void fly() {
|
||||
getImp().flyImp();
|
||||
}
|
||||
|
||||
}
|
@ -22,38 +22,44 @@
|
||||
*/
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* BlindingMagicWeapon
|
||||
* Hammer
|
||||
*
|
||||
*/
|
||||
public class BlindingMagicWeapon extends MagicWeapon {
|
||||
public class Hammer implements Weapon {
|
||||
|
||||
public BlindingMagicWeapon(BlindingMagicWeaponImpl imp) {
|
||||
super(imp);
|
||||
}
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Hammer.class);
|
||||
|
||||
@Override
|
||||
public BlindingMagicWeaponImpl getImp() {
|
||||
return (BlindingMagicWeaponImpl) imp;
|
||||
private final Enchantment enchantment;
|
||||
|
||||
public Hammer(Enchantment enchantment) {
|
||||
this.enchantment = enchantment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wield() {
|
||||
getImp().wieldImp();
|
||||
LOGGER.info("The hammer is wielded.");
|
||||
enchantment.onActivate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swing() {
|
||||
getImp().swingImp();
|
||||
LOGGER.info("The hammer is swinged.");
|
||||
enchantment.apply();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwield() {
|
||||
getImp().unwieldImp();
|
||||
LOGGER.info("The hammer is unwielded.");
|
||||
enchantment.onDeactivate();
|
||||
}
|
||||
|
||||
public void blind() {
|
||||
getImp().blindImp();
|
||||
@Override
|
||||
public Enchantment getEnchantment() {
|
||||
return enchantment;
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* MagicWeaponImpl
|
||||
*
|
||||
*/
|
||||
public abstract class MagicWeaponImpl {
|
||||
|
||||
public abstract void wieldImp();
|
||||
|
||||
public abstract void swingImp();
|
||||
|
||||
public abstract void unwieldImp();
|
||||
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Mjollnir
|
||||
*
|
||||
*/
|
||||
public class Mjollnir extends FlyingMagicWeaponImpl {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Mjollnir.class);
|
||||
|
||||
@Override
|
||||
public void wieldImp() {
|
||||
LOGGER.info("wielding Mjollnir");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swingImp() {
|
||||
LOGGER.info("swinging Mjollnir");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwieldImp() {
|
||||
LOGGER.info("unwielding Mjollnir");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flyImp() {
|
||||
LOGGER.info("Mjollnir hits the enemy in the air and returns back to the owner's hand");
|
||||
}
|
||||
}
|
@ -27,30 +27,25 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* Stormbringer
|
||||
* SoulEatingEnchantment
|
||||
*
|
||||
*/
|
||||
public class Stormbringer extends SoulEatingMagicWeaponImpl {
|
||||
public class SoulEatingEnchantment implements Enchantment {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Stormbringer.class);
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SoulEatingEnchantment.class);
|
||||
|
||||
@Override
|
||||
public void wieldImp() {
|
||||
LOGGER.info("wielding Stormbringer");
|
||||
public void onActivate() {
|
||||
LOGGER.info("The item spreads bloodlust.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swingImp() {
|
||||
LOGGER.info("swinging Stormbringer");
|
||||
public void apply() {
|
||||
LOGGER.info("The item eats the soul of enemies.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwieldImp() {
|
||||
LOGGER.info("unwielding Stormbringer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eatSoulImp() {
|
||||
LOGGER.info("Stormbringer devours the enemy's soul");
|
||||
public void onDeactivate() {
|
||||
LOGGER.info("Bloodlust slowly disappears.");
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* SoulEatingMagicWeaponImpl
|
||||
*
|
||||
*/
|
||||
public abstract class SoulEatingMagicWeaponImpl extends MagicWeaponImpl {
|
||||
|
||||
public abstract void eatSoulImp();
|
||||
|
||||
}
|
@ -22,39 +22,44 @@
|
||||
*/
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* SoulEatingMagicWeapon
|
||||
* Sword
|
||||
*
|
||||
*/
|
||||
public class SoulEatingMagicWeapon extends MagicWeapon {
|
||||
public class Sword implements Weapon {
|
||||
|
||||
public SoulEatingMagicWeapon(SoulEatingMagicWeaponImpl imp) {
|
||||
super(imp);
|
||||
}
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Sword.class);
|
||||
|
||||
@Override
|
||||
public SoulEatingMagicWeaponImpl getImp() {
|
||||
return (SoulEatingMagicWeaponImpl) imp;
|
||||
private final Enchantment enchantment;
|
||||
|
||||
public Sword(Enchantment enchantment) {
|
||||
this.enchantment = enchantment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wield() {
|
||||
getImp().wieldImp();
|
||||
LOGGER.info("The sword is wielded.");
|
||||
enchantment.onActivate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void swing() {
|
||||
getImp().swingImp();
|
||||
LOGGER.info("The sword is swinged.");
|
||||
enchantment.apply();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unwield() {
|
||||
getImp().unwieldImp();
|
||||
LOGGER.info("The sword is unwielded.");
|
||||
enchantment.onDeactivate();
|
||||
}
|
||||
|
||||
public void eatSoul() {
|
||||
getImp().eatSoulImp();
|
||||
@Override
|
||||
public Enchantment getEnchantment() {
|
||||
return enchantment;
|
||||
}
|
||||
|
||||
}
|
@ -24,11 +24,16 @@ package com.iluwatar.bridge;
|
||||
|
||||
/**
|
||||
*
|
||||
* BlindingMagicWeaponImpl
|
||||
*
|
||||
* Weapon
|
||||
*
|
||||
*/
|
||||
public abstract class BlindingMagicWeaponImpl extends MagicWeaponImpl {
|
||||
public interface Weapon {
|
||||
|
||||
public abstract void blindImp();
|
||||
void wield();
|
||||
|
||||
void swing();
|
||||
|
||||
void unwield();
|
||||
|
||||
Enchantment getEnchantment();
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||
|
||||
/**
|
||||
* Date: 12/6/15 - 11:15 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class BlindingMagicWeaponTest extends MagicWeaponTest {
|
||||
|
||||
/**
|
||||
* Invoke all possible actions on the weapon and check if the actions are executed on the actual
|
||||
* underlying weapon implementation.
|
||||
*/
|
||||
@Test
|
||||
public void testExcalibur() throws Exception {
|
||||
final Excalibur excalibur = spy(new Excalibur());
|
||||
final BlindingMagicWeapon blindingMagicWeapon = new BlindingMagicWeapon(excalibur);
|
||||
|
||||
testBasicWeaponActions(blindingMagicWeapon, excalibur);
|
||||
|
||||
blindingMagicWeapon.blind();
|
||||
verify(excalibur, times(1)).blindImp();
|
||||
verifyNoMoreInteractions(excalibur);
|
||||
}
|
||||
|
||||
}
|
@ -24,32 +24,22 @@ package com.iluwatar.bridge;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||
|
||||
/**
|
||||
* Date: 12/6/15 - 11:26 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
* Tests for hammer
|
||||
*/
|
||||
public class FlyingMagicWeaponTest extends MagicWeaponTest {
|
||||
public class HammerTest extends WeaponTest {
|
||||
|
||||
/**
|
||||
* Invoke all possible actions on the weapon and check if the actions are executed on the actual
|
||||
* underlying weapon implementation.
|
||||
*/
|
||||
@Test
|
||||
public void testMjollnir() throws Exception {
|
||||
final Mjollnir mjollnir = spy(new Mjollnir());
|
||||
final FlyingMagicWeapon flyingMagicWeapon = new FlyingMagicWeapon(mjollnir);
|
||||
|
||||
testBasicWeaponActions(flyingMagicWeapon, mjollnir);
|
||||
|
||||
flyingMagicWeapon.fly();
|
||||
verify(mjollnir, times(1)).flyImp();
|
||||
verifyNoMoreInteractions(mjollnir);
|
||||
public void testHammer() throws Exception {
|
||||
final Hammer hammer = spy(new Hammer(mock(FlyingEnchantment.class)));
|
||||
testBasicWeaponActions(hammer);
|
||||
}
|
||||
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
/**
|
||||
* The MIT License
|
||||
* Copyright (c) 2014-2016 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||
|
||||
/**
|
||||
* Date: 12/6/15 - 11:43 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class SoulEatingMagicWeaponTest extends MagicWeaponTest {
|
||||
|
||||
/**
|
||||
* Invoke all possible actions on the weapon and check if the actions are executed on the actual
|
||||
* underlying weapon implementation.
|
||||
*/
|
||||
@Test
|
||||
public void testStormBringer() throws Exception {
|
||||
final Stormbringer stormbringer = spy(new Stormbringer());
|
||||
final SoulEatingMagicWeapon soulEatingMagicWeapon = new SoulEatingMagicWeapon(stormbringer);
|
||||
|
||||
testBasicWeaponActions(soulEatingMagicWeapon, stormbringer);
|
||||
|
||||
soulEatingMagicWeapon.eatSoul();
|
||||
verify(stormbringer, times(1)).eatSoulImp();
|
||||
verifyNoMoreInteractions(stormbringer);
|
||||
}
|
||||
|
||||
}
|
@ -22,26 +22,24 @@
|
||||
*/
|
||||
package com.iluwatar.bridge;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
*
|
||||
* MagicWeapon
|
||||
*
|
||||
* Tests for sword
|
||||
*/
|
||||
public abstract class MagicWeapon {
|
||||
public class SwordTest extends WeaponTest {
|
||||
|
||||
protected MagicWeaponImpl imp;
|
||||
|
||||
public MagicWeapon(MagicWeaponImpl imp) {
|
||||
this.imp = imp;
|
||||
/**
|
||||
* Invoke all possible actions on the weapon and check if the actions are executed on the actual
|
||||
* underlying weapon implementation.
|
||||
*/
|
||||
@Test
|
||||
public void testSword() throws Exception {
|
||||
final Sword sword = spy(new Sword(mock(FlyingEnchantment.class)));
|
||||
testBasicWeaponActions(sword);
|
||||
}
|
||||
|
||||
public abstract void wield();
|
||||
|
||||
public abstract void swing();
|
||||
|
||||
public abstract void unwield();
|
||||
|
||||
public MagicWeaponImpl getImp() {
|
||||
return imp;
|
||||
}
|
||||
}
|
||||
}
|
@ -28,37 +28,32 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||
|
||||
/**
|
||||
* Date: 12/6/15 - 11:28 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
* Base class for weapon tests
|
||||
*/
|
||||
public abstract class MagicWeaponTest {
|
||||
public abstract class WeaponTest {
|
||||
|
||||
/**
|
||||
* Invoke the basic actions of the given weapon, and test if the underlying weapon implementation
|
||||
* Invoke the basic actions of the given weapon, and test if the underlying enchantment implementation
|
||||
* is invoked
|
||||
*
|
||||
* @param weaponImpl The spied weapon implementation where actions are bridged to
|
||||
* @param weapon The weapon, handled by the app
|
||||
*/
|
||||
protected final void testBasicWeaponActions(final MagicWeapon weapon,
|
||||
final MagicWeaponImpl weaponImpl) {
|
||||
protected final void testBasicWeaponActions(final Weapon weapon) {
|
||||
assertNotNull(weapon);
|
||||
assertNotNull(weaponImpl);
|
||||
assertNotNull(weapon.getImp());
|
||||
Enchantment enchantment = weapon.getEnchantment();
|
||||
assertNotNull(enchantment);
|
||||
assertNotNull(weapon.getEnchantment());
|
||||
|
||||
weapon.swing();
|
||||
verify(weaponImpl, times(1)).swingImp();
|
||||
verifyNoMoreInteractions(weaponImpl);
|
||||
verify(enchantment, times(1)).apply();
|
||||
verifyNoMoreInteractions(enchantment);
|
||||
|
||||
weapon.wield();
|
||||
verify(weaponImpl, times(1)).wieldImp();
|
||||
verifyNoMoreInteractions(weaponImpl);
|
||||
verify(enchantment, times(1)).onActivate();
|
||||
verifyNoMoreInteractions(enchantment);
|
||||
|
||||
weapon.unwield();
|
||||
verify(weaponImpl, times(1)).unwieldImp();
|
||||
verifyNoMoreInteractions(weaponImpl);
|
||||
verify(enchantment, times(1)).onDeactivate();
|
||||
verifyNoMoreInteractions(enchantment);
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user