Delphi design mode: "Headfirst design mode" DELPHI7 Code---Command mode simpleremotewithundotest[]

Source: Internet
Author: User

Command mode makes it easy to implement undo functionality.

Recipients of the command:


1unit Ureceiveobject;
2
3interface
4
5type
6 tlight = Class (TObject)
7 Public
8 procedure Open;
9 procedure Off;
Ten end;
11
12implementation
13
14{Tlight}
15
16procedure Tlight.off;
17begin
Writeln (' light is off. ');
19end;
20
21procedure Tlight.open;
22begin
Writeln (' light was on. ');
24end;
25
26end.
-

Command object:


1unit Ucommandobject;
2
3interface
4
5uses
6 ureceiveobject;
7
8type
9 TCommand = Class (TObject)
Ten public
Procedure Execute; Virtual Abstract
Procedure Undo; Virtual Abstract
The end;
14
Tlightoncommand = Class (TCommand)
Private
Flight:tlight;
Public
Constructor Create (alight:tlight);
Procedure Execute; Override
Procedure Undo; Override
The end;
23
Tlightoffcommand = Class (TCommand)
Private
Flight:tlight;
Public
Constructor Create (alight:tlight);
Procedure Execute; Override
Procedure Undo; Override
The end;
32
33implementation
34
35{Tlightoncommand}
36
37constructor tlightoncommand.create (alight:tlight);
38begin
FLight: = alight;
40end;
41
42procedure Tlightoncommand.execute;
43begin
Flight.open;
45end;
46
47procedure Tlightoncommand.undo;
48begin
Flight.off;
50end;
51
52{Tlightoffcommand}
53
54constructor tlightoffcommand.create (alight:tlight);
55begin
FLight: = alight;
57end;
58
59procedure Tlightoffcommand.execute;
60begin
Flight.off;
62end;
63
64procedure Tlightoffcommand.undo;
65begin
Flight.open;
67end;
68
69end.
70

Requester of the command:


1unit Usimpleremotewithundo;
2
3interface
4
5uses
6 ucommandobject;
7
8type
9 Tsimpleremotewithundo = Class (TObject)
Private
One by one foncommand:tcommand;
Foffcommand:tcommand;
Fundocommand:tcommand;
Public
Procedure SetCommand (Aoncommand, Aoffcommand:tcommand);
Procedure onbuttonwaspressed;
Procedure offbuttonwaspressed;
Procedure undobuttonwaspressed;
The end;
20
21implementation
22
23{Tsimpleremotewithundo}
24
25procedure tsimpleremotewithundo.offbuttonwaspressed;
26begin
Foffcommand.execute;
Fundocommand: = Foffcommand;
29end;
30
31procedure tsimpleremotewithundo.onbuttonwaspressed;
32begin
Foncommand.execute;
Fundocommand: = Foncommand;
35end;
36
37procedure Tsimpleremotewithundo.setcommand (Aoncommand, Aoffcommand:tcommand);
38begin
Foncommand: = Aoncommand;
Foffcommand: = Aoffcommand;
41end;
42
43procedure tsimpleremotewithundo.undobuttonwaspressed;
44begin
Fundocommand.undo;
46end;
47
48end.
49

Client, create a specific command object:


1program psimpleremotewithundotest;
2
3{$APPTYPE CONSOLE}
4
5uses
6 Usimpleremotewithundo in ' Usimpleremotewithundo.pas ',
7 ucommandobject in ' Ucommandobject.pas ',
8 ureceiveobject in ' Ureceiveobject.pas ';
9
10var
One by one remote:tsimpleremotewithundo;
Light:tlight;
Lightoncommand:tcommand;
Lightoffcommand:tcommand;
15
16begin
+ Remote: = tsimpleremotewithundo.create;
18
Light: = Tlight.create;
20
Lightoncommand: = Tlightoncommand.create (light);
Lightoffcommand: = Tlightoffcommand.create (light);
23
Remote.setcommand (Lightoncommand, Lightoffcommand);
25
remote.onbuttonwaspressed;
remote.offbuttonwaspressed;
remote.undobuttonwaspressed;
Writeln;
remote.offbuttonwaspressed;
remote.onbuttonwaspressed;
remote.undobuttonwaspressed;
33
Remote.free;
Light.free;
Lightoncommand.free;
Panax Notoginseng Lightoffcommand.free;
38
READLN;
40end.
41

Operation Result:

Delphi design mode: "Headfirst design mode" DELPHI7 Code---Command mode simpleremotewithundotest[)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.