我了解责任链模式。我有一个问题。从我的责任链可以看出,如果该方法未将语言环境返回给我,则它将返回null。如果返回null,如何去链中的下一个项目?
public abstract class StandardLocaleHandler {
protected StandardLocaleHandler localeHandler;
public StandardLocaleHandler() {
this.localeHandler = null;
}
protected abstract Locale getTrueLocale(HttpServletRequest req, HttpServletResponse resp, List<String> localeList, String defaultLocale, Integer cookieAge);
public void setNext(StandardLocaleHandler localeHandler) {
this.localeHandler = localeHandler;
}
public StandardLocaleHandler getNext() {
return localeHandler;
}
}
public class GetLocaleByAvailable extends StandardLocaleHandler {
@Override
protected Locale getTrueLocale(HttpServletRequest req, HttpServletResponse resp, List<String> localeList, String defaultLocale, Integer cookieAge) {
if (isNull(req.getSession().getAttribute(LANG_ATTRIBUTE)) && isNull(req.getCookies())) {
return setAvailable(req, resp, localeList, defaultLocale, cookieAge);
}
return null;
}
}
public class GetLocaleBySession extends StandardLocaleHandler {
@Override
protected Locale getTrueLocale(HttpServletRequest req, HttpServletResponse resp, List<String> localeList, String defaultLocale, Integer cookieAge) {
if (nonNull(req.getSession().getAttribute(LANG_ATTRIBUTE))) {
LOG.debug(req.getParameter(LANG_ATTRIBUTE));
return new Locale((String) req.getSession().getAttribute(LANG_ATTRIBUTE));
}
return null;
}
}
我以这种方式形成了我的责任链:
public class ChainBuilder {
private List<StandardLocaleHandler> localeHandlers = new ArrayList<>();
public void addToFilterList(StandardLocaleHandler filter) {
if (!localeHandlers.contains(filter)) {
localeHandlers.add(filter);
} else {
throw new IllegalArgumentException("Already in the list");
}
}
public StandardLocaleHandler createChainOfResponsibility() {
for (int i = 0; i < localeHandlers.size() - 1; i++) {
localeHandlers.get(i).setNext(localeHandlers.get(i + 1));
}
return localeHandlers.get(0);
}
}
ChainBuilder builder = new ChainBuilder();
builder.addToFilterList(new GetLocaleByAvailable());
builder.addToFilterList(new GetLocaleByParam());
builder.addToFilterList(new GetLocaleBySession());
builder.addToFilterList(new GetLocaleByCookie());
StandardLocaleHandler handler = builder.createChainOfResponsibility();
return handler.getTrueLocale(req, resp, localeList, defaultLocale, cookieAge);
如果链中的下一个项目返回空值,我该怎么去?
答案 0 :(得分:2)
public abstract class StandardLocaleHandler {
public final Locale getTrueLocale() {
Locale local = getTrueLocaleInternal();
return local == null && localeHandler != null ? localeHandler.getTrueLocale() : local;
}
protected abstract Locale getTrueLocaleInternal();
}
public class GetLocaleByAvailable extends StandardLocaleHandler {
@Override
protected Locale getTrueLocaleInternal() {
// TODO logic
return null;
}
}
答案 1 :(得分:1)
您需要将代码更改为类似的内容
public class GetLocaleByAvailable extends StandardLocaleHandler {
@Override
protected Locale getTrueLocale(HttpServletRequest req, HttpServletResponse resp, List<String> localeList, String defaultLocale, Integer cookieAge) {
Locale result = null;
if (isNull(req.getSession().getAttribute(LANG_ATTRIBUTE)) && isNull(req.getCookies())) {
result = setAvailable(req, resp, localeList, defaultLocale, cookieAge);
}
if (result == null) {
StandardLocaleHandler nextHandler = getNext();
if (nextHandler == null) {
return nextHandler.getTrueLocale(....);
}
}
return result;
}
}
与另一个Handler
类相同。