PolicyManager工厂模式与动态加载
Android FrameWork——PolicyManager
在学习Android跟踪setContentView执行过程代码的时遇到:
Attach(){
//着实不清楚PolicyManager到底如何创建的,哪一个对象。
mWindow = PolicyManager.makeNewWindow(this);
}
其中用到Class.forName动态加载,Java是最近才学的还真不晓得有这样的用法;
一 类之间关系
于是就了解一下PolicyManager以及相关类实现方式;
首先看一下各个类之间的UML图:
PolicyManager:提供了静态类方法接口,用于创建Window,LayoutInflate,
WindowManagerPolicy类实例;属于工厂方法;
IPolicy:提供抽象Policy创建产品接口;
Policy:具体IPolicy,实现创建产品接口;
二 代码分析
1 IPolicy
/* The implementation of this interface must be called Policy and contained * within the com.android.internal.policy.impl package */
public interface IPolicy { public Window makeNewWindow(Context context); public LayoutInflater makeNewLayoutInflater(Context context); public WindowManagerPolicy makeNewWindowManager(); }
2 Policy
public class Policy implements IPolicy { private static final String TAG = "PhonePolicy";
//提供需要预先加载的 类
private static final String[] preload_classes = { "com.android.internal.policy.impl.PhoneLayoutInflater", "com.android.internal.policy.impl.PhoneWindow", "com.android.internal.policy.impl.PhoneWindow$1", "com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback", "com.android.internal.policy.impl.PhoneWindow$DecorView", "com.android.internal.policy.impl.PhoneWindow$PanelFeatureState", "com.android.internal.policy.impl.PhoneWindow$PanelFeatureState$SavedState", };
//静态模块
static { // For performance reasons, preload some policy specific classes when // the policy gets loaded.
for (String s : preload_classes) { try { //动态加载指定的类 Class.forName(s); } catch (ClassNotFoundException ex) { Log.e(TAG, "Could not preload class for phone policy: " + s); } } }
//创建具体对象的接口
public PhoneWindow makeNewWindow(Context context) { return new PhoneWindow(context); } public PhoneLayoutInflater makeNewLayoutInflater(Context context) { return new PhoneLayoutInflater(context); } public PhoneWindowManager makeNewWindowManager() { return new PhoneWindowManager(); } }
3 PolicyManager
//访问类的静态成员的时候 会加载该类
public final class PolicyManager { private static final String POLICY_IMPL_CLASS_NAME =
"com.android.internal.policy.impl.Policy";
private static final IPolicy sPolicy; //加载该类时,所有静态成员均被会加载
static { try { //动态加载创建类实例 Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME); //创建了sPolicy指向的实例 sPolicy = (IPolicy)policyClass.newInstance(); } catch (ClassNotFoundException ex) {} } //构造函数私有类型,不能创建该类实例
private PolicyManager() {} // The static methods to spawn new policy-specific objects
public static Window makeNewWindow(Context context) { return sPolicy.makeNewWindow(context); } public static LayoutInflater makeNewLayoutInflater(Context context) { return sPolicy.makeNewLayoutInflater(context); } public static WindowManagerPolicy makeNewWindowManager() { return sPolicy.makeNewWindowManager(); } }
这里要注意的就是:动态加载Class.forName:
Class.forName()返回一个类;
String str = 用户输入的字符串
Class classType = Class.forName(str); //返回一个类
classType.newInstance(); //创建一个classType 类型的对象
在上述代码中:
Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME); //加载Policy类
sPolicy = (IPolicy)policyClass.newInstance(); //创建Policy对象 给IPolicy类型sPolicy引用;
这样动态加载加上使用工厂方法:降低耦合,提高代码的灵活性和扩展性;