现在的位置: 首页 > 综合 > 正文

VCL类学习之(二) TBasicActionLink

2017年12月01日 ⁄ 综合 ⁄ 共 2058字 ⁄ 字号 评论关闭
  1. Unit Classes;
  2. TBasicActionLink = class(TObject)
  3.   private
  4.     FOnChange: TNotifyEvent;
  5.   protected
  6.     FAction: TBasicAction;
  7.     procedure AssignClient(AClient: TObject); virtual;
  8.     procedure Change; virtual;
  9.     function IsOnExecuteLinked: Boolean; virtual;
  10.     procedure SetAction(Value: TBasicAction); virtual;
  11.     procedure SetOnExecute(Value: TNotifyEvent); virtual;
  12.   public
  13.     constructor Create(AClient: TObject); virtual;
  14.     destructor Destroy; override;
  15.     function Execute(AComponent: TComponent = nil): Boolean; virtual;
  16.     function Update: Boolean; virtual;
  17.     property Action: TBasicAction read FAction write SetAction;
  18.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  19.   end;
  20.   TBasicActionLinkClass = class of TBasicActionLink;
  21. { TBasicActionLink }
  22. constructor TBasicActionLink.Create(AClient: TObject);
  23. begin
  24.   inherited Create;
  25.   AssignClient(AClient);
  26. end;
  27. procedure TBasicActionLink.AssignClient(AClient: TObject);
  28. begin
  29. end;
  30. destructor TBasicActionLink.Destroy;
  31. begin
  32.   if FAction <> nil then FAction.UnRegisterChanges(Self);
  33.   inherited Destroy;
  34. end;
  35. procedure TBasicActionLink.Change;
  36. begin
  37.   if Assigned(OnChange) then OnChange(FAction);
  38. end;
  39. function TBasicActionLink.Execute(AComponent: TComponent): Boolean;
  40. begin
  41.   FAction.ActionComponent := AComponent;
  42.   Result := FAction.Execute;
  43. end;
  44. procedure TBasicActionLink.SetAction(Value: TBasicAction);
  45. begin
  46.   if Value <> FAction then
  47.   begin
  48.     if FAction <> nil then FAction.UnRegisterChanges(Self);
  49.     FAction := Value;
  50.     if Value <> nil then Value.RegisterChanges(Self);
  51.   end;
  52. end;
  53. function TBasicActionLink.IsOnExecuteLinked: Boolean;
  54. begin
  55.   Result := True;
  56. end;
  57. procedure TBasicActionLink.SetOnExecute(Value: TNotifyEvent);
  58. begin
  59. end;
  60. function TBasicActionLink.Update: Boolean;
  61. begin
  62.   Result := FAction.Update;
  63. end;
  64.  在上面的代码中我们可以看到TBasicActionLink的Execute方法实际上也就是调用了TBasicAction对象的虚方法Execute来负责响应客户端的请求。
  65. 应用举例

  66.  

        在通常的UI设计中,我们会在Form上放置一些菜单项,同时会把部分使用频率较高的功能以工具栏形式提供给用户。这些工具栏按钮实现的功能与菜单项完全相同,我们就可以使用Action模式来设计这些请求,然后将菜单项和工具栏按钮与这些Action对象对立关联即可。即使以后在用户界面上增加其它形式的调用,如上下文菜单,或是快捷键等,都可以直接与这些请求对象建立关联。可以很轻松地扩充用户发出请求的方式。

  67.  

 

抱歉!评论已关闭.