Command模式

最近学习模式入迷, 所以就想写一篇关于模式的文章,这篇文章是<

  1<java 与模式="">&gt; (阎宏 著)里的一个例子, 我把它改成Delphi的.第一次写东西, 有不足之处希望大家可以谅解. 
  2
  3这个例子还是比较好理解的, 所以只给出代码. 
  4
  5unit pattern; 
  6
  7interface 
  8
  9uses Dialogs; 
 10
 11type   
 12TAudioPlayer = class; 
 13
 14TCommand = class   
 15public   
 16procedure execute; virtual; abstract;   
 17end; 
 18
 19TPlayCommand = class(TCommand)   
 20private   
 21AudioPlayer: TAudioPlayer;   
 22public   
 23procedure execute; override;   
 24procedure Playcommand(AP: TAudioPlayer);   
 25end; 
 26
 27TStopCommand = class(TCommand)   
 28private   
 29AudioPlayer: TAudioPlayer;   
 30public   
 31procedure execute; override;   
 32procedure StopComman(AP: TAudioPlayer);   
 33end; 
 34
 35TRewindCommand = class(TCommand)   
 36private   
 37AudioPlayer: TAudioPlayer;   
 38public   
 39procedure execute; override;   
 40procedure RewindCommand(AP: TAudioPlayer);   
 41end; 
 42
 43TKeyPad = class   
 44private   
 45PlayCommand: TCommand;   
 46StopCommand: TCommand;   
 47RewindCommand: TCommand;   
 48public   
 49constructor Create(PlayC, StopC, RewindC: TCommand); virtual;   
 50procedure play();   
 51procedure stop();   
 52procedure rewind();   
 53end; 
 54
 55TAudioPlayer = class   
 56public   
 57procedure play();   
 58procedure stop();   
 59procedure rewind();   
 60end; 
 61
 62TClient = class   
 63private   
 64KeyPad: TKeyPad;   
 65AudioPlayer: TAudioPlayer;   
 66public   
 67constructor Create();   
 68procedure test();   
 69end; 
 70
 71implementation 
 72
 73{ TKeyPad } 
 74
 75constructor TKeyPad.Create(PlayC, StopC, RewindC: TCommand);   
 76begin   
 77PlayCommand := PlayC;   
 78StopCommand := StopC;   
 79RewindCommand := RewindC;   
 80end; 
 81
 82procedure TKeyPad.play;   
 83begin   
 84PlayCommand.execute;   
 85end; 
 86
 87procedure TKeyPad.rewind;   
 88begin   
 89RewindCommand.execute;   
 90end; 
 91
 92procedure TKeyPad.stop;   
 93begin   
 94StopCommand.execute;   
 95end; 
 96
 97{ TAudioPlayer } 
 98
 99procedure TAudioPlayer.play;   
100begin   
101ShowMessage('play');   
102end; 
103
104procedure TAudioPlayer.rewind;   
105begin   
106ShowMessage('rewind');   
107end; 
108
109procedure TAudioPlayer.stop;   
110begin   
111ShowMessage('stop');   
112end; 
113
114{ TPlayCommand } 
115
116procedure TPlayCommand.execute;   
117begin   
118inherited;   
119AudioPlayer.play();   
120end; 
121
122procedure TPlayCommand.Playcommand(AP: TAudioPlayer);   
123begin   
124self.AudioPlayer := AP;   
125end; 
126
127{ TRewindCommand } 
128
129procedure TRewindCommand.execute;   
130begin   
131inherited;   
132AudioPlayer.Rewind;   
133end; 
134
135procedure TRewindCommand.RewindCommand(AP: TAudioPlayer);   
136begin   
137AudioPlayer := ap;   
138end; 
139
140{ TStopCommand } 
141
142procedure TStopCommand.execute;   
143begin   
144inherited;   
145AudioPlayer.Stop;   
146end; 
147
148procedure TStopCommand.StopComman(AP: TAudioPlayer);   
149begin   
150AudioPlayer := ap;   
151end; 
152
153{ TClient } 
154
155constructor TClient.Create;   
156begin   
157AudioPlayer := TAudioPlayer.Create();   
158end; 
159
160procedure TClient.test;   
161var   
162PlayCommand: TCommand;   
163StopCommand: TCommand;   
164RewindCommand: TCommand;   
165begin   
166PlayCommand := TPlayCommand.Create;   
167StopCommand := TStopCommand.Create;   
168RewindCommand := TRewindCommand.Create;   
169KeyPad := TKeyPad.Create(PlayCommand, StopCommand, RewindCommand);   
170KeyPad.stop;   
171KeyPad.play;   
172KeyPad.rewind;   
173KeyPad.Stop;   
174end; 
175
176end.</java>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus