Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

1. Service scenarios

There is such an application scenario in our new project. Users will import data from different platforms to store it in mongodb. Data formats of different platforms are different, and too many models need to be established to use traditional mongodb Model. Adding a new platform in later maintenance requires adding a Model class and putting it online again, which is not appropriate in terms of usability, so I took factory mode + policy mode into consideration to optimize the code structure

Flow chart of 2.

3. Code

  • service interface

``` public interface MongoEndurance { String getLivePlatfarm(); ApiRes<Collection<? >> endurance(List<? > enduranceList); ApiRes<Collection<? >> endurance(List<? > list, String collectionName); ApiRes<Collection<? >> enduranceModel(List<? > enduranceList); ApiRes<Collection<? >> query(String collectionName, Query query); ApiRes<Collection<? >> query(String collectionName); <T> ApiRes<List<T>> findAggregateList(String collectionName, Aggregation aggregation, Class<T> clazz); } ` ` `Copy the code
  • service impl

@Service @Primary public class BaseEnduranceImpl implements MongoEndurance { private static final String COLLECTION_NAME  = "DEFULT"; @Autowired protected BaseEnduranceDao baseEnduranceDao; @Override public String getLivePlatfarm() { return COLLECTION_NAME; } /** * @title mongoDB persistent @description based on COLLECTION_NAME mongo data persistent @param: list * @createTime 2021/9/15 15:59 */ @Override public ApiRes<Collection<? >> endurance(List<? > list) { Collection<? > collection = baseEnduranceDao.insertBatch(list,COLLECTION_NAME); ApiRes<Collection<? >> apiRes = new ApiRes<>(collection); return collection == null ? apiRes.fail(ResErrorCodeEnum.C.REQUEST_PARAMETER_ERROR) : apiRes; } /** * @description is initialized based on the collectionName collectionName * @param: list * @param: collectionName * @createTime 2021/9/15 16:00 */ @Override public ApiRes<Collection<? >> endurance(List<? > list, String collectionName) { Collection<? > collection = baseEnduranceDao.insertBatch(list,collectionName); ApiRes<Collection<? >> apiRes = new ApiRes<>(collection); return collection == null ? apiRes.fail(ResErrorCodeEnum.C.REQUEST_PARAMETER_ERROR) : apiRes; } /** * @description is initialized from the passed model * @param: enduranceList * @createTime 2021/9/15 16:01 */ @Override public ApiRes<Collection<? >> enduranceModel(List<? > enduranceList) { if (enduranceList == null){ return new ApiRes<Collection<? >>().fail(ResErrorCodeEnum.C.REQUEST_PARAMETER_ERROR); } Collection<? > s = baseEnduranceDao.insertBatch(enduranceList); return new ApiRes<>(s); } /** * @description (collectionName); /** * @description (collectionName); query * @createTime 2021/9/16 11:27 */ @Override public ApiRes<Collection<? >> query(String collectionName, Query Query) {/ / if the Query is null return exception information if (Query = = null | | StringUtil. IsNullOrEmpty (collectionName)) {return new ApiRes<Collection<? >>().fail(ResErrorCodeEnum.C.REQUEST_PARAMETER_ERROR); } Collection<? > s = baseEnduranceDao.query(collectionName, query); return new ApiRes<>(s); } /** * collectionName;} /** * collectionName; collectionName * @createTime 2021/9/16 15:04 */ @Override public ApiRes<Collection<? >> query(String collectionName) { if (StringUtil.isNullOrEmpty(collectionName)){ return new ApiRes<Collection<? >>().fail(ResErrorCodeEnum.C.REQUEST_PARAMETER_ERROR); } Collection<? > s = baseEnduranceDao.query(collectionName); return new ApiRes<>(s); } /** * @title mongo data query * @description CollectionName, aggregation, clazz * @createTime 2021/9/24 11:51 */ @Override public <T> ApiRes<List<T>> findAggregateList(String collectionName, Aggregation aggregation, Class<T> clazz) { if (StringUtil.isNullOrEmpty(collectionName)){ return new ApiRes<List<T>>().fail(ResErrorCodeEnum.C.REQUEST_PARAMETER_ERROR); } List<T> s = baseEnduranceDao.findAggregateList(collectionName,aggregation,clazz); return new ApiRes<>(s); }}Copy the code

​ 

@Service public class WeizanEnduranceImpl extends BaseEnduranceImpl implements MongoEndurance { private static final String COLLECTION_NAME = "weizan"; @Override public String getLivePlatfarm() { return CommonEnum.LivePlatformEnum.WEI_ZAN.getValue(); } @Override public ApiRes<Collection<? >> endurance(List<? > list) { Collection<? > collection = baseEnduranceDao.insertBatch(list,COLLECTION_NAME); ApiRes<Collection<? >> apiRes = new ApiRes<>(collection); return collection == null ? apiRes.fail(ResErrorCodeEnum.C.REQUEST_PARAMETER_ERROR) : apiRes; }}Copy the code
  • Strategy

@Component public class Strategy { @Autowired private List<MongoEndurance> mongoEnduranceList; private static Map<String, MongoEndurance> mongoStrategyMap; Public Strategy() {} @postconstruct public void init() {// mongoStrategyMap = <K,V> format for subclass livePlatfarm (Map)this.mongoEnduranceList.stream().collect(Collectors.toMap(MongoEndurance::getLivePlatfarm, (a) -> { return a; })); } /** * @title * @description * @author ANZEPENG * @createTime 2021/9/15 15:51 */ public MongoEndurance getStrategy(String livePlatfarm) {if (livePlatfarm == null){livePlatfarm = "DEFULT";  } return mongoStrategyMap.get(livePlatfarm); }}Copy the code