Android中的类MVC设计方案 – Trigger机制

当然,“MVC”是针对Web项目提出的;也就是说,Android中没有MVC一说;但此处“MVC”能十分精辟地进行概括,毕竟Java开发人员对该概念十分之熟悉,所以在此借用“类MVC”;下面进入正题:

一、效果图展示

android 4j trigger     android 4j triggerandroid 4j trigger

层面是无法体现该方案的便宜之处,但从设计层面可以得到很好的体现,层次划分清晰可见,每层各负其责,耦合度相当之低。为了更能说明问题,4J就分别以ListActivity(即上图中的“4J NewsHeadline”)和Activity(即上图中的“4J Weather”)进行展示;但两者的实质完全相同,避免累赘,下文在阐述时仅以“4J NewsHeadline”为例。

二、设计思想

为了能更直观的阐述设计思想,先将类图和时序图贴出来:
android 4j trigger
android 4j trigger

1. OnTriggerListener

该Interface有且仅有1个方法onTrigger (),具体如下:

public boolean onTrigger(TriggerInfo triggerInfo);

2. FJUIControlManager

首先,该类中有3个成员属性:
mCurrentActivity:Activity,用于记录当前处于foreground状态的Activity;
mOnTriggerListener: OnTriggerListener,用于监听mCurrentActivity;
mFJUIControlManagerHandler: FJUIControlManagerHandler

其次,该类中有3个重要的成员方法:setCurrentActivity()、setOnTriggerListener()和postTrigger()。

该类的详细代码如下:

public class FJUIControlManager {

	// 当前处于foreground状态的Activity
	private static Activity mCurrentActivity;
	// 用于监听mCurrentActivity
	private static OnTriggerListener mOnTriggerListener;

	// 接收Control层post过来的Trigger,并通过重写handleMessage()做出相应处理
	private static final FJUIControlManagerHandler 
		mFJUIControlManagerHandler = new FJUIControlManagerHandler();

	public static void setCurrentActivity(Activity currentActivity) {
		mCurrentActivity = currentActivity;
	}

	public static void setOnTriggerListener(OnTriggerListener onTriggerListener) {
		mOnTriggerListener = onTriggerListener;
	}

	public static void removeOnTriggerListener() {
		mOnTriggerListener = null;
	}

	/**
	 * 供Control层调用,将Trigger post到主消息队列中去。
	 */
	public static void postTrigger(TriggerInfo triggerInfo) {
		Message msg = mFJUIControlManagerHandler.obtainMessage
                                                  (TriggerId.IDENTIFY, triggerInfo);
		mFJUIControlManagerHandler.sendMessage(msg);
	}

	private static class FJUIControlManagerHandler extends Handler {
		/**
		 * 注意:一定得是Looper.getMainLooper()
		 */
		public FJUIControlManagerHandler() {
			super(Looper.getMainLooper());
		}

		@Override
		public void handleMessage(Message msg) {
			// msg来自于postTrigger()
			if ((msg == null) || (msg.what != TriggerId.IDENTIFY) 
                             || (msg.obj == null) || (msg.obj.getClass() != TriggerInfo.class))
				return;

			TriggerInfo triggerInfo = (TriggerInfo) msg.obj;
			mOnTriggerListener.onTrigger(triggerInfo);
		}
	}

}

3. FJListActivity

该类主要负责重写onResume()、onPause()和实现onTrigger();在onResume()中主要负责给FJUIControlManager. mCurrentActivity赋值和注册监听,这样在子类中就不需每次重复,只需super.onResume()代替之;在onPause()中主要负责注销监听,同理在子类中只需super.onPause()代替之;而onTrigger()留给子类重写,进而分别实现各自的功能。该类的具体代码如下:

public class FJListActivity extends ListActivity implements OnTriggerListener {

	@Override
	protected void onResume() {
		super.onResume();

		FJUIControlManager.setCurrentActivity(this);
		FJUIControlManager.setOnTriggerListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();

		FJUIControlManager.removeOnTriggerListener();
	}

	public boolean onTrigger(TriggerInfo triggerInfo) {
		return true;
	}
}

4. FJTriggerMainActivity

该类继承FJListActivity,有1个重要的成员属性:mNewsHeadlineRequestId;2个重要的成员方法:onListItemClick()和onTrigger();具体代码(主要部分)如下:

public class FJTriggerMainActivity extends FJListActivity {

	// 发送NewsHeadline请求时返回的Trigger唯一标识符
	private int mNewsHeadlineRequestId; 

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {		
		switch (position) {
		case 0:			
			// 将数据请求发送到Control层,返回一个Trigger唯一标识符。
			mNewsHeadlineRequestId = NewsHeadlineControl.requestHeadlines();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onTrigger(TriggerInfo triggerInfo) {
		int triggerId = triggerInfo.getId();

		if (triggerId == mNewsHeadlineRequestId) {
			switch (triggerInfo.getArg1()) {
			case TriggerId.NEWS_HEADLINE_SUCCESS:
                                // 画面跳转
				startActivity(new Intent(this, FJNewsHeadlineActivity.class));
				break;
			case TriggerId.NEWS_HEADLINE_SERVER_EXCEPTION:
				Toast.makeText(this, 
                                             R.string.news_headline_request_server_exception, 1000).show();
				break;
			default:
				break;
			}
		} else {}

		return true;
	}

}

5. NewsHeadlineControl

该类代码比较单一,不需过多解释,就直接贴代码:

public class NewsHeadlineControl extends FJControl {

	private static List mHeadlinesList = new ArrayList();

	public static int requestHeadlines() {
		// 从服务器获取数据
		getHeadlinesFromServer();

		TriggerInfo triggerInfo = TriggerInfo.factory
                                                        (TriggerId.NEWS_HEADLINE_IDENTIFY,
						         TriggerId.NEWS_HEADLINE_SUCCESS);

		// 将Trigger发送到消息队列中去
		FJUIControlManager.postTrigger(triggerInfo);

		return triggerInfo.getId();
	}

	public static List getHeadlines() {
		return mHeadlinesList;
	}

6. FJNewsHeadlineActivity

该类主要用于展示内容,而这些数据从Control层取得:NewsHeadlineControl.getHeadlines()

三、源码下载

http://7xl53s.com1.z0.glb.clouddn.com/download/2015/08/src/4jtrigger.zip

Author: 四勾 4J

via 4jplus.com

Leave a Reply

Your email address will not be published. Required fields are marked *