接上一篇文章,现在继续讲一下Spring框架的设计模式应用
4. 观察者模式 (Observer Pattern)
描述:
- 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。当主题对象发生变化时,它的所有依赖者(观察者)都会收到通知并自动更新。
Spring 中的应用:
- 
事件机制: Spring 提供了事件发布和监听机制,允许对象之间进行松耦合的通信。 
- 
ApplicationEventPublisher: 用于发布事件。 
- 
ApplicationListener: 用于监听事件。 
示例:
            
            
              go
              
              
            
          
          // 定义一个事件
publicclass CustomEvent extends ApplicationEvent {
    private String message;
    public CustomEvent(Object source, String message) {
        super(source);
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}
// 发布事件
@Service
publicclass EventPublisherService implements ApplicationEventPublisherAware {
    private ApplicationEventPublisher publisher;
    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
    }
    public void publishCustomEvent(final String message) {
        System.out.println("Publishing custom event. ");
        CustomEvent customEvent = new CustomEvent(this, message);
        publisher.publishEvent(customEvent);
    }
}
// 监听事件
@Component
publicclass CustomEventListener implements ApplicationListener<CustomEvent> {
    @Override
    public void onApplicationEvent(CustomEvent event) {
        System.out.println("Received custom event - " + event.getMessage());
    }
}5. 策略模式 (Strategy Pattern)
描述:
- 策略模式定义了一系列算法,并将每个算法封装起来,使它们可以互换。
Spring 中的应用:
- 
Routing DataSource: 在多数据源环境下,可以根据不同的条件选择不同的数据库连接。 
- 
Transaction Management: 可以配置不同的事务管理器。 
示例:
            
            
              go
              
              
            
          
          public interface PaymentStrategy {
    void pay(int amount);
}
publicclass CreditCardPaymentStrategy implements PaymentStrategy {
    @Override
    public void pay(int amount) {
        System.out.println("Paid with credit card: $" + amount);
    }
}
publicclass PayPalPaymentStrategy implements PaymentStrategy {
    @Override
    public void pay(int amount) {
        System.out.println("Paid with PayPal: $" + amount);
    }
}
publicclass ShoppingCart {
    private PaymentStrategy paymentStrategy;
    public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }
    public void checkout(int amount) {
        paymentStrategy.pay(amount);
    }
}6. 模板方法模式 (Template Method Pattern)
描述:
- 模板方法模式定义了一个算法的骨架,而将一些步骤延迟到子类中实现。
Spring 中的应用:
- 
JdbcTemplate: 提供了一个执行 SQL 查询的基本模板,具体的查询逻辑由子类或回调函数实现。 
- 
HibernateTemplate: 类似地,提供了与 Hibernate 交互的基本模板。 
示例:
            
            
              go
              
              
            
          
          public abstractclass Game {
    protected abstract void initialize();
    protected abstract void startPlay();
    protected abstract void endPlay();
    // Template method
    public final void play() {
        // Initialize the game
        initialize();
        // Start the game
        startPlay();
        // End the game
        endPlay();
    }
}
publicclass Cricket extends Game {
    @Override
    protected void endPlay() {
        System.out.println("Cricket Game Finished!");
    }
    @Override
    protected void initialize() {
        System.out.println("Cricket Game Initialized! Enjoy the game.");
    }
    @Override
    protected void startPlay() {
        System.out.println("Cricket Game Started. Enjoy the game!");
    }
}下集预告
下一篇文章将写上Spring框架继续介绍下面三种设计模式的应用:
7. 建造者模式 (Builder Pattern)
8. 装饰器模式 (Decorator Pattern)
9. 适配器模式 (Adapter Pattern)
            
            
              go
              
              
            
          
          /// ***你们的关注是我一直写作的动力
System.out.println("请添加我的绿色公主号:");
System.out.println("Java知识日历");