Android(安卓)外观模式
16lz
2021-01-24
Android 23种设计模式
一、前言
Android 外观模式(Facade),这个模式的使用率就非常高了。我们经常引用一些第三方的功能,或者GitHub上别人封装好的功能。很多都使用的时外观模式。封装过后,我们在使用这些库的时候,只需要调用极少数的方法就可以达到目的。 我相信大家已经无数次的使用外观模式了。接下来让我们“重新”认识一下外观模式。
二、定义
提供一个接口,使得客户端只通过接口访问。隐藏内部子系统的实现。
三、例子
认识设计模式最好的方法就是理解它的demo。下面我们通过一个生产nokia手机的例子来说明。我们分为三个步骤,设计,开发和生产来模拟整个过程。
3.1、最高层接口
抽象出API方法,并实现
public abstract class NokiaPhone { public abstract void design(); public abstract void development(); public abstract void production();}public class NokiaPhoneImpl extends NokiaPhone{ private Software software = new SoftwareImpl(); private Hardware hardware = new HardwareImpl(); private Production production = new ProductionImpl(); @Override public void design() { hardware.design(); software.design(); } @Override public void development() { hardware.development(); software.development(); } @Override public void production() { hardware.test(); software.test(); production.production(); }}
最高层接口就是为了统一API,客户端调用只需要调用设计,开发和生产三个方法举行了,具体内部实现无须关注。具体的实现由内部的子系统去完成。
3.2、子系统
硬件部
public interface Hardware { public void design(); public void development(); public void test();}public class HardwareImpl implements Hardware { private static final String TAG = HardwareImpl.class.getSimpleName(); @Override public void design() { Log.d(TAG,"design"); } @Override public void development() { Log.d(TAG,"development"); } @Override public void test() { Log.d(TAG,"test pass"); }}
软件部
public interface Software { public void design(); public void development(); public void test();}public class SoftwareImpl implements Software{ private static final String TAG = SoftwareImpl.class.getSimpleName(); @Override public void design() { Log.d(TAG,"design"); } @Override public void development() { Log.d(TAG,"development"); } @Override public void test() { Log.d(TAG,"test pass"); }}
生产部
public interface Production { public void production();}public class ProductionImpl implements Production { private static final String TAG = ProductionImpl.class.getSimpleName(); @Override public void production() { Log.d(TAG,"production nokia"); }}
三个部门的职能写的比较简化,子系统还可以包含更多的子系统,比如软件部还有测试组、系统组等等。这里为了篇幅就不举例了。
3.3、调用
NokiaPhoneImpl nokiaPhoneImpl = new NokiaPhoneImpl();nokiaPhoneImpl.design();nokiaPhoneImpl.development();nokiaPhoneImpl.production();
客户端的调用就很简单了,和抽象类抽象出的方法一样。统一的API只有三个。具体的实现由子系统去完成。输出如下:
02-21 10:07:51.896 6606-6606/com.yink.designpattern.designpattern D/HardwareImpl: design02-21 10:07:51.896 6606-6606/com.yink.designpattern.designpattern D/SoftwareImpl: design02-21 10:07:51.896 6606-6606/com.yink.designpattern.designpattern D/HardwareImpl: development02-21 10:07:51.896 6606-6606/com.yink.designpattern.designpattern D/SoftwareImpl: development02-21 10:07:51.896 6606-6606/com.yink.designpattern.designpattern D/HardwareImpl: test pass02-21 10:07:51.896 6606-6606/com.yink.designpattern.designpattern D/SoftwareImpl: test pass02-21 10:07:51.896 6606-6606/com.yink.designpattern.designpattern D/ProductionImpl: production nokia
四、Context
Android源码中,Context就是外观模式的一个例子。Context这个抽象类定义了很多我们熟知的方法。它具体的实现都是在ContextImpl.java这个类里边。篇幅原因,多余的方法就省略了。
public abstract class Context { public abstract void sendBroadcast(@RequiresPermission Intent intent); public abstract ComponentName startService(Intent service); ...class ContextImpl extends Context { @Override public void sendBroadcast(Intent intent, String receiverPermission) { warnIfCallingFromSystemProcess(); String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); String[] receiverPermissions = receiverPermission == null ? null : new String[] {receiverPermission}; try { intent.prepareToLeaveProcess(this); ActivityManager.getService().broadcastIntent( mMainThread.getApplicationThread(), intent, resolvedType, null, Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE, null, false, false, getUserId()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } @Override public ComponentName startService(Intent service) { warnIfCallingFromSystemProcess(); return startServiceCommon(service, false, mUser); }
而ContextImpl实现这些抽象方法时,又通过了ActivityManager、PackageManager一系列的其他子系统。
更多相关文章
- Android(安卓)Bluetooth 蓝牙开发资料大全【新】
- Android的SQLite使用实例
- Java集合框架——Android中的ArrayList源码分析
- Android之路之十七(重要组件之Service)
- Fedora17 64位 android "failed to create the SD card" 解决方
- Android的adapter总结和深入研究
- android获取控件宽和高
- Android事件和监听器详细的介绍
- Android中自定义数据适配器Adapter