added lab14 ex1
This commit is contained in:
parent
64f7f56ffe
commit
23829388cb
8
src/lab14_strategy/ex1/AxeBehavior.java
Normal file
8
src/lab14_strategy/ex1/AxeBehavior.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package lab14_strategy.ex1;
|
||||||
|
|
||||||
|
public class AxeBehavior implements WeaponBehavior {
|
||||||
|
@Override
|
||||||
|
public String useWeapon() {
|
||||||
|
return "chops with an axe";
|
||||||
|
}
|
||||||
|
}
|
19
src/lab14_strategy/ex1/Character.java
Normal file
19
src/lab14_strategy/ex1/Character.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package lab14_strategy.ex1;
|
||||||
|
|
||||||
|
public abstract class Character {
|
||||||
|
private WeaponBehavior weapon;
|
||||||
|
|
||||||
|
public Character(WeaponBehavior weapon) {
|
||||||
|
this.weapon = weapon;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWeapon(WeaponBehavior weapon) {
|
||||||
|
this.weapon = weapon;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract String getName();
|
||||||
|
|
||||||
|
public void fight() {
|
||||||
|
System.out.println(getName() + " " + weapon.useWeapon());
|
||||||
|
}
|
||||||
|
}
|
25
src/lab14_strategy/ex1/Game.java
Normal file
25
src/lab14_strategy/ex1/Game.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package lab14_strategy.ex1;
|
||||||
|
|
||||||
|
import lab14_strategy.ex1.characters.King;
|
||||||
|
import lab14_strategy.ex1.characters.Knight;
|
||||||
|
import lab14_strategy.ex1.characters.Troll;
|
||||||
|
import lab14_strategy.ex1.characters.Queen;
|
||||||
|
import lab14_strategy.ex1.weapons.BowAndArrowBehavior;
|
||||||
|
import lab14_strategy.ex1.weapons.KnifeBehavior;
|
||||||
|
|
||||||
|
public class Game {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Character king = new King(new KnifeBehavior());
|
||||||
|
Character queen = new Queen(new BowAndArrowBehavior());
|
||||||
|
Character knight = new Knight(new SwordBehavior());
|
||||||
|
Character troll = new Troll(new AxeBehavior());
|
||||||
|
|
||||||
|
king.fight();
|
||||||
|
queen.fight();
|
||||||
|
knight.fight();
|
||||||
|
troll.fight();
|
||||||
|
|
||||||
|
king.setWeapon(new SwordBehavior());
|
||||||
|
king.fight();
|
||||||
|
}
|
||||||
|
}
|
8
src/lab14_strategy/ex1/SwordBehavior.java
Normal file
8
src/lab14_strategy/ex1/SwordBehavior.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package lab14_strategy.ex1;
|
||||||
|
|
||||||
|
public class SwordBehavior implements WeaponBehavior {
|
||||||
|
@Override
|
||||||
|
public String useWeapon() {
|
||||||
|
return "swings a sword";
|
||||||
|
}
|
||||||
|
}
|
5
src/lab14_strategy/ex1/WeaponBehavior.java
Normal file
5
src/lab14_strategy/ex1/WeaponBehavior.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package lab14_strategy.ex1;
|
||||||
|
|
||||||
|
public interface WeaponBehavior {
|
||||||
|
String useWeapon();
|
||||||
|
}
|
15
src/lab14_strategy/ex1/characters/King.java
Normal file
15
src/lab14_strategy/ex1/characters/King.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package lab14_strategy.ex1.characters;
|
||||||
|
|
||||||
|
import lab14_strategy.ex1.Character;
|
||||||
|
import lab14_strategy.ex1.WeaponBehavior;
|
||||||
|
|
||||||
|
public class King extends Character {
|
||||||
|
public King(WeaponBehavior weapon) {
|
||||||
|
super(weapon);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "King";
|
||||||
|
}
|
||||||
|
}
|
15
src/lab14_strategy/ex1/characters/Knight.java
Normal file
15
src/lab14_strategy/ex1/characters/Knight.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package lab14_strategy.ex1.characters;
|
||||||
|
|
||||||
|
import lab14_strategy.ex1.Character;
|
||||||
|
import lab14_strategy.ex1.WeaponBehavior;
|
||||||
|
|
||||||
|
public class Knight extends Character {
|
||||||
|
public Knight(WeaponBehavior weapon) {
|
||||||
|
super(weapon);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "Knight";
|
||||||
|
}
|
||||||
|
}
|
15
src/lab14_strategy/ex1/characters/Queen.java
Normal file
15
src/lab14_strategy/ex1/characters/Queen.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package lab14_strategy.ex1.characters;
|
||||||
|
|
||||||
|
import lab14_strategy.ex1.Character;
|
||||||
|
import lab14_strategy.ex1.WeaponBehavior;
|
||||||
|
|
||||||
|
public class Queen extends Character {
|
||||||
|
public Queen(WeaponBehavior weapon) {
|
||||||
|
super(weapon);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "Queen";
|
||||||
|
}
|
||||||
|
}
|
15
src/lab14_strategy/ex1/characters/Troll.java
Normal file
15
src/lab14_strategy/ex1/characters/Troll.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package lab14_strategy.ex1.characters;
|
||||||
|
|
||||||
|
import lab14_strategy.ex1.Character;
|
||||||
|
import lab14_strategy.ex1.WeaponBehavior;
|
||||||
|
|
||||||
|
public class Troll extends Character {
|
||||||
|
public Troll(WeaponBehavior weapon) {
|
||||||
|
super(weapon);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "Troll";
|
||||||
|
}
|
||||||
|
}
|
10
src/lab14_strategy/ex1/weapons/BowAndArrowBehavior.java
Normal file
10
src/lab14_strategy/ex1/weapons/BowAndArrowBehavior.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package lab14_strategy.ex1.weapons;
|
||||||
|
|
||||||
|
import lab14_strategy.ex1.WeaponBehavior;
|
||||||
|
|
||||||
|
public class BowAndArrowBehavior implements WeaponBehavior {
|
||||||
|
@Override
|
||||||
|
public String useWeapon() {
|
||||||
|
return "shoots an arrow with a bow";
|
||||||
|
}
|
||||||
|
}
|
10
src/lab14_strategy/ex1/weapons/KnifeBehavior.java
Normal file
10
src/lab14_strategy/ex1/weapons/KnifeBehavior.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package lab14_strategy.ex1.weapons;
|
||||||
|
|
||||||
|
import lab14_strategy.ex1.WeaponBehavior;
|
||||||
|
|
||||||
|
public class KnifeBehavior implements WeaponBehavior {
|
||||||
|
@Override
|
||||||
|
public String useWeapon() {
|
||||||
|
return "cuts with a knife";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user