知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

    • Design pattern

      • Theory:The concept of patterns
      • Theory:Structural of design patterns
      • Theory:Decorator pattern
      • Theory:Decorator
        • Decorator
        • Practical example
        • Conclusion
    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Design pattern
Jim
2022-10-17
目录

Theory:Decorator

Sometimes it is necessary to impose additional responsibilities on a separate object rather than the whole class. A library for building graphics in the user interface should be able to add a new property, say, frames or new behavior (for example, the ability to scroll to any element). Adding new responsibilities is permissible through inheritance. However, this solution is static, and therefore not flexible enough.

# Decorator

A more flexible approach is to put the component in another object called the decorator. The Decorator is a structural pattern used to add new responsibilities to an object dynamically without extending functionality. That lets you dynamically change the behavior of an object at runtime by wrapping it in an object of a decorator class.

Decorators are used for adding some behavior that is not part of the core functionality to all interface methods. Decorator pattern perfectly suits the following tasks:

  • caching the work results;
  • measuring the execution time of methods;
  • user access control.

The decorator pattern has the following components:

  • Component is the interface for the objects that will get new responsibilities from the decorators;
  • Concrete Component defines objects which implement the Component interface and will get new responsibilities from the concrete decorators;
  • Decorator has reference to the Component and overridden component methods;
  • Concrete Decorator extends Decorator class and adds new functions, properties or state without creating new classes;

img

The decorator pattern in JDK:

  • Streams: java.io package;
  • Collections: java.util package.

# Practical example

Let’s consider a more specific example. Our components are software developers that have to make some job, that’s why we create the Developer interface:

public interface Developer {

    public String makeJob();
}
1
2
3
4

Next, we create a concrete developer:

public class JavaDeveloper implements Developer {

    public String makeJob() {
        return "Write Java Code.";
    }
}
1
2
3
4
5
6

Now, describe the developer decorator to add functionality to our developers dynamically:

public class DeveloperDecorator implements Developer {
    private Developer developer;

    public DeveloperDecorator(Developer developer) {
        this.developer = developer;
    }

    public String makeJob() {
        return developer.makeJob();
    }
}
1
2
3
4
5
6
7
8
9
10
11

The concrete decorator is the senior java developer who has an important additional responsibility: code review.

public class SeniorJavaDeveloper extends DeveloperDecorator {

    public SeniorJavaDeveloper(Developer developer) {
        super(developer);
    }

 
    public String makeCodeReview() {
        return "Make code review.";
    }

    public String makeJob() {
        return super.makeJob() + " " + makeCodeReview();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

The second decorator is the team leader: being also a developer, the leader additionally has to communicate with customers and send weekly reports:

public class JavaTeamLead extends DeveloperDecorator {

    public JavaTeamLead(Developer developer) {
        super(developer);
    }

    public String sendWeekReport() {
        return "Send week report to customers.";
    }

    public String makeJob() {
        return super.makeJob() + " " + sendWeekReport();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Here is the final demo of the Decorator pattern:

public class Task {

    public static void main(String[] args) {
        Developer developer = new JavaTeamLead(
                                  new SeniorJavaDeveloper(
                                      new JavaDeveloper()));
        
        System.out.println(developer.makeJob());
    }
}
1
2
3
4
5
6
7
8
9
10

# Conclusion

The decorator pattern is applicable in the following cases:

  • When you want to add new properties and functions to the object dynamically;
  • When the extension of classes is superfluous.
编辑 (opens new window)
#Pattern
上次更新: 2022/10/25, 17:57:56
Theory:Decorator pattern
Theory:World Wide Web

← Theory:Decorator pattern Theory:World Wide Web→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式