| 維度 | 抽象類 | 介面 |
|---|---|---|
| 本質 | 類(可有狀態、構造器) | 契約(純行為規範,JDK 8 前) |
| 繼承/實現 | 單繼承 extends | 多實現 implements,介面多繼承 extends |
| 字段 | 實例/靜態/常量 | 隱式 public static final 常量 |
| 方法 | 抽象+具體+靜態+私有 | JDK 7-:抽象 JDK 8+:default/static/private |
| 構造器 | 有(子類調用) | 無 |
| 訪問控制 | 全套 | 隱式 public(JDK 9+ 允許 private 方法) |
滿足任一核心條件 → 抽象類:
protected 輔助方法、private 內部邏輯。DataSource、ThreadPool)。public abstract class AbstractParser {
// 共享字段
protected final Charset charset;
// 構造器強制參數
public AbstractParser(Charset charset) { this.charset = charset; }
// 模板方法:定義算法骨架
public final String parse(File file) throws IOException {
String raw = readFile(file); // 通用步驟
String processed = preProcess(raw); // 可變步驟
return doParse(processed); // 抽象步驟
}
// 具體方法(共享邏輯)
private String readFile(File file) throws IOException {
return Files.readString(file.toPath(), charset);
}
protected String preProcess(String raw) { return raw.trim(); } // 鉤子方法
// 抽象方法(子類必須實現)
protected abstract String doParse(String content);
}
// 子類只關注核心解析邏輯
public class JsonParser extends AbstractParser {
public JsonParser() { super(StandardCharsets.UTF_8); }
@Override protected String doParse(String content) {
return new JSONObject(content).toString(2);
}
}
滿足任一核心條件 → 介面:
Serializable, Comparable, Runnable(標記/能力介面)。implements。Serializable & Cloneable & Comparable。// 臃腫介面 → 拆分
// Bad
interface Machine { void print(); void scan(); void fax(); }
// Good - 客戶只依賴需要的能力
interface Printable { void print(); }
interface Scannable { void scan(); }
interface Faxable { void fax(); }
// 組合實現
class MultiFunctionPrinter implements Printable, Scannable, Faxable { ... }
class SimplePrinter implements Printable { ... }
Collection.stream())。Comparator.naturalOrder())。interface A { default void foo() { System.out.println("A"); } }
interface B extends A { default void foo() { System.out.println("B"); } }
interface C extends A { default void foo() { System.out.println("C"); } }
class D implements B, C {
// 必須顯式覆寫解決衝突
@Override public void foo() { B.super.foo(); }
}
大型框架常見模式:
// 1. 定義能力介面(多實現、組合)
public interface EventListener { void onEvent(Event e); }
public interface Ordered { int getOrder(); }
// 2. 提供骨架抽象類(共享狀態、模板方法、通用邏輯)
public abstract class AbstractEventListener implements EventListener, Ordered {
private final Logger log = LoggerFactory.getLogger(getClass());
private int order = 0;
public final void onEvent(Event e) { // 最終模板
log.debug("Processing {}", e);
try { doHandle(e); }
catch (Exception ex) { handleError(e, ex); }
}
protected abstract void doHandle(Event e); // 核心變化點
protected void handleError(Event e, Exception ex) { log.error("Fail", ex); }
@Override public int getOrder() { return order; }
public void setOrder(int order) { this.order = order; }
}
// 3. 具體實現輕量
public class UserLoginListener extends AbstractEventListener {
@Override protected void doHandle(Event e) { ... }
}
| 場景 | 技巧 |
|---|---|
| 禁止實例化工具類 | private Constructor() {} 或 abstract class + final(不可繼承) |
| 常量庫 | interface Constants { String KEY = "v"; }(不推薦,污染實現類命名空間,改用 final class) |
| 標記類型 | interface Serializable { } 無方法,運行期反射檢測 |
| 函數式介面 | @FunctionalInterface 單抽象方法,支援 Lambda |
需要定義類型契約?
├─ 是 → 是否需要共享代碼/狀態/構造器?
│ ├─ 是 → 抽象類 (extends)
│ └─ 否 → 介面 (implements)
└─ 否 → 具體類
特殊情況:
- 類已繼承別的類 → 必須介面
- 需要多重組合能力 → 介面
- 需要非 public 成員/構造器 → 抽象類
- API 公開給第三方實現 → 介面 (SPI)
- 框架內部骨架 → 抽象類