Java设计模式之装饰模式
0. 前言
写在最前面,本人的设计模式类博文,建议先看博文前半部分的理论介绍,再看后半部分的实例分析,最后再返回来复习一遍理论介绍,这时候你就会发现我在重点处标红的用心,对于帮助你理解设计模式有奇效哦~本文原创,转载请注明出处为SEU_Calvin的博客。
装饰模式是结构型设计模式之一,使用一种对客户透明的方式来动态拓展对象的功能,Java IO中各种流的嵌套就是装饰模式的很好体现。
1. 装饰模式介绍
装饰模式定义:
动态地给一个对象添加一些额外的功能,比生成子类要灵活。
装饰模式的使用场景:
需要透明且动态地拓展类的功能的场景。
装饰模式包括的角色:
(1)抽象组件Component类。
(2)组件具体实现ConcreteComponent类。也是被装饰的对象。
(3)抽象装饰类Decorator,内部持有一个组件对象的引用,职责就是装饰ConcreteComponent类。之所以是抽象的,就是为了方便不同的装饰“风格”子类的自定义实现。
(4)具体装饰类ConcreteDecorator。
2. 装饰模式实例介绍
想到装饰,很容易想到一个人穿衣服,在不同的季节穿不同的衣服,比如夏天穿短裤、冬天穿毛裤,但是不管什么季节,内裤肯定是要穿的。
抽象组件Component类就可以是抽象类Person,而ConcreteComponent类就可以是具体的一个人Calvin,也是要被装饰的对象。抽象装饰类Decorator持有了Calvin的引用,并且回调了Calvin本身的一个“穿内裤”的行为,并且在具体的装饰类中,即“冬夏”两个类中,分别为主人公Calvin穿上了毛裤和短裤,为对象增加了额外的功能。代码也比较简单,如下所示。
- /**
- * Decorator Pattern
- * Created by Calvin on 2017/5/10.
- */
- public class Decorator {
- public static void main(String[] args) {
- Person calvin = new Calvin();
- PersonCloth summerCloth = new SummerCloth(calvin);
- PersonCloth winterCloth = new WinterCloth(calvin);
- summerCloth.dressed();
- winterCloth.dressed();
- }
- public static abstract class Person{
- public abstract void dressed();
- }
- public static class Calvin extends Person{
- @Override
- public void dressed() {
- System.out.println("穿件内裤");
- }
- }
- public static abstract class PersonCloth extends Person{
- Person myPerson;
- public PersonCloth(Person myPerson) {
- this.myPerson = myPerson;
- }
- @Override
- public void dressed() {
- myPerson.dressed();
- }
- }
- public static class SummerCloth extends PersonCloth{
- public SummerCloth(Person myPerson) {
- super(myPerson);