今天修复一个公司很早以前的android应用功能,里面的代码逻辑已经完全错乱,然后发现返回的数据完全不对了。然后修复了整整两天。然后我重新整理了一遍,重构就算不上了。然后就用上了枚举。
什么是枚举?我以前也不懂,当时我看见公司的项目中使用了枚举当做项目一个控制,比如修改已经写好的app然后为一些手机厂商做定制版。可能要去掉广告,还有跳转到商店url都不同,特别是国内基本都没有google play。我们为了避免以后的修改,就会写个枚举来控制它。
- public enum Market {
- Default,Huawei(){
- @Override
- public String getMarketUrl() {
- return "http://play.huawei.com";//huawei market url
- }
- },ZTE(){
- @Override
- public boolean isShouldAd(){
- return false;
- }
- @Override
- public String getMarketUrl() {
- return "http://play.zte.com";//ZTE market url
- }
- },OneTouch(){
- @Override
- public String getMarketUrl() {
- return "http://play.huawei.com";
- }
- };
- public boolean isShouldAd(){
- return true;
- }
- public String getMarketUrl(){
- return "http:\\googleplay....";//google play url
- }
- }
public enum Market {
Default,Huawei(){
@Override
public String getMarketUrl() {
return "http://play.huawei.com";//huawei market url
}
},ZTE(){
@Override
public boolean isShouldAd(){
return false;
}
@Override
public String getMarketUrl() {
return "http://play.zte.com";//ZTE market url
}
},OneTouch(){
@Override
public String getMarketUrl() {
return "http://play.huawei.com";
}
};
public boolean isShouldAd(){
return true;
}
public String getMarketUrl(){
return "http:\\googleplay....";//google play url
}
}
通过上面的例子就大概了解了一些java枚举在android的基本使用。为了了解java枚举的原理,我写了一个很常用的红绿灯例子。下面是用枚举的代码:
- public enum TrafficLight {
- red(45) {
- @Override
- public TrafficLight nextLamp() {
- return green;
- }
- },
- green(30) {
- @Override
- public TrafficLight nextLamp() {
- return yellow;
- }
- },
- yellow(3) {
- @Override
- public TrafficLight nextLamp() {
- return red;
- }
- };
- private int time;
- private TrafficLight(int time) {
- this.time = time;
- };
- public abstract TrafficLight nextLamp();
- public int getTime() {
- return this.time;
- }
- }
public enum TrafficLight {
red(45) {
@Override
public TrafficLight nextLamp() {
return green;
}
},
green(30) {
@Override
public TrafficLight nextLamp() {
return yellow;
}
},
yellow(3) {
@Override
public TrafficLight nextLamp() {
return red;
}
};
private int time;
private TrafficLight(int time) {
this.time = time;
};
public abstract TrafficLight nextLamp();
public int getTime() {
return this.time;
}
}
然后是普通class模拟enum的代码:
- public abstract class TrafficLight {
- public static final TrafficLight red = new TrafficLight(45){
- @Override
- public TrafficLight nextLamp() {
- return green;
- }
- };
- public static final TrafficLight green = new TrafficLight(30) {
- @Override
- public TrafficLight nextLamp() {
- return yellow;
- }
- };
- public static final TrafficLight yellow = new TrafficLight(3) {
- @Override
- public TrafficLight nextLamp() {
- return red;
- }
- };
- private int time;
- private TrafficLight(int time) {
- this.time = time;
- };
- public abstract TrafficLight nextLamp();
- public int getTime() {
- return this.time;
- }
- }
public abstract class TrafficLight {
public static final TrafficLight red = new TrafficLight(45){
@Override
public TrafficLight nextLamp() {
return green;
}
};
public static final TrafficLight green = new TrafficLight(30) {
@Override
public TrafficLight nextLamp() {
return yellow;
}
};
public static final TrafficLight yellow = new TrafficLight(3) {
@Override
public TrafficLight nextLamp() {
return red;
}
};
private int time;
private TrafficLight(int time) {
this.time = time;
};
public abstract TrafficLight nextLamp();
public int getTime() {
return this.time;
}
}
通过两个比较,就会发现,其实枚举就是普通的java类,只是私有了构造方法,然后提供了几个static final 的实例变量。当然enum还提供一些其他方法。
比如:TrafficLight.green.name()还是非常好用的。
这些都是enum的一些基本应用。然后是我今天在项目如何应用用枚举的类型的。因为我们那个app有三个不同的请求数据的url。其实我们只有一个数据源,如果find不到,就会通过其他两个是读取其他网站html,然后解析,通过正则表达式匹配得到数据。每个数据源需要设置httpClient、httpGet、httpResponse等参数,然后使用了枚举。我这里贴出一点基本的。然后发现其实都是差不多的。
- import org.apache.http.HttpResponse;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- public enum RequestedProvider {
- mySelf() {
- @Override
- public String getUrl(String keyWord) {
- return "http://..." + keyWord + "...";
- }...
- },
- google() {
- @Override
- public String getUrl(String keyWord) {
- return "http://google..." + keyWord + "...";
- }...
- },
- amazon() {
- @Override
- public String getUrl(String keyWord) {
- return "http://amazon..." + keyWord + "...";
- }...
- };
- public abstract String getUrl(String keyWord);
- public HttpClient pickHttpClient() {
- return new DefaultHttpClient();
- }
- public HttpGet pickHttpGet(String url) {
- return new HttpGet(url);
- }
- public HttpResponse pickHttpResponse(HttpClient client, HttpGet get) {
- HttpResponse res = null;
- try {
- res = client.execute(get);
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return res;
- }...
- }