zoukankan      html  css  js  c++  java
  • 在Delphi中使用Action降低水平功能和业务功能的耦合

    权限和日志管理是较为常见的水平功能,而且需求比较灵活,通常硬编码到程序中。
    本文将对Delphi中的Action进行扩充实现将权限和日志管理功能从主程序中分离。
    1.  
    常见的方法是将权限管理和日志管理直接硬编码到过程或者对象的方法中。
    例如:
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if isSuperUser(UserName) then
      begin
        Close;
        addLog(UserName,'Close');
      end
      else
        showMessage('You have not this right');
    end;
    上面的代码将权限管理和日志管理硬编码到程序中,不利于程序的重用和扩展。
    2.  
    下面是扩展的一个Action类Form1CloseAction,它封装了Close方法
      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;
    在Execute方法中完成用户的逻辑业务,这样可以很好的将应用程序的水平功能和垂直功能相分离。
    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;
    注意到Form1CloseActiony有一个Form属性作为业务逻辑对象,所以在调用Execute前要初始化 Form属性。
    这样我们只要动态创建一个 TForm1CloseAction对象CloseAction 并指定Button1.Action:=CloseAction。

    3.  很明显通常一个业务逻辑对象有很多方法,如果按照上一步封装为Action的话,有很多重复编码。于是我们在TForm1CloseAction 和 Tacton之间插入一个类,并为这个类引入一个新的方法InternalExecute来封装用户的业务逻辑方法,使用Execute来完成水平功能。
      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;
    关键的方法Execute如下实现
    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;

    这样原来的TForm1CloseAction 改为
      TForm1CloseAction = class(TForm1Action)
      protectec
        procedure InternalExecute ; override ;
      public
        constructor Create(AOwner:TComponent) ;override ;
      end;

      为简便起见我使用Form来表示业务逻辑对象,而实际的业务逻辑对象最好是于界面无关的,上述方法可以看作是MVC的子模式或实现。上面的模型还有很多可以扩展的方面,例如:将HaveRight,AddLog方法分别由权限管理类和日志管理类来实现权限信息和日志信息的持久化。这样Action就像粘合剂将业务逻辑,权限管理,日志管理整合在一起。

    引用至:http://blog.csdn.net/wqsmiling/
  • 相关阅读:
    调试常用命令
    android获取手机机型、厂商、deviceID基本信息
    融云即时通讯 添加地理位置信息的功能
    Linux centOS下搭建RTMP服务器的具体步骤
    数组与字符串 1.4
    数组与字符串 1.5
    数组与字符串 1.3
    数组与字符串 1.2
    数组与字符串 1.1
    笔记本自开wifi设置
  • 原文地址:https://www.cnblogs.com/sonicit/p/840268.html
Copyright © 2011-2022 走看看