自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

工作中最常用的八種設(shè)計(jì)模式

開發(fā) 前端
今天結(jié)合我實(shí)際工作場(chǎng)景和源碼實(shí)例,跟大家一起聊聊工作中最常用的8種設(shè)計(jì)模式,希望對(duì)你會(huì)有所幫助。

前言

設(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)手地選擇合適的模式!

責(zé)任編輯:武曉燕 來源: 蘇三說技術(shù)
相關(guān)推薦

2024-03-18 08:22:15

OOM問題java線上問題

2024-10-14 08:39:29

工廠模式策略模式代碼

2024-05-09 08:19:09

OOMJVM內(nèi)存

2024-01-29 18:02:46

2021-10-27 17:57:35

設(shè)計(jì)模式場(chǎng)景

2021-04-18 21:07:32

門面模式設(shè)計(jì)

2025-04-25 07:10:00

GenAIAI工具人工智能

2021-04-14 09:02:22

模式 設(shè)計(jì)建造者

2022-06-16 11:01:22

數(shù)據(jù)庫(kù)SQL

2024-11-08 13:34:24

2022-02-17 11:03:33

數(shù)據(jù)庫(kù)基礎(chǔ)語法用法

2021-09-16 11:02:49

Python線程

2024-04-28 11:22:18

2017-10-30 13:34:22

深度學(xué)習(xí)KerasAPI

2021-08-11 17:22:11

設(shè)計(jì)模式單例

2010-05-25 09:19:13

MySQL管理工具

2009-06-10 21:58:51

Javascript常

2022-10-14 13:47:06

2024-02-26 00:00:00

stage函數(shù)進(jìn)度

2023-04-10 15:44:24

人工智能聊天機(jī)器人技能
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)