dimanche 26 mars 2017

What is the best pattern that would suit, sending notifications for respective actions?

Here is my servlet which does too many actions like the following

public class SampleServlet extends HttpServlet {
      public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {
        ServletActions.valueOf("action1").execute(params);  //Invoking respective action    
}
}

Since my servlet handles too many actions, I have implemeneted them with enum following Strategy Pattern.

Like,

public enum ServletActions {

   ACTION_1 {
      //Do some actions
      //Notify `a` regarding the actions
      //Notify `b` regarding the action performed
      //Construct response json
   },

   ACTION_2 {
      //Do some actions
      //Notify `d` regarding the actions
      //Notify `e` regarding the action performed
      //Construct response json
   },

   ACTION_3 {
      //Do some actions
      //Notify `d` regarding the actions
      //Notify `b` regarding the action performed
      //Construct response json
   },

   ACTION_4 {
   },

   ACTION_5 {
   },

   ACTION_6 {
   },

   ACTION_7 {
   };
}

a, b, c here are like sending in-progress notification, success notification, sending notification to agent, sending notification to client etc.

Handling all my notification sending task within the action makes the code too clumsy. So thought of implementing the same with

   public enum ServletActionsNotifier {

       ACTION_1_NOTIFIER {
          public void sendNotification() {
               //Send agent notification
               //Send progress notification
          }
       },

       ACTION_2_NOTIFIER {
         public void sendNotification() {
               //Send task notification
               //Send progress notification 
         }
       },

       ACTION_3_NOTIFIER {
          public void sendNotification() {
               //Send feed notification
               //Send success notification 
         }
       },

       ACTION_4_NOTIFIER {
       },

       ACTION_5_NOTIFIER {
       },

       ACTION_6_NOTIFIER {
       },

       ACTION_7_NOTIFIER {
       };
      abstract void sendNotification();
    }

Someone suggest me an alternative for implementing the notification processor in a better way in this case.

Aucun commentaire:

Enregistrer un commentaire