java枚举在android项目应用

今天修复一个公司很早以前的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.     } 
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枚举的原理,我写了一个很常用的红绿灯例子。下面是用枚举的代码:

[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.     } 
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的代码:

[java] view plaincopyprint? 在CODE上查看代码片 派生到我的代码片
  1. public abstract class TrafficLight { 
  2.      
  3.  
  4.     public static final TrafficLight red  = new TrafficLight(45){ 
  5.         @Override 
  6.         public TrafficLight nextLamp() { 
  7.             return green; 
  8.         } 
  9.     }; 
  10.     public static final TrafficLight green  = new TrafficLight(30) { 
  11.         @Override 
  12.         public TrafficLight nextLamp() { 
  13.             return yellow; 
  14.         } 
  15.     }; 
  16.      
  17.     public static final 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.     } 
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等参数,然后使用了枚举。我这里贴出一点基本的。然后发现其实都是差不多的。

[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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值