a day a better

每天进步一点点

运用代理模式,实现相同的操作方法,可以增加自己的逻辑

代理模式:类似中介的效果

代理模式定义:

 为另一个对象提供一个替身或占位符以控制对这个对象的访问。

图解如下:


代理最主要的是  :可以增加自己的操作在方法中。
 

 
public class DialogHelper {
	private volatile static DialogHelper dialogHelper = null;
	private static PromDialog currentDialog = null;
	private static Context curContext = null;
	
	private DialogHelper() {}
	
	/**
	 * 获取DialogHelper实例
	 * @return DialogHelper
	 */
	public static DialogHelper getInstance(){
		if(dialogHelper == null){
			synchronized (DialogHelper.class) {
				if(dialogHelper == null){
					dialogHelper = new DialogHelper();
				}
			}
		}
		return dialogHelper;
	}
	public static void setContext(Context context){
		curContext = context;
		initBroadcastReceiver();
	}
	public static void setKeyClickListener(KeyClickListener listener){
		mListenerUser = listener;
	}
	public static void setThreeKeyListener(ThreeKeyListener listener){
		mThreeListenerUser = listener;
	}
	private static void initBroadcastReceiver(){

	}
	/**
	 * 
	 * @param title 标题
	 * @param content 内容
	 * @param firStr  第一个字符
	 * @param middStr  第二个字符 (可为空)
	 * @param canStr 第三个字符 (可为空)
	 * @param type  类型
	 */
	private static void showDialog(String title,String content,String firStr,String middStr,String canStr,int type){
		if(currentDialog != null){
			hideDialog(false);
		}
		currentDialog = new PromDialog(curContext,type);
		if(type == 0 || type == 1 || type == 2 || type ==4){
			currentDialog.setData(title, content, firStr, canStr);
		}
		else if(type == 3){
			currentDialog.setData(title, content, firStr, middStr, canStr);
		}
	}
	//一个Button
	public static void showDialog(String title,String content,String firStr){
		showDialog(title,content,null,null,firStr,4);
		if(currentDialog != null){
			currentDialog.setListener(mListenerProxy);
			currentDialog.show();
		}
	}
	//两个Button
	public static void showDialog(String title,String content,String firStr,String canStr,DialogEnum type){
		switch (type) {
		case DIALOG_NORMOL:
			showDialog(title,content,firStr,null,canStr,0);
			break;
		case DIALOG_NO_CONTENT:
			showDialog(title,content,firStr,null,canStr,1);
			break;
		case DIALOG_HAS_CONTENT:
			showDialog(title,content,firStr,null,canStr,2);
			break;

		default:
			break;
		}
		if(currentDialog != null){
			currentDialog.setListener(mListenerProxy);
			currentDialog.show();
		}
	}
	//三个Button
	public static void showDialog(String title,String content,String firStr,String middStr,String canStr){
		showDialog(title,content,firStr,middStr,canStr,3);
		if(currentDialog != null){
			currentDialog.setThreeListener(mThreeListenerProxy);
			currentDialog.show();
		}
	}

	/**
	 * 隐藏Dialog
	 */
	public static void hideDialog(boolean isBroadcastReceiver){
		if(isBroadcastReceiver){
			if(mListenerUser != null){
				mListenerUser.backDismiss();
			}
			if(mThreeListenerUser != null){
				mThreeListenerUser.cancle();
			}
		}		
		if(currentDialog != null){
			currentDialog.dismiss();
			currentDialog = null;
		}
		
	}
	
	public static boolean isShowing(){
		if(currentDialog != null){
			return currentDialog.isShowing();
		}
		else{
			return false;
		}
		
	}
	
	private static KeyClickListener mListenerUser;
	private static ThreeKeyListener mThreeListenerUser;
	private static KeyClickListener mListenerProxy = new KeyClickListener() {
		
		@Override
		public void operation() {
			if (null != mListenerUser) {
				mListenerUser.operation();
			}
			hideDialog(false);
		}
		
		@Override
		public void cancle() {
			if (null != mListenerUser) {
				mListenerUser.cancle();
			}
			hideDialog(false);
		}
		
		@Override
		public void backDismiss() {
			if (null != mListenerUser) {
				mListenerUser.backDismiss();
			}
			hideDialog(false);
		}
	};
	private static ThreeKeyListener mThreeListenerProxy = new ThreeKeyListener() {
		
		@Override
		public void operation() {
			if(null != mThreeListenerUser){
				mThreeListenerUser.operation();
			}
			hideDialog(false);
		}
		
		@Override
		public void middle() {
			if(null != mThreeListenerUser){
				mThreeListenerUser.middle();
			}
			hideDialog(false);
		}
		
		@Override
		public void cancle() {
			if(null != mThreeListenerUser){
				mThreeListenerUser.cancle();
			}
			hideDialog(false);
		}
	};

}

  
希望认真理解代理模式,让其发挥巨大的效能。

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。原文链接http://blog.csdn.net/adayabetter?viewmode=contents https://blog.csdn.net/adayabetter/article/details/46808923
个人分类: 设计模式
上一篇Android手机中的2048游戏Demo开发
下一篇Android中图案解锁的设计原理和实现过程
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭