Using the Chain of Responsibility, Command Mode

Purpose and task

Objective: familiar with the UML, familiar with the chain of responsibility, command mode.

Tasks: in accordance with the requirements of the experiment content, done using the chain of responsibility, command mode to achieve the experiment content

Content preview

Review the contents of the course, familiar with the chain of responsibility, command mode.

Experiment contents and requirements

  • Chain of Responsibility pattern:

1, students typically leave class one day, three days Dean, Dean seven days, please Chain of Responsibility pattern is given in conjunction with the class diagram and implemented.

  • Command mode:
    1. Tape recorders have broadcast, rewind, stop function, in conjunction with an analog command mode of the system, and given class implementation code FIGS.

 

The results (available Continued)

  • Chain of Responsibility pattern:

Handler (abstract handler):

package ChainResponsibility;

 

public abstract class Handle {

    private Handle next;

    public Handle getNext(){

        return next;

    }

    public void setNext(Handle next){

        this.next=next;

    }

    public abstract void HandleRequest(int request);

}

ConcreteHandler (specifically Processor):

 

Test categories:

result:

Class Diagram:

Second, the command mode:

The Command (abstract command classes):

 

ConcreteCommand(具体命令类):

 

Invoker(调用者):

package CommandPattern;

 

import java.util.ArrayList;

 

public class Client {

    ArrayList  commands;

 

    public Client() {

        super();

        commands = new ArrayList();

}

 

    public void setCommand(Command command) {

        commands.add(command);

}

 

    public void onButtonWasPushed(int slot) {

        ((Command)commands.get(slot-1)).execute();

    }

   

    public static void main(String[] args) {

       

        Client client = new Client();

        Radio radio = new Radio();

        Command broadcastCommand = new BroadCastCommand(radio);

        Command rewindCommand = new RewindCommand(radio);

        Command stopCommand = new StopCommand(radio);

        client.setCommand(broadcastCommand);

        client.setCommand(rewindCommand);

        client.setCommand(stopCommand);

        //播音

        client.onButtonWasPushed(1);

        //倒带

        client.onButtonWasPushed(2);

        //停止

        client.onButtonWasPushed(3);

    }

}

 

Receiver(接收者):

结果如下:

 

 

思考题:

  1. 在jsp中由过滤器,请问过滤器实现是否使用了哪种设计模式?

责任链模式

 

2、电视机有三个操作:开机、关机、换台,可以通过遥控器操作,请设计类图,模拟该系统。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/yszbrzdd/article/details/93376382