在Delphi中使用Action降低水平功能和业务功能的耦合
点击次数:64 次 发布日期:2008-11-09 08:35:52 作者:源代码网
|
权限和日志管理是较为常见的水平功能,而且需求比较灵活,通常硬编码到程序中。 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 改为 软件开发网 www.mscto.com TForm1CloseAction = class(TForm1Action) protectec procedure InternalExecute ; override ; public constructor Create(AOwner:TComponent) ;override ; end; 为简便起见我使用Form来表示业务逻辑对象,而实际的业务逻辑对象最好是于界面无关的,上述方法可以看作是MVC的子模式或实现。上面的模型还有很多可以扩展的方面,例如:将HaveRight,AddLog方法分别由权限管理类和日志管理类来实现权限信息和日志信息的持久化。这样Action就像粘合剂将业务逻辑,权限管理,日志管理整合在一起。 源代码网推荐 源代码网供稿. |
