ホームページ  >  記事  >  Java  >  Springboot で switch-case を削除する方法

Springboot で switch-case を削除する方法

王林
王林転載
2023-05-14 19:49:041018ブラウズ

基本的なロジックは次のとおりです。

 String event = crsRequest.getEvent();
    CRSResponse crsResponse = null;
    switch (event) {
      case CRSRequestEvent.APP_START:
        crsResponse = processAppStartCommand(crsRequest);
        break;
      case CRSRequestEvent.INIT_COMPLETE:
        crsResponse = processInitCompleteCommand(crsRequest);
        break;
      case CRSRequestEvent.COLLECT_COMPLETE:
        crsResponse = processCollectCompleteCommand(crsRequest);
        break;
      case CRSRequestEvent.COLLECT_NO_INPUT:
        crsResponse = processCollectNoInputCommand(crsRequest);
        break;
      case CRSRequestEvent.PLAY_COMPLETE:
        crsResponse = processPlayCompleteCommand(crsRequest);
        break;
      default:
    }

書いてみると、イベントが増えるとコードが非常に長くなり、各イベントの処理機能も集中していることがわかります。 1 つのクラスで、メンテナンスが簡単です。そこで、検索と学習を通じて、Springboot のアノテーション、ストラテジー モード、シンプル ファクトリを使用して switch-case を排除できることがわかりました。

リファクタリング

構造の定義

public enum CRSEvent {
  APP_START("APP_START"),
  INIT_COMPLETE("INIT_COMPLETE"),
  PLAY_COMPLETE("PLAY_COMPLETE"),
  COLLECT_COMPLETE("COLLECT_COMPLETE"),
  COLLECT_NO_INPUT("COLLECT_NO_INPUT"),
  APP_END("APP_END"),
  RESP_ERROR_CMD("RESP_ERROR_CMD");

  private String event;

  CRSEvent(String event){
    this.event = event;
  }
  
  public String getEvent() {
    return event;
  }

  public void setEvent(String event) {
    this.event = event;
  }
}

アノテーションの定義

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CRSEventAnnotation {

  CRSEvent value();
}

定義イベント処理Interface

public interface EventProcess {
  CRSResponse execute(CRSRequest resquest);
}

常時処理クラスはこのインターフェイスを実装する必要があります。このうち、execute はイベント処理メソッドです。

特定の時刻処理クラスを記述します

次に、イベント処理クラスを 1 つずつ記述し、次のようにします。例:

@Component("appStartProcess")
@CRSEventAnnotation(value = CRSEvent.APP_START)
public class AppStartProcess implements EventProcess{

  @Override
  public CRSResponse execute(CRSRequest resquest) {
    CRSResponse response = new CRSResponse();
    response.setCommand(CRSResponseCmd.IVR_SESSION_INIT);
    CRSResponse.Message message = new CRSResponse.Message();
    message.setTts_vid("65580");
    message.setTts_speed("120");
    response.setMessage(message);
    return response;
  }
}

SpringContext ツール クラスを定義します

@Component
public class SpringContextUtil implements ApplicationContextAware{

  private ApplicationContext context;

  public ApplicationContext getContext(){
    return context;
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.context = applicationContext;
  }
}

イベント処理クラス ファクトリを定義してさまざまなイベント処理オブジェクトを生成します

@Component
public class EventProcessFactory {

  @Autowired
  SpringContextUtil contextUtil;

  private static Map<CRSEvent, EventProcess> eventProcessMap = new ConcurrentHashMap<>();

  public EventProcessFactory() {
    Map<String, Object> beanMap = contextUtil.getContext().getBeansWithAnnotation(CRSEventAnnotation.class);

    for (Object evetProcess : beanMap.values()) {
      CRSEventAnnotation annotation = evetProcess.getClass().getAnnotation(CRSEventAnnotation.class);
      eventProcessMap.put(annotation.value(), (EventProcess) evetProcess);
    }
  }
  
  public static EventProcess createEventProcess(CRSEvent event){
    return eventProcessMap.get(event);
  }
}

コード変更の呼び出し

 CRSEvent crsEvent = CRSEvent.valueOf(crsRequest.getEvent());
 EventProcess eventProcess = EventProcessFactory.createEventProcess(crsEvent);
 if (eventProcess != null){
   return eventProcess.execute(crsRequest);
 }
return null;

この方法では、コード内に switch-case がなく、イベントの追加も非常に簡単で、EventProcess インターフェイスを実装するだけです。

以上がSpringboot で switch-case を削除する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。