android 点赞功能封装

以前做的点赞功能,一般都是在当前activity里开辟一个新线程执行网络交互点赞成功,失败功能。但是问题来了,点赞的东西在很多歌activity里都会有相同的线程操作,比如文章点赞,视频点赞,主界面点赞。 所以功能点可以进行封装。防止代码的冗余,并且修改一处就可以修改多处。java 面向对象封装,好处多多。


封装性的好处

  1. Modularity(module)模块化: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.

     

  2. Information-hiding 信息的隐藏: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.

     

  3. Code re-use 由于模块化,所以可以拿来使用: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.

     

  4. Pluggability and debugging ease 坏了容易替换,不影响全局: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous(analog) to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

实例:

package cdv.cq.mobilestation.Activity.praise;

import android.content.Context;
import cdv.cq.mobilestation.tools.JsonBean;
import cdv.cq.mobilestation.tools.ResultObject;

/**
 * 点赞工具类
 * */
public class PraiseProvider {
	private Context  mcontext;
	private String  clickId;
	private String clickType;
	private ResultObject resJson = new ResultObject();
	private JsonBean jsben = new JsonBean();
	private int count;
	private Dao dao;

	public PraiseProvider(Context context,String  clickId,String clickType) {
		this.mcontext = context;
		this.clickId = clickId;
		this.clickType = clickType;
		
	    dao = Dao.getInstance(mcontext);
		Dao.createTable();
	}

	/**
	 * 点赞
	 * return 点赞成功后的数目
	 * */
	public int goPraise(){
		Thread thread = new Thread(PraiseRunnable);
		thread.start();
		try {
			//等待子线程完毕执行后续方法
			thread.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * 查询点赞数
	 * return 点赞数
	 * */
	public int queryCount(){
		Thread thread =  new Thread(QueryPraiseCountRunnable);
		thread.start();
		try {
			thread.join();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return count;
	}
	
	/**
	 * 查询是否点过赞
	 * */
	public boolean Praised(){
		int id = dao.isHasInfors(clickId,clickType);
		int operation = dao.queryUserPraise(String.valueOf(id));
		if(operation == 1){ //点过赞
			return true;
		}
		else{
			return false;
		}
	}
	
	//读取数据库点赞数
	Runnable QueryPraiseCountRunnable = new Runnable() {

		@Override
		public void run() {
			int id = dao.isHasInfors(clickId,clickType);
			if(id == -1){ //表中没有点赞 读取网络点赞数
				int operation = 0; //数目查询
				resJson = (ResultObject) jsben.getPraiseCount
						(mcontext, clickType, clickId, operation);
				if(resJson.result){
					count = Integer.parseInt((String)resJson.obj);
				}else{
					count = -1;
				}
			}else{
				//读取数据库中的缓存值
				count = dao.queryPraiseCount(String.valueOf(id)); 
			}
		}
	};

	//点赞
	Runnable PraiseRunnable = new Runnable() {

		@Override
		public void run() {
			int id = dao.isHasInfors(clickId, clickType);
			if(id == -1){ //新增数目
				int operation = 1; //数目添加1
				resJson = (ResultObject) jsben.getPraiseCount
						(mcontext, clickType, clickId, operation);
				if(resJson.result){
					count = Integer.parseInt((String)resJson.obj);
					dao.insert(clickId, clickType,count); //强转
				}else{
					count = -1;
				}
			}
		}
	};
}

activity组建界面调用:

PraiseProvider praiseProvider = new PraiseProvider(HomeTypeNewsActivity.this, cannlId, clickType);
				count = praiseProvider.queryCount();
				if(praiseProvider.Praised()){
					//点过赞
					mHandler.sendEmptyMessage(Constant.CONSTANT_FIVE);
				}else{
					mHandler.sendEmptyMessage(Constant.CONSTANT_FOUR);
				}


你可能感兴趣的:(android,封装,点赞)