-
-
Save anonymous/c275f587b9dcb19c9cfc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * Created by IntelliJ IDEA. | |
| * User: ASUS | |
| * Date: 05.02.12 | |
| * Time: 2:34 | |
| * To change this template use File | Settings | File Templates. | |
| */ | |
| interface WeaponBehavior { | |
| void useWeapon(); | |
| } | |
| public abstract class Character { | |
| protected WeaponBehavior weapon; | |
| abstract void fight(); | |
| void changeWeapon(WeaponBehavior weapon) { this.weapon = weapon; } | |
| public static void main(String[] args) { | |
| King king = new King(); | |
| king.fight(); | |
| king.changeWeapon(new BowAndArrowBehavior()); | |
| king.fight(); | |
| } | |
| } | |
| class King extends Character { | |
| King() { weapon = new SwordBehavior(); } | |
| public void fight() { weapon.useWeapon(); } | |
| } | |
| class Queen extends Character { | |
| Queen() { weapon = new KnifeBehavior(); } | |
| public void fight() { weapon.useWeapon(); } | |
| } | |
| class Knight extends Character { | |
| Knight() { weapon = new AxeBehavior(); } | |
| public void fight() { weapon.useWeapon(); } | |
| } | |
| class Troll extends Character { | |
| public void fight() {} | |
| } | |
| class KnifeBehavior implements WeaponBehavior { | |
| public void useWeapon() { System.out.println("Knife"); } | |
| } | |
| class BowAndArrowBehavior implements WeaponBehavior { | |
| public void useWeapon() { System.out.println("BowAndArrow"); } | |
| } | |
| class AxeBehavior implements WeaponBehavior { | |
| public void useWeapon() { System.out.println("Axe"); } | |
| } | |
| class SwordBehavior implements WeaponBehavior { | |
| public void useWeapon() { System.out.println("Sword");} | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment