观察者模式在个人网站邮件发布与订阅中的实践

观察者模式在个人网站邮件发布与订阅中的实践

4/28/2017 10:02:59 AM

最近在使用SSM框架开发自己个人的学习网站过程中使用到了设计模式中非常常见同时非常使用的观察者模式。所谓设计模式最重要的并非他的具体逻辑,而在于他的设计动机和实现原理。观察者模式的设计动机就是使得作为观察者在主动接收到来自被观察者的动作或消息时发生联动变化,从而实现消息的有效传递。下面是具体的核心代码:

  • 邮件发布者(被观察者)抽象,包含核心邮件订阅者列表(EmailPublishObserver.java)

     public abstract class EmailPublishObserver {
         public List<EmailReceiverImp> emailReceiverImps=new ArrayList<EmailReceiverImp>();//订阅者列表
    
         public void addEmailReceiver(EmailReceiverImp emailReceiverImp){
         this.emailReceiverImps.add(emailReceiverImp);
         }
    
         public void deleteEmailReceiver(EmailReceiverImp emailReceiverImp){
         this.emailReceiverImps.remove(emailReceiverImp);
         }
         //订阅列表更新的抽象方法
         public abstract void update(List<EmailReceiverImp> emailReceiverImps);
         //邮件发布抽象方法
         /**
          * @param filePath 所需发送邮件的路径地址
          */
         public abstract void publishEmail(String[] filePath,String emailContent);

    }

  • 邮件发布实现(emailPublisher.java)

     /**
      * 邮件广播发布
      */
     @Service("emailPublisher")
     public class EmailPublisher extends EmailPublishObserver{
             private List<EmailReceiverImp> emailReceiverImps;//关联观察者列表
    
             @Override
             public void update(List<EmailReceiverImp> emailReceiverImps) {
                 this.emailReceiverImps=emailReceiverImps;
             }
    
             @Override
             public void publishEmail(String[] filePath,String emailContent) {
                 for (EmailReceiverImp emailReceiverImp:emailReceiverImps){
                     emailReceiverImp.setFilePath(filePath);
                     emailReceiverImp.setEmailContent(emailContent);
                     emailReceiverImp.subscribe();
             }
         }
     }
  • 邮件订阅者(观察者)接口

          /**
           *Created by Administrator on 2017/3/23.
           *邮件订阅接口
           */
          public interface IEmailReceiver {
          void subscribe();
          }
  • 邮件订阅者实现

    public class EmailReceiverImp implements IEmailReceiver {

      private Email email;
      private String[] filePath;
      private String emailContent;
    
    
      public String[] getFilePath() {
          return filePath;
      }
    
      public void setFilePath(String[] filePath) {
          this.filePath = filePath;
      }
    
      public String getEmailContent() {
          return emailContent;
      }
    
      public void setEmailContent(String emailContent) {
          this.emailContent = emailContent;
      }
    
      public Email getEmail() {
          return email;
      }
    
      public void setEmail(Email email) {
          this.email = email;
      }
    
    
      public EmailReceiverImp(){
    
      }
      public EmailReceiverImp(Email email){
          this.email=email;
      }
    
      //此方法作为邮件发布后自动接收邮件(广播到所有订阅者)
      @Override
      public void subscribe() {
          BlogEmailSender emailSender=new BlogEmailSender(email);
          emailSender.setSendFileName(filePath);
          emailSender.setContent(emailContent);
          Thread sendThread=new Thread(emailSender);//创建邮件发送线程
          sendThread.start();//启动线程
          }
      }

网站界面
946447-20170519124904697-238571.png

946447-20170519125024166-2052140393.png

相关个人博客项目已经在GitHub开源,欢迎交流完善 -> https://github.com/kingxiusam/EasyBlog

转载于:https://www.cnblogs.com/ZengHuangDong/p/6781081.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值