关闭

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

标签: 代理模式设计思想
486人阅读 评论(2) 收藏 举报
分类:

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

代理模式定义:

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

图解如下:


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

 
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);
		}
	};

}

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

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:156759次
    • 积分:2774
    • 等级:
    • 排名:第14685名
    • 原创:105篇
    • 转载:37篇
    • 译文:2篇
    • 评论:69条
    最新评论