成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

工作中最常用的八種設計模式

開發 前端
今天結合我實際工作場景和源碼實例,跟大家一起聊聊工作中最常用的8種設計模式,希望對你會有所幫助。

前言

設計模式在我們日常的軟件開發中無處不在,它們幫助我們編寫更易擴展、更具可讀性的代碼。

今天結合我實際工作場景和源碼實例,跟大家一起聊聊工作中最常用的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 中深度體現。

了解它們的本質和應用場景,能夠讓我們寫出更優雅、更健壯的代碼。

下次再遇到類似問題時,希望你能得心應手地選擇合適的模式!

責任編輯:武曉燕 來源: 蘇三說技術
相關推薦

2024-03-18 08:22:15

OOM問題java線上問題

2025-06-16 08:22:23

2024-10-14 08:39:29

工廠模式策略模式代碼

2024-05-09 08:19:09

OOMJVM內存

2024-01-29 18:02:46

2021-10-27 17:57:35

設計模式場景

2021-04-18 21:07:32

門面模式設計

2025-04-25 07:10:00

GenAIAI工具人工智能

2022-06-16 11:01:22

數據庫SQL

2021-04-14 09:02:22

模式 設計建造者

2024-11-08 13:34:24

2022-02-17 11:03:33

數據庫基礎語法用法

2021-09-16 11:02:49

Python線程

2024-04-28 11:22:18

2017-10-30 13:34:22

深度學習KerasAPI

2021-08-11 17:22:11

設計模式單例

2010-05-25 09:19:13

MySQL管理工具

2022-10-14 13:47:06

2009-06-10 21:58:51

Javascript常

2024-02-26 00:00:00

stage函數進度
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲高清在线 | 九九视频在线观看 | 久久亚洲欧美日韩精品专区 | 夜夜精品视频 | 97av视频在线 | 欧美人成在线视频 | 国产精品久久久久久久久久久久久久 | 91.色| 九久久 | 欧美www在线 | 亚洲视频三 | 亚洲综合久久久 | 国产在线视频一区 | 免费看黄色国产 | 免费看一区二区三区 | 欧美亚洲国产一区 | 岛国一区| 久久电影一区 | 亚洲国产一区二区三区在线观看 | 亚洲国产一区二区三区在线观看 | 国产精品久久久久久久 | 午夜影院| 97久久久久久久久 | 九九久久免费视频 | 欧美一区二区三区在线观看 | 91久久久久久久久 | 91不卡在线 | 在线播放中文字幕 | 91精品国产一区二区三区 | av 一区二区三区 | 男女视频在线观看 | 精品熟人一区二区三区四区 | 午夜精品一区二区三区在线播放 | 欧美成人精品激情在线观看 | 国产综合精品一区二区三区 | 免费午夜视频在线观看 | 亚洲综合婷婷 | 97人人超碰 | 精品国产一级 | 精品国产91乱码一区二区三区 | 久草久草久草 |