工作中最常用的八種設(shè)計(jì)模式
前言
設(shè)計(jì)模式在我們?nèi)粘5能浖_發(fā)中無處不在,它們幫助我們編寫更易擴(kuò)展、更具可讀性的代碼。
今天結(jié)合我實(shí)際工作場(chǎng)景和源碼實(shí)例,跟大家一起聊聊工作中最常用的8種設(shè)計(jì)模式,希望對(duì)你會(huì)有所幫助。
1. 單例模式
單例模式確保一個(gè)類只有一個(gè)實(shí)例,通常用于管理共享資源,如配置、緩存、線程池等。
代碼實(shí)現(xiàn):雙重檢查鎖這是單例模式的標(biāo)準(zhǔn)寫法,既保證線程安全,又避免性能損耗。
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 中的應(yīng)用:
- java.lang.Runtime.getRuntime()
- java.util.logging.Logger
Spring 中的應(yīng)用:Spring 的 Bean 默認(rèn)是單例模式??梢酝ㄟ^ @Scope("prototype") 將其改為多例。
2. 工廠模式
工廠模式用于封裝對(duì)象的創(chuàng)建邏輯,特別是當(dāng)類實(shí)例化過程復(fù)雜時(shí),可以降低耦合度。
代碼實(shí)現(xiàn):簡(jiǎn)單工廠以支付系統(tǒng)為例,不同支付方式需要不同的對(duì)象。
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 中的應(yīng)用:
- java.util.Calendar.getInstance()
- javax.xml.parsers.DocumentBuilderFactory.newInstance()
Spring 中的應(yīng)用:
- BeanFactory 和 ApplicationContext 都是工廠模式的體現(xiàn)。
3. 策略模式
策略模式將不同算法封裝為獨(dú)立類,并允許在運(yùn)行時(shí)選擇不同的策略。
代碼實(shí)現(xiàn):促銷策略以電商促銷為例,支持滿減、打折等多種策略。
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 中的應(yīng)用:
- java.util.Comparator 是典型的策略模式。
Spring 中的應(yīng)用:
- 事務(wù)管理(TransactionManager),支持編程式和聲明式事務(wù)。
4. 代理模式
代理模式通過代理對(duì)象控制對(duì)目標(biāo)對(duì)象的訪問,常用于權(quán)限控制、日志記錄等場(chǎng)景。
代碼實(shí)現(xiàn):靜態(tài)代理模擬對(duì)一個(gè)服務(wù)的權(quán)限控制。
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 中的應(yīng)用:
- 動(dòng)態(tài)代理 java.lang.reflect.Proxy
- RMI(遠(yuǎn)程方法調(diào)用)
Spring 中的應(yīng)用:
- AOP(面向切面編程)廣泛使用代理模式。
5. 觀察者模式
觀察者模式定義一對(duì)多的依賴,當(dāng)一個(gè)對(duì)象狀態(tài)變化時(shí),所有依賴它的對(duì)象都會(huì)收到通知。
代碼實(shí)現(xiàn):事件通知模擬微博用戶的粉絲通知。
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 中的應(yīng)用:
- java.util.Observer 和 java.util.Observable
- javax.swing.event.ChangeListener
Spring 中的應(yīng)用:
- ApplicationEvent 和 ApplicationListener 是典型實(shí)現(xiàn)。
6. 裝飾器模式
裝飾器模式在不改變?cè)碱惖幕A(chǔ)上,動(dòng)態(tài)擴(kuò)展其功能。
代碼實(shí)現(xiàn):咖啡加料模擬一個(gè)咖啡訂單系統(tǒng),可以動(dòng)態(tài)加料。
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 中的應(yīng)用:
- java.io.BufferedInputStream 和 java.io.BufferedOutputStream
Spring 中的應(yīng)用:
- BeanPostProcessor 用于動(dòng)態(tài)修改 Bean 的行為。
7. 模板方法模式
模板方法模式定義一個(gè)算法的骨架,把具體的實(shí)現(xiàn)留給子類。
代碼實(shí)現(xiàn):任務(wù)執(zhí)行模板模擬定時(shí)任務(wù)的執(zhí)行流程。
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 中的應(yīng)用:
- java.util.AbstractList 和 java.util.AbstractMap
Spring 中的應(yīng)用:
- JdbcTemplate 和 RestTemplate
8. 建造者模式
建造者模式用于創(chuàng)建復(fù)雜對(duì)象,特別是當(dāng)對(duì)象有多個(gè)可選參數(shù)時(shí)。
代碼實(shí)現(xiàn):構(gòu)建 HTTP 請(qǐng)求
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 中的應(yīng)用:
- StringBuilder
- Stream.Builder
Spring 中的應(yīng)用:
- UriComponentsBuilder 用于構(gòu)建 URI。
總結(jié)
這些設(shè)計(jì)模式不僅在日常開發(fā)中有著廣泛應(yīng)用,更在 JDK 和 Spring 中深度體現(xiàn)。
了解它們的本質(zhì)和應(yīng)用場(chǎng)景,能夠讓我們寫出更優(yōu)雅、更健壯的代碼。
下次再遇到類似問題時(shí),希望你能得心應(yīng)手地選擇合適的模式!