什么是非侵入式设计?

一个客户端的代码可能包含框架功能和客户端自己的功能。

侵入式设计,就是设计者将框架功能“推”给客户端,而非侵入式设计,则是设计者将客户端的功能“拿”到框架中用。

侵入式设计有时候表现为客户端需要继承框架中的类,而非侵入式设计则表现为客户端实现框架提供的接口。

侵入式设计带来的最大缺陷是,当你决定重构你的代码时,发现之前写过的代码只能扔掉。而非侵入式设计则不然,之前写过的代码仍有价值。

struts1的设计是侵入式的:

[java] view plain  copy   public class loginAction extends Action{

public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws ActionException{

LoginForm loginForm = (LoginForm)form;

if(“scott”.equals(loginForm.getUsername() &&”tiger”.equals(loginForm.getPassword)))

{ return mapping.findForward(“success”);}

else{

return mapping.findForward(“failure”);}

}

}

而webwork的设计则是非侵入的:

[java] view plain  copy   public class LoginAction implements Action{

private final static String LOGINFAIL = “loginfail”;

private final static String SUCCESS = “success”;

private String passward;

private String username;

public String getPassword(){

return password;

}

public void setPassword(String password){

this.password = password;

}

public String getUsername(){

return username;

}

public void setUsername(String username){

this.username= username;

}

public String execute() throws Exception{

if(“yeeku”.equalsIgnoreCase(getUsername())&& “password”.equals(getPassword)){

ActionContext ctx= ActionContext.getContext();

Map session = ctx.getSession();

session.put(“username”,getUsername());

return SUCCESS;

}

else return LOGINFAIL;

}

}

原文地址:https://www.cnblogs.com/heartstage/p/3365413.html