先说下前因后果吧,在之前编程过程中,其实早就接触到了链式编程了,当时是用elastic(做搜索的),spring boot 封装的提供jar包就是链式编程调用了,当时也没注意到底怎么实现的,就在昨晚,没错,就在昨晚,在用hutool工具包,它里面提供的http访问,就是用链式编程实现的,相当好的编程方式,为啥之前没注意到,昨晚注意到了捏,这个源于我之前也封装过简易http访问工具包,用重载实现的,当时写完就感觉代码,维护性和扩展性还有参数赋值真的弱爆了…emmmm…先说下链式编程是什么吧…
它就是可以通过"."一直点出方法,然后最后使用一个方法进行返回
比如:IPTaoBaoUtil.start(“183.250.223.138”).getAll().build();
优点:1.代码可读性高 2.容易操作访问 2.维护很强
这边我的例子是 访问淘宝地址的接口,这个就是可以根据ip,然后返回具体省市县,先贴出代码:
package com.qzqt.logistics.config;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONObject;
/**
* 淘宝访问ip
* @author ornage
* @date 2018/12/5
*/
public class IPTaoBaoUtil {
private static final String COUNTRY_V = "COUNTRY_V";
private static final String REGION_V = "REGION_V";
private static final String CITY_V = "CITY_V";
private HttpResponse addressResponse;
private String country = "";
private String region = "";
private String city = "";
public static IPTaoBaoUtil start(String ipAddress) throws Exception {
return new IPTaoBaoUtil().visit(ipAddress);
}
private IPTaoBaoUtil visit(String ipAddress) throws Exception {
HttpResponse execute = HttpRequest.get("http://ip.taobao.com/service/getIpInfo" +
".php?ip=" + ipAddress).execute();
this.addressResponse = execute;
return this;
}
private IPTaoBaoUtil responseValidation() throws Exception {
if (this.addressResponse.getStatus() != HttpStatus.HTTP_OK) {
throw new Exception("访问失败");
}
return this;
}
private IPTaoBaoUtil get(String type) throws Exception {
this.responseValidation();
JSONObject addressJson = new JSONObject(this.addressResponse.body());
JSONObject data = new JSONObject(addressJson.get("data"));
if (type.equals(IPTaoBaoUtil.COUNTRY_V)) {
this.country = data.get("country").toString();
} else if (type.equals(IPTaoBaoUtil.REGION_V)) {
this.region = data.get("region").toString();
} else if (type.equals(IPTaoBaoUtil.CITY_V)) {
this.city = data.get("city").toString();
}
return this;
}
public IPTaoBaoUtil getCountry() throws Exception {
this.get(IPTaoBaoUtil.COUNTRY_V);
return this;
}
public IPTaoBaoUtil getRegion() throws Exception {
this.get(IPTaoBaoUtil.REGION_V);
return this;
}
public IPTaoBaoUtil getCity() throws Exception {
this.get(IPTaoBaoUtil.CITY_V);
return this;
}
public IPTaoBaoUtil getAll() throws Exception {
this.getCountry();
this.getRegion();
this.getCity();
return this;
}
public String build() {
return this.country + this.region + this.city;
}
}
需要的jar包:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.2.1</version>
</dependency>
代码使用
IPTaoBaoUtil.start("183.250.223.138").getAll().build();
IPTaoBaoUtil.start("183.250.223.138").getRegion().getCity().build();
好了,接下来开始讲讲,怎么回事了…
使用链式编程呢,其实就是先创建一个对象,使用方法时候,使用 return this返回自身即可,就可以实现,就可以不停的"."出方法…
这个过程其实使用的就是同一个对象…看下面代码…
IPTaoBaoUtil start = IPTaoBaoUtil.start("183.250.223.138");
System.out.println(start);
IPTaoBaoUtil city1 = start.getCity();
System.out.println(city1);
IPTaoBaoUtil region = city1.getRegion();
System.out.println(region);
String build1 = region.build();
System.out.println(build1);
执行…内存地址相同…
其实本质就是这样的,通过start静态方法,生成一个对象,然后用这个对象,使用这个类的方法,使用后,return this; 返回自身,还是这个对象
然后呢…就好了…具体查看代码进行理解…