• Android的设计模式-装饰者模式


    1.定义

    动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更为灵活。

    2.介绍

    • 装饰者模式属于结构型模式。
    • 装饰者模式在生活中应用实际上也非常广泛,一如一间房,放上厨具,它就是厨房;放上床,就是卧室。
    • 通常我们扩展类的功能是通过继承的方式来实现,但是装饰者模式是通过组合的方式来实现,这是继承的替代方案之一。

    3.UML类图

    角色说明:

    • Component(抽象组件):接口或者抽象类,被装饰的最原始的对象。具体组件与抽象装饰角色的父类。
    • ConcreteComponent(具体组件):实现抽象组件的接口。
    • Decorator(抽象装饰角色):一般是抽象类,抽象组件的子类,同时持有一个被装饰者的引用,用来调用被装饰者的方法;同时可以给被装饰者增加新的职责。
    • ConcreteDecorator(具体装饰类):抽象装饰角色的具体实现。

    4.实现

    就以装修房间为例子

    4.1 创建抽象组件

    这里是一个抽象房子类,定义一个装修的方法:

    1. public abstract class Room {
    2. public abstract void fitment();//装修方法
    3. }

    4.2 创建具体组件

    现在有一间新房子,已经装上了电:

    1. public class NewRoom extends Room {//继承Room
    2. @Override
    3. public void fitment() {
    4. System.out.println("这是一间新房:装上电");
    5. }
    6. }

    4.3 创建抽象装饰角色

    要为房子装修,定义抽象的房间装饰类:

    1. public abstract class RoomDecorator extends Room {//继承Room,拥有父类相同的方法
    2. private Room mRoom;//持有被装饰者的引用,这里是需要装修的房间
    3. public RoomDecorator(Room room) {
    4. this.mRoom = room;
    5. }
    6. @Override
    7. public void fitment() {
    8. mRoom.fitment();//调用被装饰者的方法
    9. }
    10. }

    4.4 创建具体装饰类

    我们要将房间装修成卧室和厨房,其具体实现是不同的:

    1. public class Bedroom extends RoomDecorator {//卧室类,继承自RoomDecorator
    2. public Bedroom(Room room) {
    3. super(room);
    4. }
    5. @Override
    6. public void fitment() {
    7. super.fitment();
    8. addBedding();
    9. }
    10. private void addBedding() {
    11. System.out.println("装修成卧室:添加卧具");
    12. }
    13. }
    14. public class Kitchen extends RoomDecorator {//厨房类,继承自RoomDecorator
    15. public Kitchen(Room room) {
    16. super(room);
    17. }
    18. @Override
    19. public void fitment() {
    20. super.fitment();
    21. addKitchenware();
    22. }
    23. private void addKitchenware() {
    24. System.out.println("装修成厨房:添加厨具");
    25. }
    26. }

    4.5 客户端测试:

    1. public void test() {
    2. Room newRoom = new NewRoom();//有一间新房间
    3. RoomDecorator bedroom = new Bedroom(newRoom);
    4. bedroom.fitment();//装修成卧室
    5. RoomDecorator kitchen = new Kitchen(newRoom);
    6. kitchen.fitment();//装修成厨房
    7. }

    输出结果:

    1. 这是一间新房:装上电
    2. 装修成卧室:添加卧具
    3. 这是一间新房:装上电
    4. 装修成厨房:添加厨具

    5. 应用场景

    • 需要扩展一个类的功能,或给一个类增加附加功能时
    • 需要动态的给一个对象增加功能,这些功能可以再动态的撤销
    • 当不能采用继承的方式对系统进行扩充或者采用继承不利于系统扩展和维护时。

    6. 优点

    • 采用组合的方式,可以动态的扩展功能,同时也可以在运行时选择不同的装饰器,来实现不同的功能。
    • 有效避免了使用继承的方式扩展对象功能而带来的灵活性差,子类无限制扩张的问题。
    • 被装饰者与装饰者解偶,被装饰者可以不知道装饰者的存在,同时新增功能时原有代码也无需改变,符合开放封闭原则。

    7. 缺点

    • 装饰层过多的话,维护起来比较困难。
    • 如果要修改抽象组件这个基类的话,后面的一些子类可能也需跟着修改,较容易出错。

    8. Android中的源码分析

    我们都知道ActivityServiceApplication等都是一个Context,这里面实际上就是通过装饰者模式来实现的。下面以startActivity()这个方法来简单分析一下。

    8.1 Context类

    Context实际上是个抽象类,里面定义了大量的抽象方法,其中就包含了startActivity()方法:

    1. public abstract class Context {//抽象类
    2. public abstract void startActivity(@RequiresPermission Intent intent);//抽象方法
    3. //其他代码略
    4. }

    8.2 ContextImpl类

    Context类的具体实现实际上是在ContextImpl类,里面具体实现了startActivity()方法:

    1. class ContextImpl extends Context {
    2. @Override
    3. public void startActivity(Intent intent) {
    4. warnIfCallingFromSystemProcess();
    5. startActivity(intent, null);
    6. }
    7. @Override
    8. public void startActivity(Intent intent, Bundle options) {//具体实现原理这里就不分析了
    9. warnIfCallingFromSystemProcess();
    10. // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
    11. // generally not allowed, except if the caller specifies the task id the activity should
    12. // be launched in.
    13. if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
    14. && options != null && ActivityOptions.fromBundle(options).getLaunchTaskId() == -1) {
    15. throw new AndroidRuntimeException(
    16. "Calling startActivity() from outside of an Activity "
    17. + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
    18. + " Is this really what you want?");
    19. }
    20. mMainThread.getInstrumentation().execStartActivity(
    21. getOuterContext(), mMainThread.getApplicationThread(), null,
    22. (Activity) null, intent, -1, options);
    23. }
    24. //其他代码略
    25. }

    8.3 ContextWrapper类

    通常我们在ActivityService里面调用startActivity()方法,实际上是调用他们的父类ContextWrapper里面的startActivity()方法,我们先来看下ActivityService的继承关系:

     Activity继承关系.png

    Service继承关系.png

    可以看到ActivityService都是继承自ContextWrapper,再来看看ContextWrapper的代码:

    1. public class ContextWrapper extends Context {//Context包装类
    2. Context mBase;//持有Context引用
    3. public ContextWrapper(Context base) {//这里的base实际上就是ContextImpl
    4. mBase = base;
    5. }
    6. @Override
    7. public void startActivity(Intent intent) {
    8. mBase.startActivity(intent);//调用ContextImpl的startActivity()方法
    9. }
    10. //其他代码略
    11. }

    8.4 总结

    Context类在这里就充当了抽象组件的角色,ContextImpl类则是具体的组件,而ContextWrapper就是具体的装饰角色,通过扩展ContextWrapper增加不同的功能,就形成了ActivityService等子类。最后,放一张总的UML类图帮助理解:

  • 相关阅读:
    zabbix中文乱码解决方法
    想要精通算法和SQL的成长之路 - 至少有 K 个重复字符的最长子串
    【系统分析师之路】第六章 复盘需求工程(综合知识概念)
    每个.NET开发都应掌握的C#特性(Attribute)知识点
    Java基础面试题
    IDEA中DEBUG技巧
    browserslist 选项的目的是什么?
    JUC并发编程——各种锁的理解(基于狂神说的学习笔记)
    eBPF 入门实践教程(一):编写 eBPF 程序监控打开文件路径并使用 Prometheus 可视化
    jbase实现通用码表
  • 原文地址:https://blog.csdn.net/xiaopangcame/article/details/125554897