Concept reference: https://blog.csdn.net/carson_ho/article/details/54910518
Define a template structure and delay specific content to subclasses based on inheritance
Scenes
- Public behaviors in subclasses should be extracted and concentrated in the public parent class, and special methods should be extended as needed to avoid code duplication
advantage
提高代码复用性
Subclass common methods can be abstracted into base class methods提高了拓展性
Subclasses can dynamically implement methods according to their own characteristics反向控制
Through the operation of the parent class to call its subclass, the reverse control is realized in accordance with the "opening and closing principle"
Disadvantage
- Introduced abstract classes, each different implementation requires a subclass to achieve, resulting in an increase in the number of classes, thereby increasing the complexity of the system implementation
use
Send Get/Delete request
1. 创建抽象父类
/**
* 抽象父类
*
* @author dkangel
*/
public abstract class RequestHandler {
private static final String PROTOCOL = "https://";
private static final int SUCCESS = 200;
/**
* 生成url
*
* @param host 主机名
* @param port 端口
* @param path 路径
* @return url
*/
public String generateUrl(String host, String port, String path) {
StringBuilder url = new StringBuilder(PROTOCOL);
url.append(host).append(":").append(port).append("/").append(path);
return url.toString();
}
/**
* 发送请求,由子类实现
*
* @param url url
* @return Response
*/
public abstract Response sendRequest(String url);
/**
* 处理返回结果
*
* @param response 返回体
*/
public void handleResponse(Response response) {
if (SUCCESS == response.getHttpCode()) {
System.out.println("handle success.");
}
}
/**
* 发送请求方法
*
* @param host 主机
* @param port 端口
* @param path path
*/
public void handleRequest(String host, String port, String path) {
String url = generateUrl(host, port, path);
Response res = sendRequest(url);
handleResponse(res);
}
}
2. 实现Get/Delete子类
/**
* Get请求
*
* @author dkangel
*/
public class GetRequestHandler extends RequestHandler {
@Override
public Response sendRequest(String url) {
System.out.println("send get request.");
Response response = new Response();
response.setHttpCode(200);
return response;
}
}
/**
* Delete请求
*
* @author dkangel
*/
public class DeleteRequestHandler extends RequestHandler {
@Override
public Response sendRequest(String url) {
System.out.println("send get request.");
Response response = new Response();
response.setHttpCode(200);
return response;
}
}
3. 使用
/**
* 模板方法模式使用类
*
* @author dkangel
*/
public class Main {
public static void main(String[] args) {
String host = "localhost";
String port = "8080";
String getPath = "/app/all";
get(host, port, getPath);
String deletePath = "/app/123";
delete(host, port, deletePath);
}
/**
* 发送Get请求
*
* @param host 主机名
* @param port 端口
* @param getPath path
*/
private static void get(String host, String port, String getPath) {
RequestHandler handler = new GetRequestHandler();
handler.handleRequest(host, port, getPath);
}
/**
* 发送Delete请求
*
* @param host 主机名
* @param port 端口
* @param deletePath path
*/
private static void delete(String host, String port, String deletePath) {
RequestHandler handler = new DeleteRequestHandler();
handler.handleRequest(host, port, deletePath);
}
}