原创 摸鱼的java工程师 2025-06-21 09:05 重庆
设计模式是软件开发中经过验证的最佳实践,合理运用设计模式可以提升代码的可维护性、可扩展性和可读性。
二、工厂模式(Factory Pattern)「模式定义」:通过工厂类封装对象的创建逻辑,客户端无需知道具体实现类。「适用场景」:对象创建逻辑复杂(如参数校验、初始化步骤)。需要解耦对象创建和使用的场景。实现方式:简单工厂(创建不同日志处理器)/**
* 枚举单例(推荐)
* 优点:线程安全,防止反射攻击,代码简洁
*/
public enum Singleton {
INSTANCE; // 唯一实例
// 示例方法:模拟获取配置
public String getConfig() {
return "config from singleton";
}
}
// 使用示例
public class Client {
public static void main(String[] args) {
Singleton instance1 = Singleton.INSTANCE;
Singleton instance2 = Singleton.INSTANCE;
System.out.println(instance1 == instance2); // 输出 true
}
}
三、策略模式(Strategy Pattern)「模式定义」:定义一系列算法,将每个算法封装起来,使其可以相互替换。「适用场景」:算法动态切换(如支付方式、排序策略)。避免多重条件判断(将条件逻辑封装为策略类)。实现方式:支付策略(支付宝 / 微信支付)// 日志接口
interface Logger {
void log(String message);
}
// 控制台日志实现
class ConsoleLogger implements Logger {
public void log(String message) {
System.out.println("Console: " + message);
}
}
// 文件日志实现
class FileLogger implements Logger {
public void log(String message) {
System.out.println("File: " + message);
}
}
// 工厂类
class LoggerFactory {
// 根据类型创建日志对象
public static Logger createLogger(String type) {
if ("console".equalsIgnoreCase(type)) {
return new ConsoleLogger();
} else if ("file".equalsIgnoreCase(type)) {
return new FileLogger();
} else {
throw new IllegalArgumentException("Unsupported logger type: " + type);
}
}
}
// 使用示例
public class Client {
public static void main(String[] args) {
Logger consoleLogger = LoggerFactory.createLogger("console");
consoleLogger.log("Hello, World!"); // 输出:Console: Hello, World!
}
}
四、观察者模式(Observer Pattern)「模式定义」:定义对象间的一种一对多依赖关系,当一个对象状态改变时,其依赖的对象都能得到通知并自动更新。「适用场景」:消息订阅与发布(如事件总线、通知系统)。监听状态变化(如股票价格变动、天气预警)。实现方式:天气观察者(观察者订阅天气更新)// 支付策略接口
interface PaymentStrategy {
void pay(double amount);
}
// 支付宝支付策略
class AlipayStrategy implements PaymentStrategy {
public void pay(double amount) {
System.out.println("使用支付宝支付:" + amount + "元");
}
}
// 微信支付策略
class WechatPayStrategy implements PaymentStrategy {
public void pay(double amount) {
System.out.println("使用微信支付:" + amount + "元");
}
}
// 上下文类(管理策略)
class PaymentContext {
private PaymentStrategy strategy;
public PaymentContext(PaymentStrategy strategy) {
this.strategy = strategy;
}
public void setStrategy(PaymentStrategy strategy) {
this.strategy = strategy;
}
public void executePayment(double amount) {
strategy.pay(amount);
}
}
// 使用示例
public class Client {
public static void main(String[] args) {
PaymentContext context = new PaymentContext(new AlipayStrategy());
context.executePayment(199.9); // 输出:使用支付宝支付:199.9元
context.setStrategy(new WechatPayStrategy());
context.executePayment(88.8); // 输出:使用微信支付:88.8元
}
}
五、装饰器模式(Decorator Pattern)「模式定义」:动态地给一个对象添加额外的职责,比继承更灵活。「适用场景」:扩展对象功能(如 IO 流的缓冲、加密装饰)。避免通过继承导致类爆炸(多层继承)。实现方式:咖啡调味(基础咖啡 + 牛奶 / 糖浆装饰)// 主题接口(被观察者)
interface WeatherSubject {
void addObserver(WeatherObserver observer); // 添加观察者
void removeObserver(WeatherObserver observer); // 移除观察者
void notifyObservers(); // 通知所有观察者
void setTemperature(double temperature); // 更新温度
}
// 观察者接口
interface WeatherObserver {
void update(double temperature); // 接收更新通知
}
// 具体主题(天气数据中心)
class WeatherDataCenter implements WeatherSubject {
private double temperature;
private final List<WeatherObserver> observers = new ArrayList<>();
public void addObserver(WeatherObserver observer) {
observers.add(observer);
}
public void removeObserver(WeatherObserver observer) {
observers.remove(observer);
}
public void notifyObservers() {
for (WeatherObserver observer : observers) {
observer.update(temperature); // 推送更新
}
}
public void setTemperature(double temperature) {
this.temperature = temperature;
notifyObservers(); // 温度变化时主动通知
}
}
// 具体观察者(手机APP)
class MobileAppObserver implements WeatherObserver {
private final String name;
public MobileAppObserver(String name) {
this.name = name;
}
public void update(double temperature) {
System.out.println(name + " 收到通知:当前温度为 " + temperature + "℃");
}
}
// 使用示例
public class Client {
public static void main(String[] args) {
WeatherDataCenter subject = new WeatherDataCenter();
WeatherObserver app1 = new MobileAppObserver("天气通");
WeatherObserver app2 = new MobileAppObserver("彩云天气");
subject.addObserver(app1);
subject.addObserver(app2);
subject.setTemperature(25.5); // 温度变化,触发通知
// 输出:
// 天气通 收到通知:当前温度为 25.5℃
// 彩云天气 收到通知:当前温度为 25.5℃
}
}
六、适配器模式(Adapter Pattern)「模式定义」:将一个类的接口转换成客户希望的另一个接口,使不兼容的接口可以协同工作。「适用场景」:兼容旧系统接口(如将第三方库接口转换为内部接口)。类的接口与客户需求不匹配,但又不能修改原有类。实现方式:电压适配器(220V 转 5V)// 饮料接口
interface Beverage {
String getDescription(); // 获取描述
double cost(); // 计算价格
}
// 基础咖啡(具体组件)
class BasicCoffee implements Beverage {
public String getDescription() {
return "基础咖啡";
}
public double cost() {
return 15.0; // 基础价格
}
}
// 装饰器抽象类(继承饮料接口,持有被装饰对象)
abstract class BeverageDecorator implements Beverage {
protected final Beverage beverage; // 被装饰的饮料
public BeverageDecorator(Beverage beverage) {
this.beverage = beverage;
}
public abstract String getDescription();
public abstract double cost();
}
// 牛奶装饰器(具体装饰器)
class MilkDecorator extends BeverageDecorator {
public MilkDecorator(Beverage beverage) {
super(beverage);
}
public String getDescription() {
return beverage.getDescription() + " + 牛奶"; // 扩展描述
}
public double cost() {
return beverage.cost() + 3.0; // 增加牛奶的价格
}
}
// 糖浆装饰器(具体装饰器)
class SyrupDecorator extends BeverageDecorator {
public SyrupDecorator(Beverage beverage) {
super(beverage);
}
public String getDescription() {
return beverage.getDescription() + " + 糖浆";
}
public double cost() {
return beverage.cost() + 2.0;
}
}
// 使用示例
public class Client {
public static void main(String[] args) {
// 基础咖啡 + 牛奶 + 糖浆
Beverage coffee = new BasicCoffee();
coffee = new MilkDecorator(coffee);
coffee = new SyrupDecorator(coffee);
System.out.println("饮料:" + coffee.getDescription());
System.out.println("价格:" + coffee.cost() + "元");
// 输出:
// 饮料:基础咖啡 + 牛奶 + 糖浆
// 价格:20.0元
}
}
七、模板方法模式(Template Method Pattern)「模式定义」:定义一个算法的骨架,将具体步骤延迟到子类实现,子类可以在不改变算法结构的情况下重新定义某些步骤。「适用场景」:多个子类有公共逻辑(如文件操作流程、审批流程)。避免代码重复,统一算法骨架。实现方式:文件处理器(读取文件 + 解析逻辑)// 目标接口(手机需要5V充电)
interface FiveVolt {
int getVolt5(); // 获取5V电压
}
// 被适配者(电源提供220V电压)
class AC220Volt {
public int getVolt220() {
return 220; // 原始电压
}
}
// 适配器类(将220V转换为5V)
class VoltageAdapter extends AC220Volt implements FiveVolt {
@Override
public int getVolt5() {
int volt220 = getVolt220();
// 模拟降压逻辑(实际需物理电路)
return volt220 / 44; // 220V -> 5V
}
}
// 使用示例
public class Client {
public static void main(String[] args) {
FiveVolt charger = new VoltageAdapter();
System.out.println("手机充电电压:" + charger.getVolt5() + "V"); // 输出:5V
}
}
总结:设计模式的核心思想「单一职责原则」:每个类只负责单一功能(如工厂类只负责创建对象)。「开闭原则」:对扩展开放,对修改关闭(如策略模式新增策略无需修改原有代码)。「组合复用原则」:优先使用组合 / 聚合,而非继承(如装饰器模式通过持有对象实现功能扩展)。// 模板类(定义文件处理流程)
abstract class FileProcessor {
// 模板方法:固定处理流程
public final void processFile(String path) {
String content = readFile(path); // 读取文件(子类实现)
parseContent(content); // 解析内容(子类实现)
saveToDatabase(content); // 保存到数据库(公共逻辑)
}
// 子类必须实现的抽象方法
protected abstract String readFile(String path);
protected abstract void parseContent(String content);
// 公共方法(子类可覆盖,默认实现)
protected void saveToDatabase(String content) {
System.out.println("保存内容到数据库:" + content);
}
}
// CSV文件处理器(具体子类)
class CSVProcessor extends FileProcessor {
protected String readFile(String path) {
return "模拟读取CSV文件内容:姓名,年龄\n张三,25";
}
protected void parseContent(String content) {
System.out.println("解析CSV内容:" + content);
}
// 覆盖公共方法(可选)
protected void saveToDatabase(String content) {
System.out.println("特殊处理:将CSV内容存入专用表");
}
}
// 使用示例
public class Client {
public static void main(String[] args) {
FileProcessor processor = new CSVProcessor();
processor.processFile("data.csv");
// 输出:
// 解析CSV内容:姓名,年龄\n张三,25
// 特殊处理:将CSV内容存入专用表
}
}
AI编程资讯AI Coding专区指南:https://aicoding.juejin.cn/aicoding
点击"阅读原文"了解详情~