Pull to refresh

Comments 1

ИМХО:
данный пример будет кошерен, если использовать в ParentNotificationCreator паттерн стратегия.

Например (можно и без рефлексии, если добавить вспомогательный класс или использовать method reference из java 8):
public class ParentNotificationCreator extends CoreNotificationCreator {

   private Map<String, Class<? extends CoreNotification>> messageTypeForNotification;

   public ParentNotificationCreator(Context context, Map<String, Class<? extends CoreNotification>> messageTypeForNotification) {
       super(context);
       this.messageTypeForNotification = messageTypeForNotification;
   }

   @Nullable
   @Override
   protected CoreNotification factoryMethod(String messageType, RemoteMessage remoteMessage) {
       Class<? extends CoreNotification> notificationClass =  messageTypeForNotification.get(messageType);
       if (notificationClass != null) {
             return notificationClass.getConstructor(RemoteMessage.class).newInstance(remoteMessage);
       }
       return null;
   }
}


И создавать инстанс этого класс таким способом:
Map<String, Class> messageTypeForNotification = new HashMap<>();
messageTypeForNotification.put(PickUpNotification.TYPE,  PickUpNotification.class);
messageTypeForNotification.put(GradeNotification.TYPE,  GradeNotification.class);
new ParentNotificationCreator(context, messageTypeForNotification);


Это позволит добавлять новые обработчики без изменения NotificationCretor, а также сделать общий NotificationCretor как для учителей так и для родителей.
Sign up to leave a comment.

Articles