AOP思想实例讲解

 

假设有一个系统,需要权限控制,按 oo 的方法,做个 SecurityManager, just like this:

public class SecurityManager{

public bool checkPower(User user, AcessModel am){

///here to judge user if had access to system

return true;

}

}

然后在业务逻辑中进行如下调用:

public class BusinessClass{

public void businessMethod(){

SecurityManager sm= new SecurityManager();

if(sm.checkPower(user,am)){

///you had past the check

….

}

}

}

这就造成系统的权限校验与业务逻辑掺杂一起,耦合增加,使程序的鲁棒性大打折扣。。。

但如果我们用aop则会将其彻底改观,对于websharp,可以做如下更改:

public class BusinessClass AspectObject{

 

/* 继承AspectObject,去掉businessMethod()中的:

 SecurityManager sm= new SecurityManager();

if(sm.checkPower(user,am)){

///you had past the check

….

}

*/

 }

 

public class SecurityManager IAspect{

///implement IAspect interface by execute method as followed:

public void execute(Object[] paramList){

///here to judge user if had access to system

}

}

最后在配置文件中添加:

<Websharp-Aspects>

<Aspect type=”pack.SecurityManager,pack” deploy-model=”Singleton” pointcut-type=”Method” action-position=”before” match=”*,*”/>

</Websharp-Aspects >

以上你就已实现一个aop! 这样Security Business就独立了。。。感觉websharp里对AOP的概念可能局限

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值