例子--安全检查.
假设希望检查用户是否有权限执行某个方法,如果没有权限就抛出一个异常,也就是说对要执行安全检查的代码进行方法拦截,以加
入检查代码.如果编程解决这个问题,OO无法给我们任何有效的帮助.得到的是这样的代码.
public class MyBusinessObject implements BusinessObject {
public void businessMethod1() throws UnauthorizedException {
doSecurityCheck();
// Do the work of method1
}
public void businessMethod2() throws UnauthorizedException {
doSecurityCheck();
// Do the work of method2
}
protected void doSecurityCheck() throws UnauthorizedException {
}
}
这个类有两个责任:主要责任是执行业务代码 Do the work of methodX.
次要责任是执行安全检查代码 doSecurityCheck.
OO可以尽可能减少重复的代码.如把安全检查代码归整到一个单独方法中去,但无法消除重复的代码,这要在每个业务方法中调用安
全检查方法.基于层级结构的OO对这种正交性的代码无法做到完全的消除重复代码.
Decorator模式:
//业务类
public class MyBusinessObject implements BusinessObject {
public void businessMethod1() throws UnauthorizedException {
// Do the work of method1
}
public void businessMethod2() throws UnauthorizedException {
// Do the work of method2
}
}
//装饰类
public abstract class BusinessObjectFilter implements BusinessObject {
private BusinessObject target;
public BusinessObjectFilter (BusinessObject obj) {
this.target = obj;
}
public void businessMethod1() throws UnauthorizedException {
this.target.businessMethod1();
}
public void businessMethod2() throws UnauthorizedException {
this.target.businessMethod2() ;
}
}
//组合业务操作和安全检查的类
public class RealBusinessObject extends BusinessObjectFilter {
public ReadBusinessObject(BusinessObject obj) {
super(obj);
}
//1.增加安全检查代码.
//2.一定要调用super.businessMethod1();否则这将不是一个增强功能的方法,而是在重写一个方法.
public void businessMethod1() throws UnauthorizedException {
doSecurityCheck();
super.businessMethod1();
}
public void businessMethod2() throws UnauthorizedException {
doSecurityCheck();
super.businessMethod2() ;
}
protected void doSecurityCheck() throws UnauthorizedException {
}
}
通过Decorator模式的使用,可以将两者的责任分离,(如果安全检查代码过于复杂,可以用Extract Class将代码抽离出去).
但仍有两个问题:1.要针对每一个业务对象都要写一个装饰类。强类型在这里没有多大用处,如果一个通用类能解决问题则是最恰当
的.
2.无论怎样分离代码,最后都要在一个地方将两者进行组装, 组装的动作发生在编译期。如果能在运行时组装则能
避免代码级别的耦合.利用AOP提供的方式则能做到这一点.
Observer模式.
将安全检查代码封装到接口中去,
//Observer
public interface SecurityCheck {
void doSecurityCheck() throws UnauthorizedException;
}
//Observable
public class MyBusinessObject implements BusinessObject{
private SecurityCheck securityCheck;
public void businessMethod1() throws UnauthorizedException {
securityCheck.doSecurityCheck();
// Do the work of method1
}
public void businessMethod2() throws UnauthorizedException {
securityCheck.doSecurityCheck();
// Do the work of method2
}
public void setSecurityCheck(SecurityCheck securityCheck) {
this.securityCheck = securityCheck;
}
}
安全检查作为业务方法的一个次要逻辑.但Observable要通知Observer,则代码必然会发生耦合.
普通的代理模式的代码结构与上面Decorator模式基本相同.
OO设计在此问题上只能尽可能减少重复代码,尽量分离责任,但最后仍会发生代码的耦合.
AOP的总结最大成果在于提出了一套完备的切入点理论,以及让被增强的类完全对增强一无所知-- 从前的大多数拦截技术都没有做到
这一点.以实现责任在代码级别的解耦.
Dynamic Proxy
//业务类
public class MyBusinessObject implements BusinessObject {
public void businessMethod1() throws UnauthorizedException {
// Do the work of method1
}
public void businessMethod2() throws UnauthorizedException {
// Do the work of method2
}
}
//安全检查接口
public class SecurityCheckImpl implements SecurityCheck {
public void doSecurityCheck() throws UnauthorizedException {
//doSecurityCheck
}
}
//组合业务类和安全检查的通用类
public class CommonSecurityCheck implements InvocationHandler {
private Object target;
private SecurityCheck securityCheck;
private CommonSecurityCheck(Object arg0,SecurityCheck arg1) {
this.target = arg0;
this.securityCheck = arg1;
}
public Object invoke(Object proxy,Method method,Object[] args) throws Throwable {
this.securityCheck.doSecurityCheck();
Object result = method.invoke(this.target,args);
return result;
}
}
某一业务类与安全检查代码在运行时进行组装.
分享到:
相关推荐
根据https://github.com/Akateason/XTTableDatasourceDelegateSeparation,加入了阳神的cell自适应高度代码,MJExtension,MJExtension,AFNetworking等常用开发框架,主要用于分离控制器中的代码,降低代码耦合程度...
代码 复杂网络最近邻耦合网络程序代码 复杂网络最近邻耦合网络程序代码 复杂网络最近邻耦合网络程序代码 复杂网络最近邻耦合网络程序代码 复杂网络最近邻耦合网络程序代码 复杂网络最近邻耦合网络程序代码 复杂网络...
matlab对耦合模理论的仿真代码,可以运行。
车桥耦合的求解matlab代码,即梁上面有移动载荷的数值模拟
preCICE(精确代码交互耦合环境)是用于分区多物理场模拟的耦合库,包括但不限于流体 - 结构相互作用和共轭传热模拟。 分区意味着preCICE耦合能够模拟模拟中涉及的完整物理的子部分的现有程序(求解器)。 这允许为...
公交网络邻接矩阵计算;基于公交网络的耦合映像格子算法实现;突加扰动后网络的级联失效节点比例计算;2000节点,1000时间步运行时间20分钟;C#代码实现;
rcwa-2d 严格耦合波方法matlab代码
代码加入了cell自适应高度代码,代码实现已经进行封装,配合MJExtension,MJExtension,AFNetworking等常用开发框架使用更佳,主要用于分离控制器中的代码,降低代码耦合程度,可以根据自己使用习惯调整代码。...
复杂网络最近邻耦合网络程序matlab代码.zip
分离holder的低耦合度的adapter
介绍了基于PV技术的耦合工艺从稀水溶液中回收有机物的优势,回顾了渗透汽化与其它工艺耦合过程的发展历程,特别是渗透气化-分凝(PVFC)耦合工艺从稀溶液中高效分离回收高纯度高沸点有机物的研究进展。蒸汽渗透(VP)与PV...
大数据-算法-三维可压缩流动的非定常分离及飞行运动流动耦合的非线性动态稳定性.pdf
毫米波雷达天线耦合的影响与消除博文相对应的代码和数据,包括数据和代码,为防止乱码代码都备份了txt格式。
matlab车桥耦合,计算分析,便于自学
利用MATLAB程序来计算交叉耦合系数,
利用移动热源模拟激光热成型的 热-结构 耦合分析 apdl代码
光电子课程设计的优秀文档,关于光纤耦合器的耦合比与耦合区长度关系的仿真。
我们提出对规范耦合统一尺度的解释,该尺度与任何新的粒子阈值均无关。 我们重新审视了大统一理论,并表明可以从粒子物理光谱中完全消除标量和矢量变色夸克。 结果,在我们的方法中,轨距等级问题基于不同的理由,...
光纤耦合仿真MATLAB程序包,可以仿真不同芯径的光纤模式分布,可以仿真光纤的耦合损耗。
AOP 解决紧密耦合的难题