一個軟件設計的好壞,我想很大程度上取決於它的整體架構,而這個整體架構其實就是你對整個宏觀商業業務的抽象框架,當代表業務邏輯的高層抽象層結構 合理時,你底層的具體實現需要考慮的就僅僅是一些算法和一些具體的業務實現了。當你需要再開發另一個相近的項目時,你以前的抽象層說不定還可以再次利用 。面對對象的設計,復用的重點其實應該是抽象層的復用,而不是具體某一個代碼塊的復用。
說到了抽象,我就不能不提到曾讓我頭痛的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來聲明(抽象類之所以被稱為抽象類,就是因為它包含有抽象方法。含有抽象方法的類叫做抽象類)。