设计模式深度解析:从六大原则到Spring源码,面试通关全攻略
2026/6/1 9:07:06 网站建设 项目流程

设计模式深度解析:从六大原则到Spring源码

一、为什么要学设计模式?

很多程序员对设计模式的认知停留在面试要考四个字,但设计模式真正的价值在于看懂优秀框架源码、写出可维护的代码、在团队协作中用统一语言沟通。

场景一:接手离职同事项目,类名满天飞但看不懂。

场景二:面试问Spring用了哪些设计模式,答不出来直接pass。

场景三:技术评审时if-else方案被否,不懂策略模式。

二、设计模式的本质

设计模式=建筑行业的标准图纸。六大设计原则是地基:单一职责、开闭原则、里氏替换、依赖倒置、接口隔离、迪米特法则。

策略模式=开闭+单一职责;工厂模式=依赖倒置+开闭;观察者=开闭+迪米特。三个层次:知道(背名字)→会用(掌握场景)→忘记(编码本能)。

三、核心模式实战

3.1 策略模式

定义DiscountStrategy接口 → 各策略类独立封装(VipDiscountStrategy、SuperVipDiscountStrategy)→ StrategyFactory管理路由。加新策略只需新建类加@Component注解,不修改原有代码,完美遵循开闭原则。

public interface DiscountStrategy { BigDecimal discount(BigDecimal originPrice); String getType(); } @Component public class VipDiscountStrategy implements DiscountStrategy { public BigDecimal discount(BigDecimal originPrice) { return originPrice.multiply(new BigDecimal("0.8")); } public String getType() { return "vip"; } } @Component public class DiscountStrategyFactory { private final Map<String, DiscountStrategy> map = new HashMap<>(); public DiscountStrategyFactory(List<DiscountStrategy> strategies) { for (DiscountStrategy s : strategies) map.put(s.getType(), s); } public BigDecimal calc(String type, BigDecimal price) { DiscountStrategy s = map.get(type); return s == null ? price : s.discount(price); } }
3.2 观察者模式

Spring事件机制就是观察者模式:OrderPaidEvent → SmsListener/PointsListener/InvoiceListener各监听 → OrderService完全不知道谁在监听。加CouponListener零改动。精髓:解耦发布者和订阅者

public class OrderPaidEvent extends ApplicationEvent { private final Long orderId; public OrderPaidEvent(Object source, Long orderId) { super(source); this.orderId = orderId; } } @Component public class SmsListener { @EventListener public void handle(OrderPaidEvent event) { /* 发短信 */ } } @Component public class PointsListener { @EventListener public void handle(OrderPaidEvent event) { /* 发积分 */ } }
3.3 单例模式

双重检查锁定必须加volatile(禁止指令重排序)。集群环境单例无意义需分布式锁(Redis/ZK)。Spring Bean是IoC容器级别单例(同BeanName唯一)vs 设计模式JVM级别单例。

四、Spring用了哪些设计模式(面试必考)

设计模式Spring中的应用面试要答出
工厂模式BeanFactory/ApplicationContextIoC容器=工厂+单例池
单例模式Bean默认scope=singleton容器级别vs JVM级别
代理模式AOP动态代理(JDK/CGLIB)@Transactional本质代理增强
模板方法JdbcTemplate/RestTemplate父类骨架子类步骤
策略模式Resource多种实现同接口不同实现
观察者模式@EventListener事件驱动解耦
适配器模式HandlerAdapter统一处理不同Controller
装饰器模式BeanDefinitionDecorator不改变原类动态增强
责任链模式Filter Chain请求依次经过处理器

五、避坑指南

现象正确做法
过度设计3种if-else改成策略+工厂,20行变200行变化点小于3且不频繁,if-else够用
为了模式而模式先想用哪个模式再想解决什么问题先分析问题再找对应模式
不理解意图背了8种单例写法不知为什么理解为什么比怎么写重要10倍
忽略原则只学23种模式不懂六大原则懂了开闭原则自己就能发明策略模式
模式滥用所有地方都用单例/工厂/代理每个模式增加类数量,滥用导致类爆炸

六、面试速查表

考点频率问法
六大原则5星说说开闭原则,举项目例子
策略模式5星if-else太多怎么优化
单例模式4星双重锁定为什么加volatile
工厂模式4星工厂和策略模式有什么区别
观察者模式4星MQ和观察者模式有什么区别
代理模式4星JDK动态代理和CGLIB区别

七、总结

设计模式核心不在背定义,在于理解设计原则和应对变化的思维。推荐路线:六大原则(3天)→高频6模式(1周)→Spring源码中的模式(2周)→工作中刻意练习。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询