通知 网站从因情语写改为晴雨,这个网站的模板也从calmlog_ex改为 whimurmur

行为型模式-命令模式

2029人浏览 / 0人评论 / | 作者:因情语写  | 分类: 设计模式与算法  | 标签: 设计模式与算法

作者:因情语写

链接:https://www.qingyu.blue/article/455

声明:请尊重原作者的劳动,如需转载请注明出处


  行为型模式又可以分成以下四类:第一类:通过父类与子类的关系进行实现。第二类:两个类之间。第三类:类的状态。第四类:通过中间类

  命令模式属于第二类,两个类之间的关系

  是这样的,命令发出者持有命令,命令持有执行者,这样命令发出者发出命令,执行者执行命令,命令发出者和执行者被解耦

package behavior.pattern.betweenclass.command;

public class Invoker {
    private Command command;

    public void SetCommand(Command command)
    {
        this.command = command;
    }

    public void ExecuteCommand()
    {
        command.execute();
    }
}
package behavior.pattern.betweenclass.command;

public abstract class Command
{
    protected Account account;

    public Command(Account account)
    {
        this.account = account;
    }

    public abstract void execute();
}
package behavior.pattern.betweenclass.command;

public class MoneyInCommand extends Command {

    private float amount;

    public MoneyInCommand(Account account, float amount)
    {
        super(account);
        this.amount = amount;
    }

    /// <summary>
    /// 实现存钱命令
    /// </summary>
    @Override
    public void execute()
    {
        account.moneyIn(amount);
    }
}
package behavior.pattern.betweenclass.command;

public class MoneyOutCommand extends Command {

     private float amount;
     
     public MoneyOutCommand(Account account, float amount)
     {
         super(account);
         this.amount = amount;
     }

     /// <summary>
     /// 实现取钱命令
     /// </summary>
     @Override
     public void execute()
     {
         account.moneyOut(amount);
     }

}
package behavior.pattern.betweenclass.command;

/// <summary>
/// 银行帐号
/// </summary>
public class Account
{
    /// <summary>
    /// 帐号总金额
    /// </summary>
    private float totalAmount;

    /// <summary>
    /// 存钱
    /// </summary>
    /// <param name="amount"></param>
    public void moneyIn(float amount)
    {
        this.totalAmount += amount;
    }

    /// <summary>
    /// 取钱
    /// </summary>
    /// <param name="amount"></param>
    public void moneyOut(float amount)
    {
        this.totalAmount -= amount;
    }

    public float getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(float totalAmount) {
        this.totalAmount = totalAmount;
    }

}

  测试代码

package behavior.pattern.betweenclass.command;

public class Client {
    /**
     * 对一个账户进行操作,命令处理的对象是账户,调用者调用命令,命令操作账户完成账户金额的增减
     */
    public static void main(String[] args)
    {
        // 创建银行帐号
        Account account = new Account();
        // 创建一个存入500元的命令
        Command commandIn = new MoneyInCommand(account,500);
        // 创建一个调度者
        Invoker invoker = new Invoker();

        // 设置存钱命令
        invoker.SetCommand(commandIn);
        // 执行
        invoker.ExecuteCommand();
        System.out.println("The current amount is " + account.getTotalAmount());

        // 再次存入500
        Command commandIn2 = new MoneyInCommand(account, 500);
        invoker.SetCommand(commandIn2);
        invoker.ExecuteCommand();
        System.out.println("The current amount is " + account.getTotalAmount());

        // 取出300
        Command commandOut = new MoneyOutCommand(account, 300);
        invoker.SetCommand(commandOut);
        invoker.ExecuteCommand();
        System.out.println("The current amount is " + account.getTotalAmount());

    }
}

  运行结果


亲爱的读者:有时间可以点赞评论一下

点赞(0) 打赏

全部评论

还没有评论!