package com.crawler.exception.handler; import com.crawler.exception.*; import com.crawler.exception.ExceptionHandler; import java.util.ArrayList; import java.util.List; public class ExceptionHandlerFactory { private static ExceptionHandlerFactory instance; private List handlers; private ExceptionHandlerFactory() { handlers = new ArrayList<>(); registerHandlers(); } public static ExceptionHandlerFactory getInstance() { if (instance == null) { instance = new ExceptionHandlerFactory(); } return instance; } private void registerHandlers() { handlers.add(new NetworkExceptionHandler()); handlers.add(new ParseExceptionHandler()); handlers.add(new CacheExceptionHandler()); handlers.add(new ConfigurationExceptionHandler()); handlers.add(new CrawlerExceptionHandler()); } public ExceptionHandler getHandler(Exception e) { for (ExceptionHandler handler : handlers) { if (handler.getSupportedExceptionType().isInstance(e)) { return handler; } } return null; } public void handleException(Exception e, com.crawler.view.CrawlerView view) { ExceptionHandler handler = getHandler(e); if (handler != null) { handler.handle(e, view); } else { view.showErrorMessage("未知异常: " + e.getMessage()); } } }