此代码,是在阅读unit3.81源码时写的,虽然有些粗糙,但是可以理解junit的设计思想:)
一、类得基本介绍
1、command包
a、command,抽象命令类
b、SingleCommand,单一命令
c、CommandSuite,复杂命令
d、RepeatedCommand,可以重估执行多次命令,装饰类
e、PauseCommand,暂停一段时候后,执行命令,装饰类
2、person包
a、Person ,描述人人可以执行的动作
b、BackwardCommand,后退命令
c、SitDownCommand,坐下命令
d、WalkCommand,前进命令
3、其他
a、CommandListener,通知命令的执行结果
b、ComandResult,观察命令的执行情况,并把情况告诉CommandListner
c、FailCommandException,命令执行失败异常类
二、相关说明
1、command是命令模式、装饰器模式、组合模式的核心
2、CommandResult,用来观察command的执行结果,用到模板模式
3、CommandListener。命令执行结果的输出地,只要实现Listener,可以输出到屏幕,txt,数据库
4、这些都可以在junit找到影子:)
三、相关代码
/** 命名类,核心中的核心 */
public interface Command {
public void exec(CommandResult result) ;
}
/**复合命令 */
public class CommandSuite implements Command {
private List<Command> commands = new LinkedList<Command>();
public void addCommand(Command c){
commands.add(c);
}
public void exec(CommandResult result){
for(Command c:commands){
c.exec(result);
}
}
}
/**单一命令 */
public interface SingleCommand extends Command {
public void doCommand() throws FailedCommandException;
public Person getPerson();
}
/**装饰器类,暂时5s,再执行 */
public class PauseCommand implements Command {
private Command c;
public PauseCommand(Command c){
this.c = c;
}
public void baseExec(CommandResult result) {
c.exec(result);
}
public void exec(CommandResult r){
System.out.println("暂停5s!");
baseExec(r);
}
}
/**重复十次 */
public class RepeatedCommand implements Command {
private Command c;
public RepeatedCommand(Command c){
this.c = c;
}
public void baseExec(CommandResult result) {
c.exec(result);
}
public void exec(CommandResult r){
System.out.println("重复10次!");
for(int i = 0 ;i < 10;i++){
baseExec(r);
}
}
}
/** 人的各种行为 */
public class Person {
private String name;
public Person(String name){
this.name = name ;
}
public Person() {
}
public void walk() throws FailedCommandException {
int a = new Random().nextInt() % 2;
if(a == 0 ){
throw new FailedCommandException(name+",拒绝命令");
}
System.out.println(name+",前进!");
}
public void sitDown() throws FailedCommandException{
int a = new Random().nextInt() % 2;
if(a == 0 ){
throw new FailedCommandException(name+",拒绝命令");
}
System.out.println(name+",坐下");
}
public void backward() throws FailedCommandException{
int a = new Random().nextInt() % 2;
if(a == 0 ){
throw new FailedCommandException(name+",拒绝命令");
}
System.out.println(name+",后退");
}
public String getName() {
return name;
}
}
/**后退命令*/
public class BackwardCommand implements SingleCommand {
private Person p;
public BackwardCommand(Person p){
this.p = p;
}
public void exec(CommandResult result) {
result.doCommand(this);
}
public void doCommand() throws FailedCommandException {
p.backward();
}
public Person getPerson() {
return p;
}
}
/**坐下命令 */
public class SitDownCommand implements SingleCommand {
private Person p;
public SitDownCommand(Person p){
this.p = p;
}
public void exec(CommandResult result){
result.doCommand(this);
}
public void doCommand() throws FailedCommandException {
p.sitDown();
}
public Person getPerson() {
return p;
}
}
/** 前进命令*/
public class WalkCommand implements SingleCommand {
private Person p;
public WalkCommand(Person p){
this.p = p;
}
public void exec(CommandResult result) {
result.doCommand(this);
}
public void doCommand() throws FailedCommandException {
p.walk();
}
public Person getPerson() {
return p;
}
}
/**本来要先写个接口,再写个实现类,但在这里采用简单一点方式实现 */
public class CommandListener {
public void onAddFailureCommand(SingleCommand c,Throwable e){
System.out.println(e.getMessage());
}
public void onAddSuccessCommand(SingleCommand c){
System.out.println(c.getPerson().getName()+"得到小红花");
}
}
public class CommandResult {
int failureCommands = 0;
int successCommands = 0;
CommandListener cl =new CommandListener();
public void fireSuccessCommand(SingleCommand c){
cl.onAddSuccessCommand(c);
}
public void fireFailedCommand(SingleCommand c,Throwable t){
cl.onAddFailureCommand(c,t);
}
public void doCommand(SingleCommand c){
try {
c.doCommand();
successCommands++;
fireSuccessCommand(c);
} catch (FailedCommandException e) {
failureCommands++;
fireFailedCommand(c,e);
return;
}
}
public int getFailureCommands() {
return failureCommands;
}
public int getSuccessCommands() {
return successCommands;
}
}
public class MainTest {
public static void main(String[] args){
Person zhangSan =new Person("张三");
Command c1 = new SitDownCommand(zhangSan);
Command c2 = new WalkCommand(zhangSan);
Command c3 = new BackwardCommand(zhangSan);
CommandSuite cs =new CommandSuite();
cs.addCommand(c1);
cs.addCommand(c2);
cs.addCommand(c3);
cs.addCommand(c2);
cs.addCommand(c1);
cs.addCommand(c2);
cs.addCommand(c3);
cs.addCommand(c2);
cs.addCommand(c1);
cs.addCommand(c2);
cs.addCommand(c3);
cs.addCommand(c2);
cs.addCommand(c1);
cs.addCommand(c2);
cs.addCommand(c3);
cs.addCommand(new PauseCommand(c2));
cs.addCommand(c3);
cs.addCommand(new RepeatedCommand(c2));
CommandResult cr =new CommandResult();
PauseCommand cc = new PauseCommand(cs) ;
cc.exec(cr);
System.out.println("failure:"+cr.getFailureCommands());
System.out.println("success:"+cr.getSuccessCommands());
}
}