Template mode is commonly used as a behavioral design pattern, the main idea is to define a process skeleton code in the template, and some of the methods used in the skeleton code does not implement these methods left to subclasses to achieve. If we have a scenario of business development, our business system needs to queries from various credit channels credit report, the contents of each actuator output is not the same, but they are divided into three steps to establish a connection, access to the original report content, parsing reporting and storage. In this way, we define a template class:
public abstract class AbstractReportTemplate {
protected Logger logger = LoggerFactory.getLogger(getClass());
public final void doTask(){
doConnect();
getReport();
parseRepot();
}
/**
* 建立连接
*/
public abstract void doConnect();
/**
* 获取报告
*/
public abstract void getReport();
/**
* 解析报告并落库
*/
public abstract void parseRepot();
}
If we now want to docking A credit bureaus, that as long as we inherit this template class, to achieve the above abstract methods can, task flow unchanged.
public class AReport extends AbstractReportTemplate {
public void doConnect() {
logger.info("do connect");
}
public void getReport() {
logger.info("get report");
}
public void parseRepot() {
logger.info("parseReport");
}
}
Method call:
public static void main(String[] args){
AbstractReportTemplate aReport = new AReport();
aReport.doTask();
}
Visible, template mode very simple to implement. Now I will introduce several template pattern in the source code.
1.jdk the InputStream class is a template class, which defines the abstract read method
public int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte)c;
int i = 1;
try {
for (; i < len ; i++) {
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte)c;
}
} catch (IOException ee) {
}
return i;
}
public abstract int read() throws IOException;
There are many implementation class, as shown below:
Here is the ByteArrayInputStream of:
public synchronized int read() {
return (pos < count) ? (buf[pos++] & 0xff) : -1;
}
The data source is used to initialize 2.jdbc AbstractDataSourceInitializer
@PostConstruct
protected void initialize() {
if (!isEnabled()) {
return;
}
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
String schemaLocation = getSchemaLocation();
if (schemaLocation.contains(PLATFORM_PLACEHOLDER)) {
String platform = getDatabaseName();
schemaLocation = schemaLocation.replace(PLATFORM_PLACEHOLDER, platform);
}
populator.addScript(this.resourceLoader.getResource(schemaLocation));
populator.setContinueOnError(true);
customize(populator);
DatabasePopulatorUtils.execute(populator, this.dataSource);
}
protected abstract DataSourceInitializationMode getMode();
protected abstract String getSchemaLocation();
The realization BatchDataSourceInitializer
@Override
protected DataSourceInitializationMode getMode() {
return this.properties.getInitializeSchema();
}
@Override
protected String getSchemaLocation() {
return this.properties.getSchema();
}
Text Source: https: //github.com/jinjunzhu/design-pattern.git
Micro-channel public number, welcome attention, learning to grow together