1. Android进程模型
(1) All Activities and Services implemented by classes in your .apk file will run in one process and in the context of your application. That is to say: If your activity or service calls getApplication() during its "lifespan",the application object returned will be an instance of the Application subclass specified in the <application> element of your AndroidManifest.xml. The only exception is if you use the android:process attribute to explicitly place a component in another process.
(2) Application in one package share not only the same Application object, but all of the same globals statics : application object/instance, application context , IntentReceiver, ContentProvider
(3) No matter which process starts an activity in your application, your activity will run in the same process as your Application object (that is, *not* in the caller's process)
(4) The lifespan of your Application object encompasses the lifespans of all Activities and Services in your application
You can find more detail info in below discussion list :
2. Android分布式对象机制
Android IPC Binder采用分布式对象机制,分布式对象机制有几个特点:
(1) 针对接口编程。在23-GOF设计模式中,针对接口编程是设计的首要准则。针对接口编程的主要目的就是信息隐藏,隔离变化,把模块的使用者和实现者之间的耦合减到最小。利用binder,组件可以动态替换或者增加,具有更强的灵活性
(2) 分布式对象底层用RPC作为进程间通信机制,RPC本质是一个同步C/S编程模式。
(3) 通信协议:parcel, bundle。Parcel比bundle复杂,可传输串行化类,因此负载更高。Bundle实际上是一个map,只能放入标准原语,比如string,int,bool 等。
3. Android异步通信机制
在移动网络通信环境中通常具有高延迟和传输错误率高的特点,因此同步传输模式有时侯会造成手机不必要等待从而造成系统性能下降。Android提供了一套名为intent的异步通信机制。 需要说明的是,intent实现也是基于binder的,android缺乏一套强大的异步通信架构,虽然移植了dbus,但是目前只有blueZ基于它,并没有当前流行的linux平台(手机/MID)一样大量的使用dbus
4. Android线程模型&线程间通信
(1) Android avoids creating additional threads in a process, keeping an application single-threaded ,there are two exceptions : a) create your own thread to avoid waiting of time-costing network handling b) when will be called from a different thread (more specifically, from a pool of threads the system maintains in your process for dispatching incoming IPCs
(2) how to create a thread : a) use the standard library Thread class b) extend
HandlerThread : The lifespan of your Application object encompasses the
lifespans of all Activities and Services in your application c) Implement
runnables ,then handler.postRunnable
(3) communication b/w threads : through message queue , see looper and handler
5. 反射机制。
反射机制指的是程序能够在运行状态中,根据类名动态的创建同名的对象,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法。反射机制是移动计算领域一个非常重要的技术,特别是如果应是context-aware的。这里context和android application context含义类似,包括一个应用下的所有元素:content provider, activity, service, intent receiver etc。如果应用和middleware built in monolithic fashion,那么应用和middleware 在编码时就需要考虑可能的context state 组合,而且the memory footprint of the monolithic middleware increases with each context state combination handled. 为了减小footprint minimal 并且使系统更灵活, middleware 被划分成更小的模块组合(模块化设计),应用只需要通过部署XML配置文件实例化满足需求的模块。这样的设计也大量应用在J2EE架构中,如spring IOC(控制反转:Bean对象的产生和销毁,对象之间的相互调用都不再代码里面控制,而是把这些全部交给spring框架根据用户配置文件进行管理)。从这个意义上说,android应用也是context aware的。
6. 良好的扩展性
Android designed on modules which can be replacable. This is a way for third party to enhance the system function.
1) Create a new service/activity or new provider in APK.(similar as plug-in)
2) provide extend way like OEM interface in RIL
需要说明的是android 在java space不支持plugin,不能不说是一种遗憾,if plug-in supported, third party can add their private function without touch present Android framework or an application, it will be very useful for thirdparty, thirdparty can keep their plugin privatly.
7. 分离界面和实现。
以前各种GUI的不统一,导致手机程序移植非常困难,MVC模式便盛行于当今先进手机平台领域,比如TAT,mozilla。MVC类似web网页的开发方式:应用程序的界面(view)用HTML来写,应用程序的逻辑(model)用C/C++写,界面在浏览器(html解释器,渲染)中运行,而应用程序的逻辑作为WEB服务器运行。而用脚本语言(javascript)把两者胶合HTML起来(controller),这可以说是把三者的长处发挥到了极致。再扩展一下HTTP协议,支持异步事件,完全可以实现传统的应用程序。
Android UI 采用典型的MVC架构:用标记语言(XML)开发界面,用编程语言(java)来实现controller,model
8. 观察者模式
移动开发中有大量的案例,比如,网络操作通常都要花很长时间,手机端监听网络状态变化等,用到了观察者模式。此外观察者模式对分离界面和实现很有帮助。
9. Android factory模式