Monkey 平 Monkey 平
首页
  • JAVA知识点
  • Docker
  • Linux
友情链接
关于
GitHub (opens new window)

Evan Xu

前端界的小学生
首页
  • JAVA知识点
  • Docker
  • Linux
友情链接
关于
GitHub (opens new window)
  • 设计模式

    • JAVA设计模式-单例模式
    • JAVA设计模式-工厂模式
    • JAVA设计模式-建造者模式
    • JAVA设计模式-原型模式
    • JAVA设计模式-适配器模式
    • JAVA设计模式-装饰模式
      • JAVA设计模式-装饰模式
        • 介绍
        • 角色
        • 示例
        • TextAreaCompont
        • TextAreaConcreteCompont
        • TextAreaDecorator
        • CountTextAreaDecorator
        • Test
        • 运行结果
        • 总结
    • JAVA设计模式-外观模式
    • JAVA设计模式-代理模式
    • JAVA设计模式-桥接模式
  • JAVA知识点
  • 设计模式
Monkey 平
2022-10-15
目录

JAVA设计模式-装饰模式

# JAVA设计模式-装饰模式

# 介绍

装饰模式是一种结构型模式,在不改变现有对象结构的前提下,给现有对象添加新的功能,动态增加职责,是现有类的一个包装。

# 角色

  1. Component:定义一个对象接口。
  2. ConcreteComponent:定义一个对象,是Component的具体实现类,也是被装饰的对象。
  3. Decorator:抽象装饰类,里面定义了和Component一样的接口,并且包含一个Component对象的引用。
  4. ConcreteDecorator:抽象装饰类Decorator的具体实现类,负责向对象添加职责。

# 示例

我们以一个文本框为例,文本框可以正常输入信息,我们需要给他添加一些职责,例如输入信息后统计输入的信息长度的功能。

# TextAreaCompont

/**
 * All rights Reserved, Designed By monkey.blog.xpyvip.top
 *
 * @version V1.0
 * @Package com.xpyvip.designpattern.decorator
 * @Description: 文本框对象接口
 * @Author: xpy
 * @Date: Created in 2022年10月05日 11:16 上午
 */
public interface TextAreaCompont {

    /**
     * 输入
     * @param str
     * @return
     */
    String input(String str);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# TextAreaConcreteCompont

/**
 * All rights Reserved, Designed By monkey.blog.xpyvip.top
 *
 * @version V1.0
 * @Package com.xpyvip.designpattern.decorator
 * @Description: 文本框对象类
 * @Author: xpy
 * @Date: Created in 2022年10月05日 1:19 下午
 */
public class TextAreaConcreteCompont implements TextAreaCompont{
    public String input(String str) {
        return str;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# TextAreaDecorator

/**
 * All rights Reserved, Designed By monkey.blog.xpyvip.top
 *
 * @version V1.0
 * @Package com.xpyvip.designpattern.decorator
 * @Description: 文本框抽象装饰类
 * @Author: xpy
 * @Date: Created in 2022年10月05日 1:19 下午
 */
public abstract class TextAreaDecorator implements TextAreaCompont{

    private TextAreaCompont textAreaCompont;

    public TextAreaDecorator(TextAreaCompont textAreaCompont) {
        this.textAreaCompont = textAreaCompont;
    }

    public String input(String str) {
        return textAreaCompont.input(str);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# CountTextAreaDecorator

/**
 * All rights Reserved, Designed By monkey.blog.xpyvip.top
 *
 * @version V1.0
 * @Package com.xpyvip.designpattern.decorator
 * @Description: 文本框抽象装饰类具体实现类-统计输入字符长度
 * @Author: xpy
 * @Date: Created in 2022年10月05日 1:19 下午
 */
public class CountTextAreaDecorator extends TextAreaDecorator{

    public CountTextAreaDecorator(TextAreaCompont textAreaCompont) {
        super(textAreaCompont);
    }

    @Override
    public String input(String str) {
        int length = str.length();
        System.out.println("输入的字符长度为:" + length);
        return super.input(str);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# Test

/**
 * All rights Reserved, Designed By monkey.blog.xpyvip.top
 *
 * @version V1.0
 * @Package com.xpyvip.designpattern.decorator
 * @Description: 测试类
 * @Author: xpy
 * @Date: Created in 2022年10月05日 1:35 下午
 */
public class Test {
    public static void main(String[] args) {
        TextAreaCompont textAreaCompont = new TextAreaConcreteCompont();
        TextAreaDecorator textAreaDecorator = new CountTextAreaDecorator(new TextAreaConcreteCompont());

        System.out.println("原始组件输入");
        String textAreaCompontInput = textAreaCompont.input("123456");
        System.out.println("原始组件输入结果" + textAreaCompontInput);
        System.out.println("======================");
        System.out.println("装饰之后组件输入");
        String textAreaDecoratorInput = textAreaDecorator.input("123456");
        System.out.println("装饰之后组件输入结果" + textAreaDecoratorInput);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 运行结果

image-20221005134528261

# 总结

装饰模式可以动态的扩展一个实现类的功能,动态添加功能或动态撤销,并且不会修改原有对象的结构,是继承的一个替代模式。比较容易搞混的是代理模式,代理模式主要是由代理对象去控制原有对象。装饰模式主要注重增加撤销功能,代理模式主要强调对原有对象的控制。

编辑 (opens new window)
上次更新: 2023/02/26, 10:03:01
JAVA设计模式-适配器模式
JAVA设计模式-外观模式

← JAVA设计模式-适配器模式 JAVA设计模式-外观模式→

最近更新
01
Linux文件夹权限操作
10-30
02
Linux基础知识一
10-25
03
JAVA设计模式-代理模式
10-18
更多文章>
Theme by Vdoing | Copyright © 2019-2023 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式