Using action in Delphi to reduce coupling between horizontal and business functions

Source: Internet
Author: User
Permission and log management are common horizontal functions with flexible requirements. Program .
This article will expand the action in Delphi to separate the permission and log management functions from the main program.
1.
A common method is to hard-code permission management and log management to a process or object.
For example:
Procedure tform1.button1click (Sender: tobject );
Begin
If issuperuser (username) then
Begin
Close;
Addlog (username, 'close ');
End
Else
Showmessage ('you have not this right ');
End;
The above Code Permission management and log management are hardcoded into a program, which is not conducive to program reuse and expansion.
2.
The following is an extended action class form1closeaction, which encapsulates the close method.
Tform1closeaction = Class (taction)
Private
Foprname: string;
Fusername: string;
Fform: tform1;
Procedure setform (const value: tform1 );
Procedure setoprname (const value: string );
Procedure setusername (const value: string );
Public
Function execute: Boolean; override;
Constructor create (aowner: tcomponent); override;
Published
Property Username: String read fusername write setusername;
Property oprname: String read foprname write setoprname;
Property Form: tform1 read fform write setform;
End;
Complete the user's logical business in the execute method, so that the horizontal and vertical functions of the application can be well separated.
Function tform1closeaction. Execute: Boolean;
Begin
Result: = inherited execute;
If haveright (username, oprname) then
Begin
Form. close;
Addlog (username, 'close ');
End
Else begin
Showmessage ('you have not this right ');
End;
End;
Note that form1closeactiony has a form attribute as the business logic object, so you must initialize the form Attribute before calling execute.
In this way, we only need to dynamically create a tform1closeaction object closeaction and specify button1.action: = closeaction.

3. Obviously, a business logic Object usually has many methods. If it is encapsulated as an action in the previous step, there are many repeated codes. Therefore, we insert a class between tform1closeaction and tacton, introduce a new method internalexecute for this class to encapsulate the user's business logic method, and execute the horizontal function.
Tform1action = Class (taction)
Private
Foprname: string;
Fusername: string;
Fform: tform1;
Procedure setform1 (const value: tform1 );
Procedure setoprname (const value: string );
Procedure setusername (const value: string );
Protected
Procedure internalexecute; virtual; abstract;
Public
Function execute: Boolean; override;
Constructor create (aowner: tcomponent); override;
Published
Property Username: String read fusername write setusername;
Property oprname: String read foprname write setoprname;
Property Form: tform1 read fform write setform1;
End;
The key method is execute as follows:
Function tform1action. Execute: Boolean;
Begin
Result: = inherited execute;
If haveright (username, oprname) then
Begin
Self. internalexecute;
End
Else begin
Showmessage ('you have not this right ');
End;
Addlog (username, 'close ');

End;

In this way, the original tform1closeaction is changed
Tform1closeaction = Class (tform1action)
Protectec
Procedure internalexecute; override;
Public
Constructor create (aowner: tcomponent); override;
End;

For convenience, I use form to represent the business logic object. The actual business logic object is preferably interface-independent. The above method can be seen as the sub-mode or implementation of MVC. The above model also has many aspects that can be extended. For example, the haveright and addlog methods are used by the permission management class and log management class respectively to realize the persistence of permission information and log information. In this way, action is like a combination of business logic, permission management, and log management.

Reference:Http://blog.csdn.net/wqsmiling/

Related Article

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.