最直观的构建者,很多框架都这么用,为什么?因为参数太多,直接(*,*,*)的话容易搞混,而且默认值放在外面很乱
reader = new ReliableSpoolingFileEventReader.Builder() .spoolDirectory(directory) .completedSuffix(completedSuffix) .includePattern(includePattern) .ignorePattern(ignorePattern) .trackerDirPath(trackerDirPath) .annotateFileName(fileHeader) .fileNameHeader(fileHeaderKey) .annotateBaseName(basenameHeader) .baseNameHeader(basenameHeaderKey) .deserializerType(deserializerType) .deserializerContext(deserializerContext) .deletePolicy(deletePolicy) .inputCharset(inputCharset) .decodeErrorPolicy(decodeErrorPolicy) .consumeOrder(consumeOrder) .recursiveDirectorySearch(recursiveDirectorySearch) .build();
里面的实现很简单,给了默认值和设置方法,最后构建还是把前面的各种参数用构造器构造出来
private File spoolDirectory; private String completedSuffix = SpoolDirectorySourceConfigurationConstants.SPOOLED_FILE_SUFFIX; private String includePattern = SpoolDirectorySourceConfigurationConstants.DEFAULT_INCLUDE_PAT; private String ignorePattern = SpoolDirectorySourceConfigurationConstants.DEFAULT_IGNORE_PAT; private String trackerDirPath = SpoolDirectorySourceConfigurationConstants.DEFAULT_TRACKER_DIR; private Boolean annotateFileName = SpoolDirectorySourceConfigurationConstants.DEFAULT_FILE_HEADER; private String fileNameHeader = SpoolDirectorySourceConfigurationConstants.DEFAULT_FILENAME_HEADER_KEY; private Boolean annotateBaseName = SpoolDirectorySourceConfigurationConstants.DEFAULT_BASENAME_HEADER; private String baseNameHeader = SpoolDirectorySourceConfigurationConstants.DEFAULT_BASENAME_HEADER_KEY; private String deserializerType = SpoolDirectorySourceConfigurationConstants.DEFAULT_DESERIALIZER; private Context deserializerContext = new Context(); private String deletePolicy = SpoolDirectorySourceConfigurationConstants.DEFAULT_DELETE_POLICY; private String inputCharset = SpoolDirectorySourceConfigurationConstants.DEFAULT_INPUT_CHARSET; private DecodeErrorPolicy decodeErrorPolicy = DecodeErrorPolicy.valueOf( SpoolDirectorySourceConfigurationConstants.DEFAULT_DECODE_ERROR_POLICY .toUpperCase(Locale.ENGLISH)); private ConsumeOrder consumeOrder = SpoolDirectorySourceConfigurationConstants.DEFAULT_CONSUME_ORDER; private boolean recursiveDirectorySearch = SpoolDirectorySourceConfigurationConstants.DEFAULT_RECURSIVE_DIRECTORY_SEARCH; public Builder spoolDirectory(File directory) { this.spoolDirectory = directory; return this; } public Builder completedSuffix(String completedSuffix) { this.completedSuffix = completedSuffix; return this; } public Builder includePattern(String includePattern) { this.includePattern = includePattern; return this; } public Builder ignorePattern(String ignorePattern) { this.ignorePattern = ignorePattern; return this; } public Builder trackerDirPath(String trackerDirPath) { this.trackerDirPath = trackerDirPath; return this; } public Builder annotateFileName(Boolean annotateFileName) { this.annotateFileName = annotateFileName; return this; } public Builder fileNameHeader(String fileNameHeader) { this.fileNameHeader = fileNameHeader; return this; } public Builder annotateBaseName(Boolean annotateBaseName) { this.annotateBaseName = annotateBaseName; return this; } public Builder baseNameHeader(String baseNameHeader) { this.baseNameHeader = baseNameHeader; return this; } public Builder deserializerType(String deserializerType) { this.deserializerType = deserializerType; return this; } public Builder deserializerContext(Context deserializerContext) { this.deserializerContext = deserializerContext; return this; } public Builder deletePolicy(String deletePolicy) { this.deletePolicy = deletePolicy; return this; } public Builder inputCharset(String inputCharset) { this.inputCharset = inputCharset; return this; } public Builder recursiveDirectorySearch(boolean recursiveDirectorySearch) { this.recursiveDirectorySearch = recursiveDirectorySearch; return this; } public Builder decodeErrorPolicy(DecodeErrorPolicy decodeErrorPolicy) { this.decodeErrorPolicy = decodeErrorPolicy; return this; } public Builder consumeOrder(ConsumeOrder consumeOrder) { this.consumeOrder = consumeOrder; return this; } public ReliableSpoolingFileEventReader build() throws IOException { return new ReliableSpoolingFileEventReader(spoolDirectory, completedSuffix, includePattern, ignorePattern, trackerDirPath, annotateFileName, fileNameHeader, annotateBaseName, baseNameHeader, deserializerType, deserializerContext, deletePolicy, inputCharset, decodeErrorPolicy, consumeOrder, recursiveDirectorySearch); }