马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
接上一篇文章,如今继承讲一下Spring框架的设计模式应用
4. 观察者模式 (Observer Pattern)
描述:
Spring 中的应用:
变乱机制: Spring 提供了变乱发布和监听机制,允许对象之间举行松耦合的通讯。
ApplicationEventPublisher: 用于发布变乱。
ApplicationListener: 用于监听变乱。
示例:
- // 定义一个事件
- 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 中的应用:
示例:
- 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 中的应用:
示例:
- 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)
- /// ***你们的关注是我一直写作的动力
- System.out.println("请添加我的绿色公主号:");
- System.out.println("Java知识日历");
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |