java枚举在android项目应用

来源:互联网 发布:电视围棋直播软件 编辑:程序博客网 时间:2024/06/10 18:43

今天修复一个公司很早以前的android应用功能,里面的代码逻辑已经完全错乱,然后发现返回的数据完全不对了。然后修复了整整两天。然后我重新整理了一遍,重构就算不上了。然后就用上了枚举。

什么是枚举?我以前也不懂,当时我看见公司的项目中使用了枚举当做项目一个控制,比如修改已经写好的app然后为一些手机厂商做定制版。可能要去掉广告,还有跳转到商店url都不同,特别是国内基本都没有google play。我们为了避免以后的修改,就会写个枚举来控制它。

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public enum Market {
  2.      
  3.     Default,Huawei(){ 
  4.         @Override 
  5.         public String getMarketUrl() {
  6.             return "http://play.huawei.com";//huawei market url
  7.         } 
  8.     },ZTE(){ 
  9.         @Override 
  10.         public boolean isShouldAd(){
  11.             return false
  12.         } 
  13.         @Override 
  14.         public String getMarketUrl() {
  15.             return "http://play.zte.com";//ZTE market url 
  16.         } 
  17.     },OneTouch(){ 
  18.         @Override 
  19.         public String getMarketUrl() {
  20.             return "http://play.huawei.com";
  21.         } 
  22.     }; 
  23.      
  24.      
  25.     public boolean isShouldAd(){
  26.         return true;
  27.     } 
  28.      
  29.     public String getMarketUrl(){
  30.         return "http:\\googleplay....";//google play url
  31.     } 
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public enum Market { 
  2.      
  3.     Default,Huawei(){ 
  4.         @Override 
  5.         public String getMarketUrl() { 
  6.             return "http://play.huawei.com";//huawei market url 
  7.         } 
  8.     },ZTE(){ 
  9.         @Override 
  10.         public boolean isShouldAd(){ 
  11.             return false
  12.         } 
  13.         @Override 
  14.         public String getMarketUrl() { 
  15.             return "http://play.zte.com";//ZTE market url 
  16.         } 
  17.     },OneTouch(){ 
  18.         @Override 
  19.         public String getMarketUrl() { 
  20.             return "http://play.huawei.com"
  21.         } 
  22.     }; 
  23.      
  24.      
  25.     public boolean isShouldAd(){ 
  26.         return true
  27.     } 
  28.      
  29.     public String getMarketUrl(){ 
  30.         return "http:\\googleplay....";//google play url 
  31.     } 

通过上面的例子就大概了解了一些java枚举在android的基本使用。为了了解java枚举的原理,我写了一个很常用的红绿灯例子。下面是用枚举的代码:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public enum TrafficLight {
  2.  
  3.     red(45) { 
  4.         @Override 
  5.         public TrafficLight nextLamp() {
  6.             return green; 
  7.         } 
  8.     }, 
  9.     green(30) { 
  10.         @Override 
  11.         public TrafficLight nextLamp() {
  12.             return yellow; 
  13.         } 
  14.     }, 
  15.     yellow(3) { 
  16.         @Override 
  17.         public TrafficLight nextLamp() {
  18.             return red; 
  19.         } 
  20.     }; 
  21.  
  22.     private int time;
  23.  
  24.     private TrafficLight(int time) {
  25.         this.time = time;
  26.     }; 
  27.  
  28.     public abstract TrafficLight nextLamp();
  29.  
  30.     public int getTime() {
  31.         return this.time;
  32.     } 
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public enum TrafficLight { 
  2.  
  3.     red(45) { 
  4.         @Override 
  5.         public TrafficLight nextLamp() { 
  6.             return green; 
  7.         } 
  8.     }, 
  9.     green(30) { 
  10.         @Override 
  11.         public TrafficLight nextLamp() { 
  12.             return yellow; 
  13.         } 
  14.     }, 
  15.     yellow(3) { 
  16.         @Override 
  17.         public TrafficLight nextLamp() { 
  18.             return red; 
  19.         } 
  20.     }; 
  21.  
  22.     private int time; 
  23.  
  24.     private TrafficLight(int time) { 
  25.         this.time = time; 
  26.     }; 
  27.  
  28.     public abstract TrafficLight nextLamp(); 
  29.  
  30.     public int getTime() { 
  31.         return this.time; 
  32.     } 
然后是普通class模拟enum的代码:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public abstractclass TrafficLight { 
  2.      
  3.  
  4.     public staticfinal TrafficLight red  =new TrafficLight(45){
  5.         @Override 
  6.         public TrafficLight nextLamp() {
  7.             return green;
  8.         } 
  9.     }; 
  10.     public staticfinal TrafficLight green  =new TrafficLight(30) {
  11.         @Override 
  12.         public TrafficLight nextLamp() {
  13.             return yellow;
  14.         } 
  15.     }; 
  16.      
  17.     public staticfinal TrafficLight yellow  =new TrafficLight(3) {
  18.         @Override 
  19.         public TrafficLight nextLamp() {
  20.             return red; 
  21.         } 
  22.     }; 
  23.  
  24.     private int time;
  25.  
  26.     private TrafficLight(int time) {
  27.         this.time = time;
  28.     }; 
  29.  
  30.     public abstract TrafficLight nextLamp();
  31.  
  32.     public int getTime() {
  33.         return this.time;
  34.     } 
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public abstractclass TrafficLight { 
  2.      
  3.  
  4.     public staticfinal TrafficLight red  = new TrafficLight(45){ 
  5.         @Override 
  6.         public TrafficLight nextLamp() { 
  7.             return green; 
  8.         } 
  9.     }; 
  10.     public staticfinal TrafficLight green  = new TrafficLight(30) { 
  11.         @Override 
  12.         public TrafficLight nextLamp() { 
  13.             return yellow; 
  14.         } 
  15.     }; 
  16.      
  17.     public staticfinal TrafficLight yellow  = new TrafficLight(3) { 
  18.         @Override 
  19.         public TrafficLight nextLamp() { 
  20.             return red; 
  21.         } 
  22.     }; 
  23.  
  24.     private int time; 
  25.  
  26.     private TrafficLight(int time) { 
  27.         this.time = time; 
  28.     }; 
  29.  
  30.     public abstract TrafficLight nextLamp(); 
  31.  
  32.     public int getTime() { 
  33.         return this.time; 
  34.     } 
  通过两个比较,就会发现,其实枚举就是普通的java类,只是私有了构造方法,然后提供了几个static final 的实例变量。当然enum还提供一些其他方法。比如:TrafficLight.green.name()还是非常好用的。

这些都是enum的一些基本应用。然后是我今天在项目如何应用用枚举的类型的。因为我们那个app有三个不同的请求数据的url。其实我们只有一个数据源,如果find不到,就会通过其他两个是读取其他网站html,然后解析,通过正则表达式匹配得到数据。每个数据源需要设置httpClient、httpGet、httpResponse等参数,然后使用了枚举。我这里贴出一点基本的。然后发现其实都是差不多的。

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. import org.apache.http.HttpResponse;
  2. import org.apache.http.client.ClientProtocolException;
  3. import org.apache.http.client.HttpClient;
  4. import org.apache.http.client.methods.HttpGet;
  5. import org.apache.http.impl.client.DefaultHttpClient;
  6.  
  7. public enum RequestedProvider {
  8.  
  9.     mySelf() { 
  10.         @Override 
  11.         public String getUrl(String keyWord) {
  12.             return "http://..." + keyWord +"..."
  13.         }... 
  14.     }, 
  15.     google() { 
  16.         @Override 
  17.         public String getUrl(String keyWord) {
  18.             return "http://google..." + keyWord +"..."
  19.         }... 
  20.     }, 
  21.     amazon() { 
  22.         @Override 
  23.         public String getUrl(String keyWord) {
  24.             return "http://amazon..." + keyWord +"..."
  25.         }... 
  26.     }; 
  27.  
  28.     public abstract String getUrl(String keyWord);
  29.  
  30.     public HttpClient pickHttpClient() {
  31.         return new DefaultHttpClient();
  32.     } 
  33.  
  34.     public HttpGet pickHttpGet(String url) {
  35.         return new HttpGet(url);
  36.     } 
  37.  
  38.     public HttpResponse pickHttpResponse(HttpClient client, HttpGet get) {
  39.         HttpResponse res = null;
  40.         try
  41.             res = client.execute(get); 
  42.         } catch (ClientProtocolException e) {
  43.             // TODO Auto-generated catch block
  44.             e.printStackTrace(); 
  45.         } catch (IOException e) {
  46.             // TODO Auto-generated catch block
  47.             e.printStackTrace(); 
  48.         } 
  49.         return res; 
  50.     }... 
  51.  
0 0