package com.try2better.practice.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.try2better.practice.service.version.VersionsService; @Controller @RequestMapping("/version") public class VersionController { @Autowired private VersionsService versionsService; @ResponseBody @RequestMapping(value="/ping") public String ping(String version){ return versionsService.execute(version); } }
package com.try2better.practice.service.version; import java.util.HashSet; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class VersionsService { private AbastractService headVersionService; @Autowired private Version1Service version1Service; @Autowired private Version2Service version2Service; @Autowired private Version3Service version3Service; public String execute(String version){ //構造責任鏈 buildNextService(); return headVersionService.execute(version); } private void buildNextService(){ AbastractService[] abastractServiceArray = new AbastractService[]{ version1Service, version2Service, version3Service }; if(abastractServiceArray == null || abastractServiceArray.length < 1){ throw new RuntimeException("沒有處理的service!"); } if(hasNull(abastractServiceArray)){ throw new RuntimeException("有service爲null的狀況!"); } if(isRepetitive(abastractServiceArray)){ throw new RuntimeException("有循環依賴的狀況,有可能形成死循環!"); } for (int i = abastractServiceArray.length - 2; i >= 0; i--) { abastractServiceArray[i].setNextAbastractService(abastractServiceArray[i + 1]); } headVersionService = abastractServiceArray[0]; } /** * 判斷service有無爲空的狀況 * * @param array * @return * @author zhouh-b * @time 2017年9月13日 */ private <T> boolean hasNull(T[] array){ for (int i = 0; i < array.length; i++) { if(array[i] == null){ return true; } } return false; } /** * 判斷有無重複的狀況,若是有可能形成死循環 * * @param array * @return * @author zhouh-b * @time 2017年9月13日 */ private <T> boolean isRepetitive(T[] array){ Set<T> set = new HashSet<T>(); for (int i = 0; i < array.length; i++) { set.add(array[i]); } return (array.length != set.size()); } }
package com.try2better.practice.service.version; public abstract class AbastractService { private AbastractService nextAbastractService; //公用的方法 public void commonMethods(){ //... } public abstract boolean canHandle(String version); public abstract String doBusiness(String version); public String execute(String version) { if(canHandle(version)){ return doBusiness(version); }else{ AbastractService nextService = getNextAbastractService(); if(nextService == null){ throw new RuntimeException("沒有匹配的service可以處理version:" + version); } return nextService.execute(version); } } public AbastractService getNextAbastractService() { return nextAbastractService; } public void setNextAbastractService(AbastractService nextAbastractService) { this.nextAbastractService = nextAbastractService; } }
package com.try2better.practice.service.version; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Service; @Service public class Version1Service extends AbastractService{ @Override public boolean canHandle(String version) { return StringUtils.isNotBlank(version) && version.equals("1"); } @Override public String doBusiness(String version) { return "version 1 service handle"; } }
package com.try2better.practice.service.version; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Service; @Service public class Version2Service extends AbastractService{ @Override public boolean canHandle(String version) { return StringUtils.isNotBlank(version) && version.equals("2"); } @Override public String doBusiness(String version) { return "version 2 service handle"; } }
package com.try2better.practice.service.version; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Service; @Service public class Version3Service extends AbastractService{ @Override public boolean canHandle(String version) { return StringUtils.isNotBlank(version) && version.equals("3"); } @Override public String doBusiness(String version) { return "version 3 service handle"; } }