王朝网络
分享
 
 
 

Command模式实现的redo/undo功能(以前在闪吧发过)

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

记得对设计模式描述得最好的就是csdn上面的一篇《追mm和设计模式》的帖子

下面是其中对Command模式的描述:

Command—俺有一个mm家里管得特别严,没法见面,只好借助于她弟弟在我们俩之间

传送信息,她对我有什么指示,就写一张纸条让她弟弟带给我。这不,她弟弟又传送过来

一个command,为了感谢他,我请他吃了碗杂酱面,哪知道他说:“我同时给我姐姐三个

男朋友送command,就数你最小气,才请我吃面。”,

命令模式:命令模式把一个请求或者操作封装到一个对象中。命令模式把发出命令的责任

和执行命令的责任分割开,委派给不同的对象。命令模式允许请求的一方和发送的一方独

立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收

,以及操作是否执行,何时被执行以及是怎么被执行的。系统支持命令的撤消。

这是Command模式的UML图

pre {font-family:"Courier New", Courier, Arial; font-size: 12px;}

.operator {color: #000000;}

.keyword {color: #993300;}

.identifier {color: #000087;}

.properties {color: #000087;}

.identifier2 {color : #000087;}

.linecomment, .blockcomment {color: #808080;}

.string {color: #0000FF;}

class Command

{

private function Command()

{

}

// Methods

public function Execute():Void

{

}

public function UnExecute():Void

{

}

}

// "ConcreteCommand"

class CalculatorCommand extends Command

{

// Fields

private var $operator:String;

private var operand:Number;

private var calculator:Calculator;

// Constructor

public function CalculatorCommand(calculator:Calculator, $operator:String, operand:Number)

{

this.calculator = calculator;

this.$operator = $operator;

this.operand = operand;

}

// Properties

public function set Operator(value:String):Void

{

$operator = value;

}

public function set Operand(value:Number):Void

{

operand = value;

}

// Methods

public function Execute():Void

{

calculator.Operation($operator, operand);

}

public function UnExecute():Void

{

calculator.Operation(Undo($operator), operand);

}

// Private helper function

private function Undo($operator:String):String

{

var undo:String ="";

switch ($operator)

{

case "+" :

undo = "-";

break;

case "-" :

undo = "+";

break;

case "*" :

undo = "/";

break;

case "/" :

undo = "*";

break;

}

return undo;

}

}

// "Receiver"

class Calculator

{

// Fields

private var total:Number = 0;

// Methods

public function Operation($operator:String, operand:Number):Void

{

switch ($operator)

{

case "+" :

total += operand;

break;

case "-" :

total -= operand;

break;

case "*" :

total *= operand;

break;

case "/" :

total /= operand;

break;

}

trace("Total =" + total + " (following" + $operator + " " + operand + ")");

}

}

// "Invoker"

class User

{

// Fields

private var calculator:Calculator;

private var commands:Array = new Array();

private var current:Number = 0;

public function User()

{

calculator = new Calculator();

}

// Methods

public function Redo(levels:Number):Void

{

//trace(commands[current++] instanceof Command);

trace("---- Redo " + levels + " levels ");

// Perform redo operations

for (var i = 0; i < levels; i++)

{

if (current < commands.length - 1)

{

commands[current++].Execute();

}

}

}

public function Undo(levels:Number):Void

{

trace("---- Undo" + levels + " levels ");

// Perform undo operations

for (var i = 0; i < levels; i++)

{

if (current > 0)

{

commands[--current].UnExecute();

}

}

}

public function Compute($operator:String, operand:Number):Void

{

// Create command operation and execute it

var command:Command = new CalculatorCommand(calculator, $operator, operand);

command.Execute();

// Add command to undo list

commands.push(command);

current++;

}

}

client.fla

// Create user and let her compute

var user:User = new User();

user.Compute('+', 100);

user.Compute('-', 50);

user.Compute('*', 10);

user.Compute('/', 2);

// Undo and then redo some commands

user.Undo(4);

user.Redo(3);

output:

Total =100 (following+ 100)

Total =50 (following- 50)

Total =500 (following* 10)

Total =250 (following/ 2)

---- Undo4 levels

Total =500 (following* 2)

Total =50 (following/ 10)

Total =100 (following+ 50)

Total =0 (following- 100)

---- Redo 3 levels

Total =100 (following+ 100)

Total =50 (following- 50)

Total =500 (following* 10)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有