设计模式--命令模式和访问者模式

1、命令模式

package com.yqj.pattern.command;

//具体执行者
class LightReceiver{
    public void on(){
        System.out.println("开灯");
    }

    public void off(){
        System.out.println("关灯");
    }
}

//命令接口
interface Command{
    //执行命令
    public void execute();
    //撤销操作
    public void undo();
}

//具体命令,开灯
class LightOnCommand implements Command{
    //聚合具体的执行者
    private LightReceiver lightReceiver;
    //传入执行者
    public LightOnCommand(LightReceiver lightReceiver) {
        this.lightReceiver = lightReceiver;
    }

    @Override
    public void execute() {
        //调用执行者的方法
        lightReceiver.on();
    }

    @Override
    public void undo() {
        //调用执行者的方法
        lightReceiver.off();
    }
}

//具体命令,关灯
class LightOffCommand implements Command{

    private LightReceiver lightReceiver;

    public LightOffCommand(LightReceiver lightReceiver) {
        this.lightReceiver = lightReceiver;
    }

    @Override
    public void execute() {
        lightReceiver.off();
    }

    @Override
    public void undo() {
        lightReceiver.on();
    }
}

//具体命令,空实现,方便初始化按钮。省掉对空的判断
class NoCommand implements Command{

    @Override
    public void execute() {

    }

    @Override
    public void undo() {

    }
}

class RemoteController{
    //按钮命令数值
    private Command[] onCommands;
    private Command[] offCommands;
    //撤销命令,记录上次的操作
    private Command undoCommand;

    public RemoteController() {
        //初始化按钮
        onCommands = new Command[5];
        offCommands = new Command[5];
        undoCommand = new NoCommand();
        for (int i=0 ; i<5 ; i++){
           onCommands[i] = new NoCommand();
           offCommands[i] = new NoCommand();
        }
    }

    //给按钮设置具体的命令
    public void setCommand(int index,Command onCommand,Command offCommand){
        onCommands[index] = onCommand;
        offCommands[index] = offCommand;
    }

    //按下开按钮
    public void onButtonWasPushed(int index){
        //找到按下的按钮,并调用该按钮的方法
        onCommands[index].execute();
        //记录操作,用于撤销
        undoCommand = onCommands[index];
    }

    //按下关按钮
    public void offButtonWasPushed(int index){
        offCommands[index].execute();
        undoCommand = offCommands[index];
    }

    //按撤销按钮
    public void undoButtonWasPushed(){
        undoCommand.undo();
    }
}

public class Client {
    public static void main(String[] args) {
        //创建电灯对象(执行者)
        LightReceiver lightReceiver = new LightReceiver();
        //创建开,关灯的命令对象
        LightOnCommand lightOnCommand = new LightOnCommand(lightReceiver);
        LightOffCommand lightOffCommand = new LightOffCommand(lightReceiver);
        //创建遥控器
        RemoteController remoteController = new RemoteController();
        //设置按钮对应的命令
        remoteController.setCommand(0,lightOnCommand,lightOffCommand);
        //按下按钮
        remoteController.onButtonWasPushed(0);
        remoteController.offButtonWasPushed(0);
        remoteController.undoButtonWasPushed();
    }
}

2、访问者模式

package com.yqj.pattern.visitor;

import java.util.ArrayList;
import java.util.List;

abstract class People{
    private String name;

    public People(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public abstract void accept(Action action);
}

class Man extends People{

    public Man(String name) {
        super(name);
    }

    //双分派,首先将具体状态action作为参数传递到man中(第一次分派)
    //然后man类调用作为参数的action的具体方法getManResult(),同时将自己this作为参数传入(第二次分派)
    @Override
    public void accept(Action action) {
        action.getManResult(this);
    }
}

class Woman extends People{

    public Woman(String name) {
        super(name);
    }

    @Override
    public void accept(Action action) {
        action.getWomanResult(this);
    }
}

abstract class Action{
    //得到男的评价
    public abstract void getManResult(Man man);
    //得到女的评价
    public abstract void getWomanResult(Woman woman);
}

class Success extends Action{

    @Override
    public void getManResult(Man man) {
        System.out.println("男的赞同 "+ man.getName());
    }

    @Override
    public void getWomanResult(Woman woman) {
        System.out.println("女的赞同 " + woman.getName());
    }
}

class Fail extends Action{

    @Override
    public void getManResult(Man man) {
        System.out.println("男的反对" + man.getName());
    }

    @Override
    public void getWomanResult(Woman woman) {
        System.out.println("女的反对" + woman.getName());
    }
}

//数据结构,维护和管理很多人
class ObjectStructure{
    //维护集合
    private List<People> elements = new ArrayList<>();
    //添加一个人
    public void add(People p){
        elements.add(p);
    }
    //删除一个人
    public void remove(People p){
        elements.remove(p);
    }
    //显示测评情况
    public void display(Action action){
        for (People element : elements) {
            element.accept(action);
        }
    }
}

public class Client {
    public static void main(String[] args) {
        //创建两个评价标准
        Action success = new Success();
        Action fail = new Fail();
        //创建访问者对象
        Man bob = new Man("bob");
        Man tom = new Man("tom");
        Woman alice = new Woman("alice");
        //高层遍历访问者的对象
        ObjectStructure objectStructure = new ObjectStructure();
        //加入集合
        objectStructure.add(bob);
        objectStructure.add(tom);
        objectStructure.add(alice);
        //评价
        objectStructure.display(success);
        objectStructure.display(fail);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值