在现代软件系统设计中,设计模式不仅仅是编程技巧,更是架构思维的体现。架构师在设计系统时,需要通过设计模式提升系统的可扩展性、可维护性、复用性和稳定性。本文将从架构师的角度,分析常用设计模式的应用场景,并给出 Java 示例。
一、设计模式的架构价值
-
解耦业务逻辑
-
通过模式将变化点隔离,降低模块间耦合。
-
例如策略模式将不同算法实现与业务逻辑解耦。
-
-
提高系统可扩展性
-
模式提供统一的扩展点,使新增功能无需修改已有代码。
-
例如工厂方法模式、抽象工厂模式提供创建对象的扩展能力。
-
-
增强系统复用性
-
模式通过抽象和组合,实现通用解决方案,减少重复开发。
-
例如模板方法模式复用业务流程骨架,只修改可变部分。
-
-
提升系统稳定性
-
模式定义了严格的交互规则,减少意外副作用。
-
例如观察者模式保证事件传播的可控性。
-
二、结构型设计模式示例:桥接模式(Bridge)
桥接模式用于将抽象部分与实现部分分离,便于独立扩展。对于大型系统,架构师常用它来隔离接口和实现,避免类爆炸。
// Implementor接口
interface PaymentGateway {
void pay(double amount);
}
// ConcreteImplementor
class Alipay implements PaymentGateway {
@Override
public void pay(double amount) {
System.out.println("支付宝支付:" + amount + "元");
}
}
class WeChatPay implements PaymentGateway {
@Override
public void pay(double amount) {
System.out.println("微信支付:" + amount + "元");
}
}
// Abstraction
abstract class Payment {
protected PaymentGateway gateway;
public Payment(PaymentGateway gateway) {
this.gateway = gateway;
}
abstract void processPayment(double amount);
}
// RefinedAbstraction
class OnlinePayment extends Payment {
public OnlinePayment(PaymentGateway gateway) {
super(gateway);
}
@Override
void processPayment(double amount) {
gateway.pay(amount);
}
}
// 使用示例
public class BridgePatternDemo {
public static void main(String[] args) {
Payment payment1 = new OnlinePayment(new Alipay());
payment1.processPayment(100);
Payment payment2 = new OnlinePayment(new WeChatPay());
payment2.processPayment(200);
}
}
架构价值:
-
支付方式(Alipay/WeChatPay)可独立扩展。
-
支付业务逻辑(OnlinePayment)可独立扩展。
-
系统避免类爆炸和耦合问题,适合大规模系统。
三、行为型设计模式示例:策略模式(Strategy)
策略模式用于定义一系列算法,并使它们可互换。架构师通常用策略模式实现业务规则的动态切换。
// 策略接口
interface PromotionStrategy {
void applyPromotion();
}
// 具体策略
class CouponPromotion implements PromotionStrategy {
public void applyPromotion() {
System.out.println("使用优惠券减免10元");
}
}
class DiscountPromotion implements PromotionStrategy {
public void applyPromotion() {
System.out.println("打九折优惠");
}
}
// 上下文
class PromotionContext {
private PromotionStrategy strategy;
public PromotionContext(PromotionStrategy strategy) {
this.strategy = strategy;
}
public void executePromotion() {
strategy.applyPromotion();
}
}
// 使用示例
public class StrategyPatternDemo {
public static void main(String[] args) {
PromotionContext context1 = new PromotionContext(new CouponPromotion());
context1.executePromotion();
PromotionContext context2 = new PromotionContext(new DiscountPromotion());
context2.executePromotion();
}
}
架构价值:
-
业务规则可动态切换,系统可配置化。
-
新增促销策略无需修改原有代码,遵循开闭原则。
四、创建型设计模式示例:工厂方法模式(Factory Method)
工厂方法模式用于定义一个创建对象的接口,让子类决定实例化哪一个类,架构师在微服务或插件化架构中常用此模式。
// 产品接口
interface Notification {
void notifyUser();
}
// 具体产品
class EmailNotification implements Notification {
public void notifyUser() {
System.out.println("发送邮件通知");
}
}
class SMSNotification implements Notification {
public void notifyUser() {
System.out.println("发送短信通知");
}
}
// 工厂接口
interface NotificationFactory {
Notification createNotification();
}
// 具体工厂
class EmailFactory implements NotificationFactory {
public Notification createNotification() {
return new EmailNotification();
}
}
class SMSFactory implements NotificationFactory {
public Notification createNotification() {
return new SMSNotification();
}
}
// 使用示例
public class FactoryMethodDemo {
public static void main(String[] args) {
NotificationFactory factory1 = new EmailFactory();
Notification notification1 = factory1.createNotification();
notification1.notifyUser();
NotificationFactory factory2 = new SMSFactory();
Notification notification2 = factory2.createNotification();
notification2.notifyUser();
}
}
架构价值:
-
系统易于扩展,新增通知方式只需添加新的工厂与产品类。
-
避免直接实例化对象,降低耦合,提高可测试性。
五、架构师使用设计模式的思路
-
从系统演化角度选模式
-
考虑业务可能扩展点和变化点,选择合适模式进行解耦。
-
-
从团队协作角度选模式
-
模式明确责任划分,降低模块间沟通成本。
-
-
从性能与复杂度权衡
-
不滥用模式,避免过度设计;优先解决实际问题。
-
-
模式组合使用
-
在大型系统中,常常组合使用多个模式,例如策略模式 + 工厂模式 + 桥接模式,形成灵活可扩展的架构体系。
-
六、总结
设计模式在架构师视角下,不只是“写代码的技巧”,而是应对复杂系统变化的架构手段。通过模式,系统可以更易扩展、维护、测试和演化。Java 的面向对象特性与丰富的接口抽象,使其成为设计模式的理想语言。
核心原则:
-
解耦业务逻辑
-
提供系统扩展点
-
遵循开闭原则
-
降低未来维护成本
1089

被折叠的 条评论
为什么被折叠?



