Delphi design mode: "Headfirst design Mode" Delphi Code---Mode Summary of a call interface and a few ducks [turn]

Source: Internet
Author: User

One and a few ducks.

1. Start with a barking interface.


1
2{"Headfirst design mode" Delphi Code pattern Summary}
3{a Barking interface}
4{compiler tool: Delphi2010 for Win32}
5{e-mail: [Email protected]}
6
7unit uquackable;
8
9interface
10
11type
Iquackable = Interface (iinterface)
Procedure Quack;
The end;
15
16implementation
17
18end.
+

2, a few ducks realize the call interface.


1
2{"Headfirst design mode" Delphi Code pattern Summary}
3{several ducks realized the sound interface}
4{compiler tool: Delphi2010 for Win32}
5{e-mail: [Email protected]}
6
7unit Uduck;
8
9interface
10
11uses
uquackable;
13
14type
Tmallardduck = Class (Tinterfacedobject, Iquackable)
Public
Procedure Quack;
The end;
19
Tredheadduck = Class (Tinterfacedobject, Iquackable)
Public
Procedure Quack;
The end;
24
Tduckcall = Class (Tinterfacedobject, Iquackable)
Public
Procedure Quack;
The end;
29
Trubberduck = Class (Tinterfacedobject, Iquackable)
to public
Procedure Quack;
The end;
34
35implementation
36
37{Tmallardduck}
38
39procedure Tmallardduck.quack;
40begin
Writeln (' quack ');
42end;
43
44{Tredheadduck}
45
46procedure Tredheadduck.quack;
47begin
Writeln (' quack ');
49end;
50
51{Tduckcall}
52
53procedure Tduckcall.quack;
54begin
Writeln (' Kwak ');
56end;
57
58{Trubberduck}
59
60procedure Trubberduck.quack;
61begin
Writeln (' Squeak ');
63end;
64
65end.

3, the call interface for the duck to provide services. The Barking interface is more closely related to the canard than to its implementation class.


1
2{"Headfirst design mode" Delphi Code pattern Summary}
3{duck-Naruto Simulator}
4{compiler tool: Delphi2010 for Win32}
5{e-mail: [Email protected]}
6
7unit Uducksimulator;
8
9interface
10
11uses
Uquackable, Uduck;
13
14type
Tducksimulator = Class (TObject)
Private
Procedure simulate (aduck:iquackable); overload;
Public
Procedure simulate; overload;
The end;
21st
22implementation
23
24{Tducksimulator}
25
26procedure tducksimulator.simulate (aduck:iquackable); {given to polymorphic processing}
27begin
Aduck.quack;
29end;
30
31procedure tducksimulator.simulate;
32var
amallardduck:iquackable;
aredheadduck:iquackable;
aduckcall:iquackable;
arubberduck:iquackable;
37begin
Try
39 {Create a Duck instance}
Amallardduck: = tmallardduck.create;
Aredheadduck: = tredheadduck.create;
Aduckcall: = tduckcall.create;
Arubberduck: = trubberduck.create;
44
Writeln (' Duck Simulator ');
46
47 {simulated Duck beep}
Simulate (Amallardduck);
Simulate (Aredheadduck);
Simulate (Aduckcall);
Wuyi simulate (Arubberduck);
Finally
Amallardduck: = nil;
Aredheadduck: = nil;
Aduckcall: = nil;
Arubberduck: = nil;
The end;
58end;
59
60end.

4, the client.


1
2{"Headfirst design mode" Delphi Code pattern Summary}
3{Client}
4{compiler tool: Delphi2010 for Win32}
5{e-mail: [Email protected]}
6
7program Pducksimulator;
8
9{$APPTYPE CONSOLE}
10
11uses
Uduck in ' Uduck.pas ',
Uducksimulator in ' Uducksimulator.pas ',
Uquackable in ' Uquackable.pas ';
15
16var
Asimulator:tducksimulator;
18
19begin
Asimulator: = tducksimulator.create;
21st
Asimulator.simulate;
23
Asimulator.free;
25
READLN;
27end.

5. Operation Result:

Delphi design mode: "Headfirst design Mode" Delphi Code---Mode Summary of a call interface and a few ducks [turn]

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.