读书笔记_java设计模式深入研究 第十章 命令模式 Command

1,命令模式:主要针对需要执行的任务或用户提出的请求进行封装与抽象。抽象的命令接口描述了任务或请求的共同特征,而实现交由不同的具体命令对象完成。每个命令对象都是相互独立的,它负责完成需要执行的任务,却并不关心是谁调用的。
2,UML模型:
读书笔记_java设计模式深入研究 第十章 命令模式 Command
读书笔记_java设计模式深入研究 第十章 命令模式 Command
3,角色分析:
    -1,ICommander:抽象命令者,是一个接口,规定了用来封装请求的若干个方法。
    -2,ConcreteCommander:具体命令发送者,即命令源。实现命令接口。
    -3,Invoke:请求者,具体命令的管理和维护类。请求者是包含一个“命令接口”变量的实例。请求者调用命令接口,让具体命令执行那些封装的请求方法。
    -4,Reciever:命令接受者,是一个类的实例,该实例负责执行与请求相关的操作,
4,代码实例:
 
package pattern.chp10.commander.simple;
 
/**
* 类描述:命令接口
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 4, 2015 11:07:14 AM Jing Created.
*
*/
public interface ICommand {
/**
*
* 方法说明:打扫命令
*
* Author: Jing
* Create Date: Jan 4, 2015 11:07:36 AM
*
* @return void
*/
public void sweep();
}
package pattern.chp10.commander.simple;
 
/**
* 类描述:命令发送者
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 4, 2015 11:19:09 AM Jing Created.
*
*/
public class Teacher implements ICommand{
private Student reciever;
 
public Teacher(Student student) {
 
this.reciever = student;
}
 
public void sweep() {
reciever.sweeping();
}
 
}
 
package pattern.chp10.commander.simple;
 
/**
* 类描述:命令接受者,学生
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 4, 2015 11:07:55 AM Jing Created.
*
*/
public class Student {
/**
*
* 方法说明:正在打扫
*
* Author: Jing
* Create Date: Jan 4, 2015 11:08:36 AM
*
* @return void
*/
void sweeping(){
System.out.println("We are sweeping the floor");
}
}
package pattern.chp10.commander.simple;
 
/**
* 类描述:命令请求者
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 4, 2015 11:21:03 AM Jing Created.
*
*/
public class Invoke {
 
ICommand command;
 
public Invoke(ICommand command) {
 
this.command = command;
}
public void execute(){
command.sweep();
}
}
package pattern.chp10.commander.simple;
 
/**
* 类描述:
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 4, 2015 2:31:11 PM Jing Created.
*
*/
public class MainTest {
 
public static void main(String[] args) {
Student s = new Student();
ICommand t = new Teacher(s);
Invoke invoke = new Invoke(t);
invoke.execute();
}
}
5,深入理解命令模式,命令集管理
        假设输入多边形坐标,求取面积,代码如下:
·
package pattern.chp10.commander.area;
 
/**
* 类描述:坐标
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 4, 2015 2:55:20 PM Jing Created.
*
*/
public class Point {
 
float x, y;
 
public Point(float x, float y) {
this.x = x;
this.y = y;
}
 
}
package pattern.chp10.commander.area;
 
/**
* 类描述:面积计算
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 4, 2015 2:56:12 PM Jing Created.
*
*/
public class PolyonCalc {
/**
*
* 方法说明:根据坐标点计算面积
*
* Author: Jing Create Date: Jan 4, 2015 2:56:40 PM
*
* @param pt
* @return
* @return float
*/
public float getArea(Point[] pt) {
 
float s = 0;
int size = pt.length;
if (size < 3) {
 
return s;
}
 
s = pt[0].y * (pt[size - 1].x - pt[1].x);
for (int i = 1; i < size; i++) {
 
s += pt[i].y * (pt[(i - 1)].x - pt[(i + 1) % size].x);
}
 
return (float) s / 2;
}
}
package pattern.chp10.commander.area;
 
/**
* 类描述:命令发送者
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 4, 2015 3:04:52 PM Jing Created.
*
*/
public interface ICommand {
/**
*
* 方法说明:计算面积
*
* Author: Jing
* Create Date: Jan 4, 2015 3:05:10 PM
*
* @return
* @return float
*/
float calc();
}
package pattern.chp10.commander.area;
 
/**
* 类描述:具体面积命令发送者
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 4, 2015 3:05:44 PM Jing Created.
*
*/
public class AreaCommander implements ICommand {
 
PolyonCalc calc;
Point[] pt;
 
public AreaCommander(PolyonCalc calc, Point[] pt) {
 
this.calc = calc;
this.pt = pt;
}
 
public float calc() {
 
return calc.getArea(pt);
}
 
}
package pattern.chp10.commander.area;
 
import java.util.ArrayList;
 
/**
* 类描述:命令管理
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 4, 2015 3:07:22 PM Jing Created.
*
*/
public class CommandManage {
ArrayList<ICommand> list = new ArrayList<ICommand>();
/**
*
* 方法说明:添加命令
*
* Author: Jing
* Create Date: Jan 5, 2015 9:41:32 AM
*
* @param c
* @return void
*/
public void add(ICommand c){
list.add(c);
}
/**
*
* 方法说明:执行命令
*
* Author: Jing
* Create Date: Jan 5, 2015 9:41:59 AM
*
* @return void
*/
public void executeCommand(){
for(ICommand command : list){
float value = command.calc();
System.out.println("The area is : " + value);
}
}
}
 
package pattern.chp10.commander.area;
 
/**
* 类描述:
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 5, 2015 9:43:15 AM Jing Created.
*
*/
public class MainTest {
 
public static void main(String[] args) {
CommandManage manage = new CommandManage();
PolyonCalc calc = new PolyonCalc();
Point[] pt = new Point[3];
pt[0] = new Point(0, 0);
pt[1] = new Point(1, 0);
pt[2] = new Point(0, 1);
AreaCommander com = new AreaCommander(calc, pt);
manage.add(com);
manage.executeCommand();
}
}

6,命令模式与JDK事件处理:
    JDK事件处理机制如图:
    读书笔记_java设计模式深入研究 第十章 命令模式 Command

 -1,事件监听器是保存侦听哪些数据源的容器,技能注册事件源,也能撤销事件源。
 -2,当有事件发生的时候,事件监听器遍历监听的数据源容器,判断到底是哪个数据源产生的事件,若能找到,则执行对应的方法。
 -3,事件监听是java消息处理机制的核心,由java系统完成。
7,命令模式与多线程:
 连续接收字符串信息,并对重要的信息进行提示。
 
package pattern.chp10.commander.message;
 
/**
* 类描述:信息接收功能
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 5, 2015 10:03:36 AM Jing Created.
*
*/
public interface ISource {
/**
*
* 方法说明:是否为信息标志
*
* Author: Jing
* Create Date: Jan 5, 2015 10:05:13 AM
*
* @return
* @return boolean
*/
boolean isbFire();
/**
*
* 方法说明:设置重要信息标志
*
* Author: Jing
* Create Date: Jan 5, 2015 10:05:26 AM
*
* @param bFire
* @return void
*/
void setbFire(boolean bFire);
}
 
package pattern.chp10.commander.message;
 
/**
* 类描述:信息
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 5, 2015 10:04:59 AM Jing Created.
*
*/
public class Msgsrc implements ISource {
 
String msg;
boolean bFire;
 
public String getMsg() {
return msg;
}
 
public void setMsg(String msg) {
this.msg = msg;
}
 
public boolean isbFire() {
return bFire;
}
 
public void setbFire(boolean fire) {
 
this.bFire = fire;
}
/**
*
* 方法说明:判断消息
*
* Author: Jing
* Create Date: Jan 5, 2015 10:08:53 AM
*
* @param msg
* @return void
*/
public void come(String msg){
this.msg = msg;
if(msg.startsWith("zhang:")){
bFire = true;
//其他功能代码
}
}
 
}
package pattern.chp10.commander.message;
 
/**
* 类描述:特殊消息处理功能(命令接收者)
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 5, 2015 10:19:57 AM Jing Created.
*
*/
public interface IReceiver {
/**
*
* 方法说明:消息处理
*
* Author: Jing
* Create Date: Jan 5, 2015 10:20:34 AM
*
* @param src
* @return void
*/
void process(ISource src);
}

 
package pattern.chp10.commander.message;
 
/**
* 类描述:具体消息处理类
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 5, 2015 10:21:02 AM Jing Created.
*
*/
public class ShowReceive implements IReceiver {
 
public void process(ISource src) {
Msgsrc obj = (Msgsrc) src;
System.out.println(obj.getMsg());
}
 
}
package pattern.chp10.commander.message;
 
/**
* 类描述:Email接受者
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 5, 2015 10:22:44 AM Jing Created.
*
*/
public class EmailReceive implements IReceiver {
 
public void process(ISource src) {
 
System.out.println("This is email receiver,ths msg is : " + ((Msgsrc) src).getMsg());
}
 
}
package pattern.chp10.commander.message;
 
/**
* 类描述:命令监测功能(相当于Invoke)
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 5, 2015 10:24:37 AM Jing Created.
*
*/
public interface ICommand {
/**
*
* 方法说明:消息处理
*
* Author: Jing
* Create Date: Jan 5, 2015 10:25:39 AM
*
* @return void
*/
void noticeCommand();
}
package pattern.chp10.commander.message;
 
/**
* 类描述:具体命令类
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 5, 2015 10:34:13 AM Jing Created.
*
*/
public class Command implements ICommand {
 
IReceiver rvr;
ISource src;
 
public Command(IReceiver rvr, ISource src) {
 
this.rvr = rvr;
this.src = src;
}
 
public void noticeCommand() {
 
rvr.process(src);
}
}
 
package pattern.chp10.commander.message;
 
import java.util.Vector;
 
/**
* 类描述:命令管理
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 5, 2015 10:37:15 AM Jing Created.
*
*/
public class CommandManage extends Thread {
 
Vector<ICommand> v = new Vector<ICommand>();
ISource src;
boolean bMark = true;
 
public CommandManage(ISource src) {
 
this.src = src;
}
 
public void addCommand(ICommand c) {
 
v.add(c);
}
 
public void run() {
 
while (bMark) {
 
if (!src.isbFire()) {
 
continue;
}
for (ICommand comm : v) {
 
comm.noticeCommand();
}
src.setbFire(false);
}
}
 
}
 
package pattern.chp10.commander.message;
 
/**
* 类描述:
*
* @author: Jing
* @version $Id: Exp$
*
* History: Jan 5, 2015 10:55:06 AM Jing Created.
*
*/
public class MainTest {
 
public static void main(String[] args) throws Exception {
Msgsrc src = new Msgsrc();//定义事件源(发送者)
ShowReceive rvr = new ShowReceive();//定义接收者
EmailReceive rvr2 = new EmailReceive();
Command com = new Command(rvr, src);//定义命令
Command com2 = new Command(rvr2, src);//定义命令
CommandManage manage = new CommandManage(src);//命令接收集
manage.addCommand(com);
manage.addCommand(com2);
manage.start();
String[] s = {"Liu:sdad", "zhang:sdada", "li:sdasdasd"};
for(String sTemp: s){
src.come(sTemp);
Thread.sleep(1000);
}
manage.bMark = false;
}
}