1.定义
动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更为灵活。
2.介绍
3.UML类图
角色说明:
4.实现
就以装修房间为例子
4.1 创建抽象组件
这里是一个抽象房子类,定义一个装修的方法:
- public abstract class Room {
- public abstract void fitment();//装修方法
- }
4.2 创建具体组件
现在有一间新房子,已经装上了电:
- public class NewRoom extends Room {//继承Room
- @Override
- public void fitment() {
- System.out.println("这是一间新房:装上电");
- }
- }
4.3 创建抽象装饰角色
要为房子装修,定义抽象的房间装饰类:
- public abstract class RoomDecorator extends Room {//继承Room,拥有父类相同的方法
- private Room mRoom;//持有被装饰者的引用,这里是需要装修的房间
-
- public RoomDecorator(Room room) {
- this.mRoom = room;
- }
-
- @Override
- public void fitment() {
- mRoom.fitment();//调用被装饰者的方法
- }
- }
4.4 创建具体装饰类
我们要将房间装修成卧室和厨房,其具体实现是不同的:
- public class Bedroom extends RoomDecorator {//卧室类,继承自RoomDecorator
-
- public Bedroom(Room room) {
- super(room);
- }
-
- @Override
- public void fitment() {
- super.fitment();
- addBedding();
- }
-
- private void addBedding() {
- System.out.println("装修成卧室:添加卧具");
- }
- }
-
- public class Kitchen extends RoomDecorator {//厨房类,继承自RoomDecorator
-
- public Kitchen(Room room) {
- super(room);
- }
-
- @Override
- public void fitment() {
- super.fitment();
- addKitchenware();
- }
-
- private void addKitchenware() {
- System.out.println("装修成厨房:添加厨具");
- }
- }
4.5 客户端测试:
- public void test() {
- Room newRoom = new NewRoom();//有一间新房间
- RoomDecorator bedroom = new Bedroom(newRoom);
- bedroom.fitment();//装修成卧室
- RoomDecorator kitchen = new Kitchen(newRoom);
- kitchen.fitment();//装修成厨房
- }
输出结果:
- 这是一间新房:装上电
- 装修成卧室:添加卧具
- 这是一间新房:装上电
- 装修成厨房:添加厨具
我们都知道Activity
、Service
、Application
等都是一个Context
,这里面实际上就是通过装饰者模式来实现的。下面以startActivity()
这个方法来简单分析一下。
8.1 Context类
Context
实际上是个抽象类,里面定义了大量的抽象方法,其中就包含了startActivity()
方法:
- public abstract class Context {//抽象类
- public abstract void startActivity(@RequiresPermission Intent intent);//抽象方法
- //其他代码略
- }
8.2 ContextImpl类
Context
类的具体实现实际上是在ContextImpl
类,里面具体实现了startActivity()
方法:
- class ContextImpl extends Context {
- @Override
- public void startActivity(Intent intent) {
- warnIfCallingFromSystemProcess();
- startActivity(intent, null);
- }
-
- @Override
- public void startActivity(Intent intent, Bundle options) {//具体实现原理这里就不分析了
- warnIfCallingFromSystemProcess();
-
- // Calling start activity from outside an activity without FLAG_ACTIVITY_NEW_TASK is
- // generally not allowed, except if the caller specifies the task id the activity should
- // be launched in.
- if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0
- && options != null && ActivityOptions.fromBundle(options).getLaunchTaskId() == -1) {
- throw new AndroidRuntimeException(
- "Calling startActivity() from outside of an Activity "
- + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
- + " Is this really what you want?");
- }
- mMainThread.getInstrumentation().execStartActivity(
- getOuterContext(), mMainThread.getApplicationThread(), null,
- (Activity) null, intent, -1, options);
- }
-
- //其他代码略
- }
8.3 ContextWrapper类
通常我们在Activity
、Service
里面调用startActivity()
方法,实际上是调用他们的父类ContextWrapper
里面的startActivity()
方法,我们先来看下Activity
、Service
的继承关系:
Activity继承关系.png
Service继承关系.png
可以看到Activity
、Service
都是继承自ContextWrapper
,再来看看ContextWrapper
的代码:
- public class ContextWrapper extends Context {//Context包装类
- Context mBase;//持有Context引用
-
- public ContextWrapper(Context base) {//这里的base实际上就是ContextImpl
- mBase = base;
- }
-
- @Override
- public void startActivity(Intent intent) {
- mBase.startActivity(intent);//调用ContextImpl的startActivity()方法
- }
-
- //其他代码略
- }
8.4 总结
Context
类在这里就充当了抽象组件的角色,ContextImpl
类则是具体的组件,而ContextWrapper
就是具体的装饰角色,通过扩展ContextWrapper
增加不同的功能,就形成了Activity
、Service
等子类。最后,放一张总的UML类图帮助理解: