当前位置: 首页 > news >正文

Java基础-设计模式详解

摘要:设计模式是软件工程中解决常见问题的经典方案。本文结合Java语言特性,深入解析常用设计模式的核心思想、实现方式及实际应用场景,帮助开发者提升代码质量和可维护性。


一、设计模式概述

1.1 什么是设计模式?

设计模式(Design Pattern)是经过验证的、可重复使用的代码设计经验总结,分为创建型结构型行为型三大类,共23种经典模式。

1.2 学习设计模式的意义

  • 提高代码复用性
  • 增强系统扩展性
  • 提升团队协作效率
  • 优化代码结构

二、创建型模式实践

2.1 单例模式(Singleton)

场景:全局唯一对象(如配置管理器)

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;}
}

特点:双重检查锁实现线程安全


2.2 工厂模式(Factory)

场景:对象创建逻辑封装

interface Shape {void draw();
}class Circle implements Shape {public void draw() {System.out.println("绘制圆形");}
}class ShapeFactory {public Shape getShape(String type) {if ("CIRCLE".equalsIgnoreCase(type)) {return new Circle();}return null;}
}

优势:解耦客户端与具体实现类


三、结构型模式解析

3.1 适配器模式(Adapter)

场景:接口兼容转换

class LegacySystem {public void legacyRequest() {System.out.println("旧系统请求");}
}interface NewSystem {void request();
}class Adapter implements NewSystem {private LegacySystem legacy = new LegacySystem();public void request() {legacy.legacyRequest();}
}

3.2 装饰器模式(Decorator)

场景:动态扩展功能

interface Coffee {double getCost();
}class BasicCoffee implements Coffee {public double getCost() { return 2.0; }
}abstract class CoffeeDecorator implements Coffee {protected Coffee decoratedCoffee;public CoffeeDecorator(Coffee coffee) {this.decoratedCoffee = coffee;}
}class MilkDecorator extends CoffeeDecorator {public MilkDecorator(Coffee coffee) {super(coffee);}public double getCost() {return super.decoratedCoffee.getCost() + 0.5;}
}

四、行为型模式应用

4.1 观察者模式(Observer)

场景:事件驱动系统

interface Observer {void update(String message);
}class ConcreteObserver implements Observer {public void update(String message) {System.out.println("收到通知:" + message);}
}class Subject {private List<Observer> observers = new ArrayList<>();public void addObserver(Observer o) {observers.add(o);}public void notifyObservers(String message) {observers.forEach(o -> o.update(message));}
}

4.2 策略模式(Strategy)

场景:算法灵活切换

interface PaymentStrategy {void pay(double amount);
}class AlipayStrategy implements PaymentStrategy {public void pay(double amount) {System.out.println("支付宝支付:" + amount);}
}class PaymentContext {private PaymentStrategy strategy;public void setStrategy(PaymentStrategy strategy) {this.strategy = strategy;}public void executePayment(double amount) {strategy.pay(amount);}
}

五、模式选择指南

模式类型典型场景常用模式
创建型对象创建管理工厂、单例、建造者
结构型类/对象组合适配器、代理、装饰器
行为型对象交互观察者、策略、模板方法

http://www.mrgr.cn/news/97297.html

相关文章:

  • [项目总结] 在线OJ刷题系统项目技术应用(上)
  • 燕山大学计算机网络实验(包括实验指导书)
  • 7B斗671B:扩散模型能否颠覆自回归霸权?
  • 网络编程—TCP/IP模型(TCP协议)
  • 通过Postman和OAuth 2.0连接Dynamics 365 Online的详细步骤
  • Java 进化之路:从 Java 8 到 Java 21 的重要新特性
  • 数据库原理
  • (二)输入输出处理——打造智能对话的灵魂
  • AI Agent开发大全第二十课-如何开发一个MCP(从0开发一个MCP Server)
  • 250405-VSCode编辑launch.json实现Debug调试Open-WebUI
  • Android学习总结之应用启动流程(从点击图标到界面显示)
  • STM32F103C8T6实现 SG90 180 °舵机任意角度转动
  • 【蓝桥杯】算法笔记3
  • JJJ:generic netlink例程分析
  • Flask+Vue构建图书管理系统及Echarts组件的使用
  • 第3课:状态管理与事件处理
  • 高级:分布式系统面试题精讲
  • 一、简单的 Django 服务
  • (一)从零开始:用 LangChain 和 ZhipuAI 搭建简单对话
  • 基于YOLO11实例分割与奥比中光相机的快递包裹抓取点检测