JAVA design pattern (5) command pattern

The command pattern encapsulates "requests" into objects so that other objects can be parameterized with different requests, queues, or logs. Command mode also supports undoable operations.

package com.zaxk.study.pattern;

/**
 * command mode
 * Created by ZhuXu on 2017/11/11 0011.
 */
public class CommandTest {

    public static void main(String[] args) {
        Weapon sword = new Sword();
        Weapon bow = new Bow();
        Weapon staff = new Staff();

        Command swordAttack = new NormalAttackCommand(sword);
        Command swordSkilll = new SkillAttackCommand(sword);

        Command bowAttack = new NormalAttackCommand(bow);
        Command bowSkilll = new SkillAttackCommand(bow);

        Command statffAttack = new NormalAttackCommand(staff);
        Command staffSkilll = new SkillAttackCommand(staff);

        Role warrior = new Role("战士", swordAttack, swordSkilll);
        Role hunter = new Role("猎人", bowAttack, bowSkilll);
        Role mage = new Role("法师", statffAttack, staffSkilll);

        warrior.attack();
        hunter.attack();
        mage.attack();

        warrior.skill();
        hunter.skill();
        mage.skill();

        warrior.defence();
        hunter.escape();
        mage.escape();

    }
}

/**
 * Order
 * Command mode: Command
 */
interface Command {
    void execute();
}

/**
 * attack command
 * Command mode: Command
 */
interface AttackCommand extends Command {

    void attack();

    default void execute() {
        attack();
    }
}

/**
 * Defense orders
 * Command mode: ConcreteCommand
 */
class DefenseCommand implements Command {

    public void execute() {
        System.out.println("I carry it, you output quickly!");
    }
}

/**
 * escape command
 * Command mode: ConcreteCommand
 */
class EscapeCommand implements Command {

    public void execute() {
        System.out.println("Fuck, slipped!");
    }
}

/**
 * Normal attack command
 * Command mode: ConcreteCommand
 */
class NormalAttackCommand implements AttackCommand {

    Weapon weapon;

    NormalAttackCommand(Weapon weapon) {
        this.weapon = weapon;
    }

    @Override
    public void attack() {
        weapon.attack();
    }
}

/**
 * Skill attack command
 * Command mode: ConcreteCommand
 */
class SkillAttackCommand implements AttackCommand {

    Weapon weapon;

    SkillAttackCommand(Weapon weapon) {
        this.weapon = weapon;
    }

    @Override
    public void attack() {
        weapon.skill();
    }
}

/**
 * weapons
 * Command mode: Receiver
 */
abstract class Weapon {
    abstract void attack();
    abstract void skill();
}

/**
 * Sword
 * Command mode: ConcreteReceiver
 */
class Sword extends Weapon {

    @Override
    void attack() {
        System.out.println("I cut!");
    }

    @Override
    void skill() {
        System.out.println("Starburst Air Slash!");
    }
}

/**
 * Bow and arrow
 * Command mode: ConcreteReceiver
 */
class Bow extends Weapon {

    @Override
    void attack() {
        System.out.println("I shot!");
    }

    @Override
    void skill() {
        System.out.println("All arrows fired!");
    }
}

/**
 * Staff
 * Command mode: ConcreteReceiver
 */
class Staff extends Weapon {

    @Override
    void attack() {
        System.out.println("I hit!");
    }

    @Override
    void skill() {
        System.out.println("God kills!");
    }
}

/**
 * Role
 * Command mode: Invoker
 */
class Role {

    private String name;

    private Command attackCommand, skillCommand, defenseCommand, escapeCommand;

    Role(String name, Command attackCommand, Command skillCommand) {
        this.name = new StringBuffer(name).append(":").toString();
        this.attackCommand = attackCommand;
        this.skillCommand = skillCommand;
        defenseCommand = new DefenseCommand();
        escapeCommand = new EscapeCommand();
    }

    public void attack() {
        System.out.print(name);
        attackCommand.execute();
    }

    public void skill() {
        System.out.print(name);
        skillCommand.execute();
    }

    public void defence() {
        System.out.print(name);
        defenseCommand.execute();
    }

    public void escape() {
        System.out.print(name);
        escapeCommand.execute();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326522442&siteId=291194637