工作中最常用的八種設計模式
前言
設計模式在我們日常的軟件開發中無處不在,它們幫助我們編寫更易擴展、更具可讀性的代碼。
今天結合我實際工作場景和源碼實例,跟大家一起聊聊工作中最常用的8種設計模式,希望對你會有所幫助。
1. 單例模式
單例模式確保一個類只有一個實例,通常用于管理共享資源,如配置、緩存、線程池等。
代碼實現:雙重檢查鎖這是單例模式的標準寫法,既保證線程安全,又避免性能損耗。
public class Singleton {
private static volatile Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
JDK 中的應用:
- java.lang.Runtime.getRuntime()
- java.util.logging.Logger
Spring 中的應用:Spring 的 Bean 默認是單例模式。可以通過 @Scope("prototype") 將其改為多例。
2. 工廠模式
工廠模式用于封裝對象的創建邏輯,特別是當類實例化過程復雜時,可以降低耦合度。
代碼實現:簡單工廠以支付系統為例,不同支付方式需要不同的對象。
public class PaymentFactory {
public static Payment createPayment(String type) {
switch (type) {
case "AliPay":
return new AliPay();
case "WeChatPay":
return new WeChatPay();
default:
throw new IllegalArgumentException("Unknown payment type");
}
}
}
JDK 中的應用:
- java.util.Calendar.getInstance()
- javax.xml.parsers.DocumentBuilderFactory.newInstance()
Spring 中的應用:
- BeanFactory 和 ApplicationContext 都是工廠模式的體現。
3. 策略模式
策略模式將不同算法封裝為獨立類,并允許在運行時選擇不同的策略。
代碼實現:促銷策略以電商促銷為例,支持滿減、打折等多種策略。
public interface PromotionStrategy {
void applyPromotion();
}
public class DiscountStrategy implements PromotionStrategy {
@Override
public void applyPromotion() {
System.out.println("Applying discount...");
}
}
public class PromotionContext {
private PromotionStrategy strategy;
public PromotionContext(PromotionStrategy strategy) {
this.strategy = strategy;
}
public void executePromotion() {
strategy.applyPromotion();
}
}
JDK 中的應用:
- java.util.Comparator 是典型的策略模式。
Spring 中的應用:
- 事務管理(TransactionManager),支持編程式和聲明式事務。
4. 代理模式
代理模式通過代理對象控制對目標對象的訪問,常用于權限控制、日志記錄等場景。
代碼實現:靜態代理模擬對一個服務的權限控制。
public interface Service {
void execute();
}
public class RealService implements Service {
@Override
public void execute() {
System.out.println("Executing real service...");
}
}
public class ServiceProxy implements Service {
private RealService realService;
@Override
public void execute() {
System.out.println("Checking permissions...");
if (realService == null) {
realService = new RealService();
}
realService.execute();
}
}
JDK 中的應用:
- 動態代理 java.lang.reflect.Proxy
- RMI(遠程方法調用)
Spring 中的應用:
- AOP(面向切面編程)廣泛使用代理模式。
5. 觀察者模式
觀察者模式定義一對多的依賴,當一個對象狀態變化時,所有依賴它的對象都會收到通知。
代碼實現:事件通知模擬微博用戶的粉絲通知。
public interface Observer {
void update(String message);
}
public class User implements Observer {
private String name;
public User(String name) {
this.name = name;
}
@Override
public void update(String message) {
System.out.println(name + " received message: " + message);
}
}
public class Weibo {
private List<Observer> observers = new ArrayList<>();
public void follow(Observer observer) {
observers.add(observer);
}
public void post(String message) {
for (Observer observer : observers) {
observer.update(message);
}
}
}
JDK 中的應用:
- java.util.Observer 和 java.util.Observable
- javax.swing.event.ChangeListener
Spring 中的應用:
- ApplicationEvent 和 ApplicationListener 是典型實現。
6. 裝飾器模式
裝飾器模式在不改變原始類的基礎上,動態擴展其功能。
代碼實現:咖啡加料模擬一個咖啡訂單系統,可以動態加料。
public interface Coffee {
String getDescription();
double getCost();
}
public class SimpleCoffee implements Coffee {
@Override
public String getDescription() {
return "Simple Coffee";
}
@Override
public double getCost() {
return 5.0;
}
}
public class MilkDecorator implements Coffee {
private Coffee coffee;
public MilkDecorator(Coffee coffee) {
this.coffee = coffee;
}
@Override
public String getDescription() {
return coffee.getDescription() + ", Milk";
}
@Override
public double getCost() {
return coffee.getCost() + 1.5;
}
}
JDK 中的應用:
- java.io.BufferedInputStream 和 java.io.BufferedOutputStream
Spring 中的應用:
- BeanPostProcessor 用于動態修改 Bean 的行為。
7. 模板方法模式
模板方法模式定義一個算法的骨架,把具體的實現留給子類。
代碼實現:任務執行模板模擬定時任務的執行流程。
public abstract class Task {
public final void execute() {
init();
doWork();
cleanup();
}
protected abstract void init();
protected abstract void doWork();
protected void cleanup() {
System.out.println("Default cleanup...");
}
}
public class DataProcessingTask extends Task {
@Override
protected void init() {
System.out.println("Initializing data...");
}
@Override
protected void doWork() {
System.out.println("Processing data...");
}
}
JDK 中的應用:
- java.util.AbstractList 和 java.util.AbstractMap
Spring 中的應用:
- JdbcTemplate 和 RestTemplate
8. 建造者模式
建造者模式用于創建復雜對象,特別是當對象有多個可選參數時。
代碼實現:構建 HTTP 請求
public class HttpRequest {
private String method;
private String url;
private String body;
private HttpRequest(Builder builder) {
this.method = builder.method;
this.url = builder.url;
this.body = builder.body;
}
public static class Builder {
private String method;
private String url;
private String body;
public Builder method(String method) {
this.method = method;
return this;
}
public Builder url(String url) {
this.url = url;
return this;
}
public Builder body(String body) {
this.body = body;
return this;
}
public HttpRequest build() {
return new HttpRequest(this);
}
}
}
JDK 中的應用:
- StringBuilder
- Stream.Builder
Spring 中的應用:
- UriComponentsBuilder 用于構建 URI。
總結
這些設計模式不僅在日常開發中有著廣泛應用,更在 JDK 和 Spring 中深度體現。
了解它們的本質和應用場景,能夠讓我們寫出更優雅、更健壯的代碼。
下次再遇到類似問題時,希望你能得心應手地選擇合適的模式!