意图

  • Attach additional responsibilities to an object dynamically.
  • 为一个对象动态的添加职责.
  • Decorators provide a flexible alternative to subclassing for extending functionality.
  • 对扩展功能来所,包装模式提供了一个比子类话更灵活的替代方式.

结构

  • Component : defines the interface for objects that can have responsibilities added to them dynamically.
  • Component : 为对象定义了一个接口使得可以动态的加载职责.
  • Decorator : maintains a reference to a Component object and defines an interface that conforms to Component's interface.
  • Decorator : 保有一个Component对象的引用并且定义了一个接口来执行Component中的接口方法.
  • ConcreteDecorator : adds responsibilities to the component.
  • ConcreteDecorator: 向component添加职责.

应用场景

  • to add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects.
  • 向单个对象动态和透明的添加职责,也就是说,不影响其他对象.
  • for responsibilities that can be withdrawn.
  • 对于可取消的职责.
  • when extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing.
  • 当通过子类化来扩展不现实时.有时需要大量单独的扩展并且为了支持每一种组合会使得子类膨胀.或者类可能被隐藏或者不能子类化.

作用效果

  • More flexibility than static inheritance. The Decorator pattern provides a more flexible way to add responsibilities to objects than can be had with static (multiple) inheritance. With decorators responsibilities can be added and removed at run-time simply by attaching and detaching them.
  • 比静态的继承更灵活.装饰模式提供了一种更灵活的方式来扩展对象的职责,相比于静态的(多个)子类.使用装饰器,职责可以在运行时添加或移除,只需装上货卸除他们.
  • Avoids feature-laden classes high up in the hierarchy. Decorator offers a pay-as-you-go approach to adding responsibilities. Instead of trying to support all foreseeable features in a complex, customizable class, you can define a simple class and add functionality incrementally with Decorator objects. Functionality can be composed from simple pieces. As a result, an application needn't pay for features it doesn't use.
  • 避免繁重的深层子类化.装饰器提供了按需的添加职责.避免试图在一个复杂的,客户化的子类中支持所有可预见的特性,你可以使用一个简单的类,然后增量式的装饰对象的功能.功能可以从一个个简单的部分组合.这样你的应用将不需要付出不使用的特性的代价.
  • A decorator and its component aren't identical. A decorator acts as a transparent enclosure. But from an object identity point of view, a decorated component is not identical to the component itself. Hence you shouldn't rely on object identity when you use decorators.
  • 一个装饰器和它的部件不是同一的.一个装饰器表现为一个透明的封装.但是用一个对象一致的角度,装饰的部件和部件本身是不一致的.因此当以使用包装器时应当不依赖于对象一致.
  • Lots of little objects. A design that uses Decorator often results in systems composed of lots of little objects that all look alike. The objects differ only in the way they are interconnected, not in their class or in the value of their variables. Although these systems are easy to customize by those who understand them, they can be hard to learn and debug.
  • 众多小对象.一个使用了装饰模式的设计导致一个有众多相似的小对象的系统.对象之间的区别仅仅在于他们之间的交互,而不在于他们的类或他们的值.虽然这里系统很容易被理解他们的人订制,但是他们很难学习和调试.

Java设计模式 Design Pattern:包装模式 Decorator Pattern相关推荐

  1. python中的装饰器、装饰器模式_python 设计模式之装饰器模式 Decorator Pattern

    #写在前面 已经有一个礼拜多没写博客了,因为沉醉在了<妙味>这部小说里,里面讲的是一个厨师苏秒的故事.现实中大部分人不会有她的天分.我喜欢她的性格:总是想着去解决问题,好像从来没有怨天尤人 ...

  2. python 设计模式之装饰器模式 Decorator Pattern

    #写在前面 已经有一个礼拜多没写博客了,因为沉醉在了<妙味>这部小说里,里面讲的是一个厨师苏秒的故事.现实中大部分人不会有她的天分.我喜欢她的性格:总是想着去解决问题,好像从来没有怨天尤人 ...

  3. 【java设计模式】【行为模式Behavioral Pattern】迭代器模式Iterator Pattern

    1 package com.tn.pattern; 2 3 public class Client { 4 public static void main(String[] args) { 5 Obj ...

  4. .net 有哪些主流的设计模式_「设计模式自习室」门面模式 Facade Pattern

    前言 <设计模式自习室>系列,顾名思义,本系列文章带你温习常见的设计模式.主要内容有: 该模式的介绍,包括: 引子.意图(大白话解释) 类图.时序图(理论规范) 该模式的代码示例:熟悉该模 ...

  5. 【设计模式自习室】门面模式 Facade Pattern

    前言 <设计模式自习室>系列,顾名思义,本系列文章带你温习常见的设计模式.主要内容有: 该模式的介绍,包括: 引子.意图(大白话解释) 类图.时序图(理论规范) 该模式的代码示例:熟悉该模 ...

  6. 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)

    [索引页] [源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:webabcd 介绍 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互 ...

  7. 设计模式 - 学习笔记 - 工厂模式Factory Pattern

    设计模式 - 学习笔记 - 工厂模式Factory Pattern 1. 简单工厂 1.1 应用场景 1.2 UML 1.3 优劣分析 好处 缺点 1.4 代码示例 抽象产品 AbstractProd ...

  8. Java设计模式(装饰者模式-组合模式-外观模式-享元模式)

    Java设计模式Ⅳ 1.装饰者模式 1.1 装饰者模式概述 1.2 代码理解 2.组合模式 2.1 组合模式概述 2.2 代码理解 3.外观模式 3.1 外观模式概述 3.2 代码理解 4.享元模式 ...

  9. Java设计模式之中介者模式(UML类图分析+代码详解)

    大家好,我是一名在算法之路上不断前进的小小程序猿!体会算法之美,领悟算法的智慧~ 希望各位博友走过路过可以给我点个免费的赞,你们的支持是我不断前进的动力!! 加油吧!未来可期!! 本文将介绍java设 ...

最新文章

  1. 数据库初学者_面向初学者的免费6小时数据科学课程
  2. 获清华特奖又上“最强大脑”!这位90后学神去一线抗疫的理由是……
  3. 徘徊于win和ubuntu
  4. script type=text/template是干什么的,为什么要把html写在js中? 这是什么编程语言风格,都能这样用吗?...
  5. UVA 10910 Marks Distribution(组合数学 或 递推)
  6. 学长毕业日记 :本科毕业论文写成博士论文的神操作20170402
  7. java kettle6_Java调用Kettle6的transaction和job
  8. 亚马逊DynamoDB
  9. 良心推荐丨国庆7天长假,程序员最佳度假去处
  10. python try catch打印到日志_django 捕获异常和日志系统过程详解
  11. windows + Linux 自定义模板配置 怎么使用自定义规范管理器
  12. 内存数据集产生的隐性成本
  13. USACO 4.3 Street RaceThe Primes(跪了)
  14. 为什么说12306比淘宝双十一的技术挑战更大?
  15. android studio umake,Android Studio中NDK开发傻瓜教程(CMake)
  16. 中医针灸学综合练习题库【10】
  17. 隐藏百度地图logo
  18. (十四)ATP应用测试平台——使用docker-compose一键式安装ATP应用测试平台的依赖服务
  19. Facebook新闻摘要算法是怎样的
  20. Kafka bootstrap.servers

热门文章

  1. 第六章 颠覆与重塑思维,大数据与思维革命
  2. 博物馆3d数字化全景展示设计方案
  3. oracle 优化 io占用,oracle 整体性能优化概述之五:调整磁盘io
  4. onsemi安森美FDMS86252L 50V 12A 56mΩ N沟道屏蔽门极MOSFET管
  5. 移植 u-boot-2020.07 到 iTOP-4412(二)地址相关码 boot
  6. Solidity智能合约库:区块链工程师的随身工具箱
  7. R语言数据分析笔记——Cohort 存留分析
  8. uni-app 小程序 微信订阅消息通知
  9. Himall商城ExpressDaDaHelper订单预发布 查询运费后发单接口
  10. jquery实现HTML复选框变单选框