Java描述设计模式(09):装饰模式
本文源碼:GitHub·點這里 || GitEE·點這里
一、生活場景
1、場景描述
孫悟空有七十二般變化,他的每一種變化都給他帶來一種附加的本領。他變成魚兒時,就可以到水里游泳;他變成鳥兒時,就可以在天上飛行。
2、場景圖解
3、代碼實現
public class C01_InScene {public static void main(String[] args) {TheGreatestSage greatestSage = new Monkey();TheGreatestSage fish = new Fish(greatestSage);fish.shapeDesc();// 這里雖然是魚形態,但是本體是悟空,所以可以直接變化TheGreatestSage bird = new Bird(fish);bird.shapeDesc();} } // 頂級接口 interface TheGreatestSage {// 定義一個描述形態的方法void shapeDesc (); } // 悟空本尊 class Monkey implements TheGreatestSage{@Overridepublic void shapeDesc() {System.out.println("Monkey.move()");} } // 包裝一層變化的描述 class Change implements TheGreatestSage {private TheGreatestSage greatestSage;Change(TheGreatestSage greatestSage){this.greatestSage = greatestSage;}@Overridepublic void shapeDesc() {greatestSage.shapeDesc();} } // 具體的變化形態 class Fish extends Change{public Fish(TheGreatestSage greatestSage) {super(greatestSage);}public void shapeDesc(){System.out.println("魚在水中游...");} } class Bird extends Change{public Bird(TheGreatestSage greatestSage) {super(greatestSage);}public void shapeDesc(){System.out.println("鳥在空中飛...");} }二、裝飾模式
1、基礎概念
裝飾模式又名包裝(Wrapper)模式。裝飾模式以對客戶端透明的方式擴展對象的功能,是繼承關系的一個替代方案。
裝飾模式以對客戶透明的方式動態地給一個對象附加上更多的責任。換言之,客戶端并不會覺得對象在裝飾前和裝飾后有什么不同。裝飾模式可以在不使用創造更多子類的情況下,將對象的功能加以擴展。
2、核心角色
- 抽象構件角色
給出一個抽象接口,以規范準備接收附加責任的對象。
- 具體構件角色
定義一個將要接收附加責任的類。
- 裝飾角色
持有一個構件對象的實例,并定義一個與抽象構件接口一致的接口。
- 具體裝飾角色
負責給構件對象“貼上”附加的責任。
3、模式圖解
4、源代碼實現
// 抽象構件角色 interface Component {void sampleOperation(); } // 具體構件角色 class ConcreteComponent implements Component{@Overridepublic void sampleOperation() {System.out.println("業務相關代碼");} } //裝飾角色 class Decorator implements Component{private Component component;public Decorator(Component component){this.component = component;}@Overridepublic void sampleOperation() {//委派給構件進行處理component.sampleOperation();} } // 具體裝飾角色 class ConcreteDecoratorA extends Decorator{public ConcreteDecoratorA(Component component) {super(component);}@Overridepublic void sampleOperation() {super.sampleOperation();System.out.println("A.處理相關業務的代碼");} } class ConcreteDecoratorB extends Decorator{public ConcreteDecoratorB(Component component) {super(component);}@Overridepublic void sampleOperation() {super.sampleOperation();System.out.println("B.處理相關業務方法");} }5、該模式簡化
- 簡化抽象類
如果只有一個ConcreteComponent類,那么可以考慮去掉抽象的Component類(接口),把Decorator作為一個ConcreteComponent子類。
- 簡化裝飾類
如果只有一個ConcreteDecorator類,那么就沒有必要建立一個單獨的Decorator類,而可以把Decorator和ConcreteDecorator的責任合并成一個類。
6、半透明說明
1)、純粹的裝飾模式很難找到。裝飾模式的用意是在不改變接口的前提下,增強類的功能。
2)、在增強功能的時候,往往需要建立新的公開的方法。
3)、這就導致了大多數的裝飾模式的實現都是“半透明”的,而不是完全透明的。換言之,允許裝飾模式改變接口,增加新的方法。這意味著客戶端可以聲明ConcreteDecorator類型的變量,從而可以調用ConcreteDecorator類中才有的方法。
4)、半透明的裝飾模式是介于裝飾模式和適配器模式之間的。適配器模式的用意是改變類的接口,也可以通過改寫一個或幾個方法,或增加新的方法來增強類的功能。
5)、大多數的裝飾模式實際上是半透明的裝飾模式,這樣的裝飾模式也稱做半裝飾、半適配器模式。
三、實際應用
1、JDK中IO流
1)、基本描述
裝飾模式在Java語言中的最經典的應用就是Java I/O類庫的設計。很少單一的創建流對象,通過創建多個疊合對象來提供所期望的IO流功能,因此裝飾模式是Java I/O類庫的基本模式。
2)、IO流圖解描述
- 抽象構件角色
由InputStream扮演。這是一個抽象類,為各種子類型提供統一的接口。
- 具體構件角色
由FileInputStream、StringBufferInputStream等類扮演。它們實現了抽象構件角色所規定的接口。
- 抽象裝飾角色
由FilterInputStream扮演。它實現了InputStream所規定的接口。
- 具體裝飾角色
由幾個類扮演,分別是BufferedInputStream、DataInputStream以及兩個不常用到的類LineNumberInputStream、PushbackInputStream。
四、優缺點總結
1、模式的優點
(1)裝飾模式與繼承關系的目的都是要擴展對象的功能,但是裝飾模式可以提供比繼承更多的靈活性。裝飾模式允許系統動態決定“貼上”一個需要的“裝飾”,或者除掉一個不需要的“裝飾”。繼承關系則不同,繼承關系是靜態的,它在系統運行前就決定了。
(2)通過使用不同的具體裝飾類以及這些裝飾類的排列組合,工程師可以創造出很多不同行為的組合。
2、模式的優點
由于使用裝飾模式,可以比使用繼承關系需要較少數目的類。使用較少的類,當然使設計比較易于進行。但是,在另一方面,使用裝飾模式會產生比使用繼承關系更多的對象。
五、源代碼地址
GitHub地址:知了一笑 https://github.com/cicadasmile/model-arithmetic-parent 碼云地址:知了一笑 https://gitee.com/cicadasmile/model-arithmetic-parent總結
以上是生活随笔為你收集整理的Java描述设计模式(09):装饰模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编程体系结构(04):JavaIO流文件
- 下一篇: 关于Java性能监控您不知道的5件事