jasu's blog
블로그 메뉴글
Command
Programming/Design Patterns
2007. 2. 21. 11:08
1 interface Command{
2 public function execute();
3 }
///////////////////////////////
1 class Light{
2 private var name:String;
3 public function Light(name:String){
4 this.name = name;
5 }
6 public function lightOn():Void{
7 trace(name+" : 불을 켜다");
8 }
9 public function lightOff():Void{
10 trace(name+" : 불을 끄다");
11 }
12 }
/////////////////////////////////
1 class LightOffCommand implements Command{
2 private var light:Light;
3
4 public function LightOffCommand(light:Light){
5 this.light = light;
6 }
7 public function execute(){
8 light.lightOff();
9 }
10 }
////////////////////////////////////
1 class LightOnCommand implements Command{
2 private var light:Light;
3
4 public function LightOnCommand(light:Light){
5 this.light = light;
6 }
7 public function execute(){
8 light.lightOn();
9 }
10 }
/////////////////////////////////////
1 class RemoteControl{
2 private var onCommands:Array;
3 private var offCommands:Array;
4
5 public function RemoteControl(){
6 onCommands = new Array();
7 offCommands = new Array();
8 }
9 public function setCommand(slot:Number, onCommand:LightOnCommand, offCommand:LightOffCommand):Void{
10 onCommands[slot] = onCommand;
11 offCommands[slot] = offCommand;
12 }
13 public function onButtonWasPressed(slot:Number):Void{
14 onCommands[slot].execute();
15 }
16 public function offButtonWasPressed(slot:Number):Void{
17 offCommands[slot].execute();
18 }
19 }
//////////////////////////////////////
1 class RemoteControlTest
2 {
3 public function RemoteControlTest ()
4 {
5 init ();
6 }
7 public function init () : Void
8 {
9 var remote : RemoteControl = new RemoteControl ();
10 var light : Light = new Light ("스텐드");
11 var lightOn : LightOnCommand = new LightOnCommand (light);
12 var lightOff : LightOffCommand = new LightOffCommand (light);
13 remote.setCommand (0, lightOn, lightOff);
14 remote.onButtonWasPressed (0);
15 remote.offButtonWasPressed (0);
16 }
17 }
2 public function execute();
3 }
///////////////////////////////
1 class Light{
2 private var name:String;
3 public function Light(name:String){
4 this.name = name;
5 }
6 public function lightOn():Void{
7 trace(name+" : 불을 켜다");
8 }
9 public function lightOff():Void{
10 trace(name+" : 불을 끄다");
11 }
12 }
/////////////////////////////////
1 class LightOffCommand implements Command{
2 private var light:Light;
3
4 public function LightOffCommand(light:Light){
5 this.light = light;
6 }
7 public function execute(){
8 light.lightOff();
9 }
10 }
////////////////////////////////////
1 class LightOnCommand implements Command{
2 private var light:Light;
3
4 public function LightOnCommand(light:Light){
5 this.light = light;
6 }
7 public function execute(){
8 light.lightOn();
9 }
10 }
/////////////////////////////////////
1 class RemoteControl{
2 private var onCommands:Array;
3 private var offCommands:Array;
4
5 public function RemoteControl(){
6 onCommands = new Array();
7 offCommands = new Array();
8 }
9 public function setCommand(slot:Number, onCommand:LightOnCommand, offCommand:LightOffCommand):Void{
10 onCommands[slot] = onCommand;
11 offCommands[slot] = offCommand;
12 }
13 public function onButtonWasPressed(slot:Number):Void{
14 onCommands[slot].execute();
15 }
16 public function offButtonWasPressed(slot:Number):Void{
17 offCommands[slot].execute();
18 }
19 }
//////////////////////////////////////
1 class RemoteControlTest
2 {
3 public function RemoteControlTest ()
4 {
5 init ();
6 }
7 public function init () : Void
8 {
9 var remote : RemoteControl = new RemoteControl ();
10 var light : Light = new Light ("스텐드");
11 var lightOn : LightOnCommand = new LightOnCommand (light);
12 var lightOff : LightOffCommand = new LightOffCommand (light);
13 remote.setCommand (0, lightOn, lightOff);
14 remote.onButtonWasPressed (0);
15 remote.offButtonWasPressed (0);
16 }
17 }