In the interface-oriented design, I am using the framework mode

Source: Internet
Author: User
In the future, more and more interfaces and structures will be designed in Delphi, because it is too convenient.

System. regularexpressions is a good demonstration of the structure-based design. However, it is more appropriate to use interfaces.

Someone else has used the interface for writing.ProgramI started using the program from the beginning.

Now we have summarized four frameworks: 1. Direct implementation; 2. Indirect implementation (or inheritance implementation); 3. Overwrite implementation; 4. delegate implementation.

1. Direct implementation:

In the following example, tmy1 and tmy2 are used, but imy1 and imy2 are used in specific applications. This is what I call interface-oriented.
Tmy1 and tmy2 directly implement all the methods of the interface. This is what I call direct implementation.
This may causeCodeRepeated, but it is quite practical if the program is small.

Unit unit1; interfaceuses winapi. windows, winapi. messages, system. sysutils, system. variants, system. classes, VCL. graphics, VCL. controls, VCL. forms, VCL. dialogs; Type tform1 = Class (tform) Procedure formcreate (Sender: tobject); end; IA = interface procedure method_a; end; IB = interface (IA) Procedure method_ B; end; imy1 = interface (IB) Procedure method_my1; end; imy2 = interface (IB) Procedure method_my2; end; tmy1 = Class (category, imy1) Procedure method_a; Procedure method_ B; Procedure method_my1; end; tmy2 = Class (tinterfacedobject, imy2) Procedure method_a; Procedure method_ B; Procedure method_my2; end; var form1: tform1; implementation {$ R *. DFM} {tmy1} procedure tmy1.method _ A; begin showmessage ('A'); end; Procedure tmy1.method _ B; begin showmessage ('B'); end; Procedure tmy1.method _ my1; begin showmessage ('my1'); end; {tmy2} procedure tmy2.method _ A; begin showmessage ('A'); end; Procedure tmy2.method _ B; begin showmessage ('B'); end; Procedure tmy2.method _ my2; begin showmessage ('my2'); end; {test} procedure tform1.formcreate (Sender: tobject); var V1: imy1; V2: imy2; begin V1: = tmy1.create; v1.method _ A; v1.method _ B; v1.method _ my1; V2: = tmy2.create; v2.method _ A; v2.method _ B; v2.method _ my2; end.
Ii. Indirect implementation:

The following example uses an indirect TB class to avoid repeated code in tmy1 and tmy2.

Unit unit1; interfaceuses winapi. windows, winapi. messages, system. sysutils, system. variants, system. classes, VCL. graphics, VCL. controls, VCL. forms, VCL. dialogs; Type tform1 = Class (tform) Procedure formcreate (Sender: tobject); end; IA = interface procedure method_a; end; IB = interface (IA) Procedure method_ B; end; TB = Class (tinterfacedobject, IB) Procedure method_a; Procedure method_ B; end; imy1 = interface (IB) Procedure method_my1; end; imy2 = interface (IB) Procedure method_my2; end; tmy1 = Class (TB, imy1) Procedure method_my1; end; tmy2 = Class (TB, imy2) Procedure method_my2; end; var form1: tform1; implementation {$ R *. DFM} {TB} procedure TB. method_a; begin showmessage ('A'); end; Procedure TB. method_ B; begin showmessage ('B'); end; {tmy1} procedure tmy1.method _ my1; begin showmessage ('my1'); end; {tmy2} procedure tmy2.method _ my2; begin showmessage ('my2'); end; {test} procedure tform1.formcreate (Sender: tobject); var V1: imy1; V2: imy2; begin V1: = tmy1.create; v1.method _; v1.method _ B; v1.method _ my1; V2: = tmy2.create; v2.method _ A; v2.method _ B; v2.method _ my2; end.
Iii. Coverage implementation:

In the process of inheriting from TB, you can also achieve polymorphism by overwriting the virtual function. The following tmy2 will do the same.

Unit unit1; interfaceuses winapi. windows, winapi. messages, system. sysutils, system. variants, system. classes, VCL. graphics, VCL. controls, VCL. forms, VCL. dialogs; Type tform1 = Class (tform) Procedure formcreate (Sender: tobject); end; IA = interface procedure method_a; end; IB = interface (IA) Procedure method_ B; end; TB = Class (tinterfacedobject, IB) Procedure method_a; virtual; Procedure method_ B; virtual; end; imy1 = interface (IB) Procedure method_my1; end; imy2 = interface (IB) Procedure method_my2; end; tmy1 = Class (TB, imy1) Procedure method_my1; end; tmy2 = Class (TB, imy2) Procedure method_a; override; Procedure method_ B; override; Procedure method_my2; end; vaR form1: tform1; implementation {$ R *. DFM} {TB} procedure TB. method_a; begin showmessage ('A'); end; Procedure TB. method_ B; begin showmessage ('B'); end; {tmy1} procedure tmy1.method _ my1; begin showmessage ('my1'); end; {tmy2} procedure tmy2.method _; begin showmessage ('A _ my2'); end; Procedure tmy2.method _ B; begin showmessage ('B _ my2'); end; Procedure tmy2.method _ my2; begin showmessage ('my2'); end; {test} procedure tform1.formcreate (Sender: tobject); var V1: imy1; V2: imy2; begin V1: = tmy1.create; v1.method _; v1.method _ B; v1.method _ my1; V2: = tmy2.create; v2.method _ A; v2.method _ B; v2.method _ my2; end.
Iv. delegate implementation:

Methods In the interface must be implemented, but other implementations can also be borrowed (or delegated) using the implements keyword;
However, the official document said that this method only applies to win32. that is to say, this method does not work in win64 and other systems. What should I do?

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.