标签:
原文:http://blog.csdn.net/sunboard/article/details/3831823
一个软件设计的好坏,我想非常大程度上取决于它的总体架构,而这个总体架构事实上就是你对整个宏观商业业务的抽象框架,当代表业务逻辑的高层抽象层结构 合理时,你底层的详细实现须要考虑的就不过一些算法和一些详细的业务实现了。当你须要再开发还有一个相近的项目时,你曾经的抽象层说不定还能够再次利用 。面对对象的设计,复用的重点事实上应该是抽象层的复用,而不是详细某一个代码块的复用。
说到了抽象,我就不能不提到曾让我头痛的Java接口和Java抽象类了,这也是本文我想说的重点。
既然面向对象设计的重点在于抽象,那Java接口和Java抽象类就有它存在的必定性了。
Java接口(interface)和Java抽象类(abstract class)代表的就是抽象类型,就是我们须要提出的抽象层的详细表现。OOP面向对象的编程,假设要提高程序的复用率,添加程序 的可维护性,可扩展性,就必须是面向接口的编程,面向抽象的编程,正确地使用接口、抽象类这些实用的抽象类型作为你结构层次上的顶层。
Java接口和Java抽象类有太多相似的地方,又有太多特别的地方,到底在什么地方,才是它们的最佳位置呢?把它们比較一下,你就能够发现了。
Java接口和Java抽象类的存在就是为了用于详细类的实现和继承的,假设你准备写一个详细类去继承还有一个详细类的话,那你的设计就有非常大问题了。Java抽象类就是为了继承而存在的,它的抽象方法就是为了强制子类必须去实现的。
使用Java接口和抽象Java类进行变量的类型声明、參数是类型声明、方法的返还类型说明,以及数据类型的转换等。而不要用详细Java类进行变量的类型声明、參数是类型声明、方法的返还类型说明,以及数据类型的转换等。
以下给出一个详细的接口Action,代码例如以下所看到的:
package org.springframework.webflow.execution;
public interface Action {
public Event execute(RequestContext context) throws Exception;
}在这个接口中,定义了一个没有详细实现的方法,方法名叫做execute(),返回类型是Event。如前面第一条所述,接口中的方法都是没有实现的。这些方法的详细实现是在实现(implements)这个接口的类中给出的。再来看一个实现Action接口的抽象类AbstractAction,代码例如以下。
package org.springframework.webflow.action;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.ClassUtils;
import org.springframework.webflow.core.collection.AttributeMap;
import org.springframework.webflow.execution.Action;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;
public abstract class AbstractAction implements Action, InitializingBean {
protected final Log logger = LogFactory.getLog(getClass());
public EventFactorySupport getEventFactorySupport() {
return new EventFactorySupport();
}
public void afterPropertiesSet() throws Exception {
try {
initAction();
} catch (Exception ex) {
throw new BeanInitializationException("Initialization of this Action failed: " + ex.getMessage(), ex);
}
}
protected void initAction() throws Exception {
}
protected Event success() {
return getEventFactorySupport().success(this);
}
protected Event success(Object result) {
return getEventFactorySupport().success(this, result);
}
protected Event error() {
return getEventFactorySupport().error(this);
}
protected Event error(Exception e) {
return getEventFactorySupport().error(this, e);
}
protected Event yes() {
return getEventFactorySupport().yes(this);
}
protected Event no() {
return getEventFactorySupport().no(this);
}
protected Event result(boolean booleanResult) {
return getEventFactorySupport().event(this, booleanResult);
}
protected Event result(String eventId) {
return getEventFactorySupport().event(this, eventId);
}
protected Event result(String eventId, AttributeMap resultAttributes) {
return getEventFactorySupport().event(this, eventId, resultAttributes);
}
protected Event result(String eventId, String resultAttributeName, Object resultAttributeValue) {
return getEventFactorySupport().event(this, eventId, resultAttributeName, resultAttributeValue);
}
public final Event execute(RequestContext context) throws Exception {
Event result = doPreExecute(context);
if (result == null) {
result = doExecute(context);
doPostExecute(context);
} else {
if (logger.isInfoEnabled()) {
logger.info("Action execution disallowed; pre-execution result is ‘" + result.getId() + "‘");
}
}
return result;
}
protected String getActionNameForLogging() {
return ClassUtils.getShortName(getClass());
}
protected Event doPreExecute(RequestContext context) throws Exception {
return null;
}
//抽象方法
protected abstract Event doExecute(RequestContext context) throws Exception;
protected void doPostExecute(RequestContext context) throws Exception {
}
}在抽象类AbstractAction中,既有详细实现的方法,又有没有详细实现的抽象方法
//抽象方法 protected abstract Event doExecute(RequestContext context) throws Exception;须要注意的是在抽象类中,假设方法没有详细实现(就是方法后面没有{}),那么必须加上abstract来声明这种方法,而接口中不须要使用abstract来声明(抽象类之所以被称为抽象类,就是由于它包括有抽象方法。含有抽象方法的类叫做抽象类)。
标签:
原文地址:http://www.cnblogs.com/lcchuguo/p/4513006.html