`
dr2tr
  • 浏览: 138476 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Design Pattern -- Decorator

阅读更多

Decorator 模式, 其功能为;Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.  

类图表示为:

解释一下这张图,这个模式主要有两点:

1。所需功能(Operation) 的部分实现是通过继承一个接口(或父类,通常也是Decoratee的父类)得到的

2。Decorator 通常聚合有它与Dectratee的(公共)父类

比较简单的关于这个的例子是改造Swing中JScrollPane和JTextArea的一种设计。我们知道,JTextArea需要卷轴的话,需要把它放到JScrollPane上。比如:

JTextArea textArea = new JTextArea();
JScrollPane scrollPane 
= new JScrollPane(textArea);

对此,我们可以通过Decrator模式进行改造试验。

如类图:

Decorator

以TextView 作为JTextArea,之前所说的功能我们只需要:

ScrollDecorator scrollDecorator = 
                    
new ScrollDecorator(new TextView());

就可以完成了。

有关这个模式,经常有说起Java IO 中String 的设计。这里不做多说,因为网上相关资料应该不少。另外,Decorator 和Composite,Decorator 和State、Strategy 的区别,有空再写吧。

参考:

http://en.wikipedia.org/wiki/Decorator_pattern

http://www.nshen.net/designpattern/DesignPattern/DecoratorPattern.htm

http://www.dofactory.com/Patterns/PatternDecorator.aspx

http://www.cnblogs.com/singlepine/archive/2005/10/30/265023.html

http://www.enet.org.cn/Article/wybc/qtlb/200603/Article_14822.html

分享到:
评论

相关推荐

    Design Pattern - Decorator

    NULL 博文链接:https://tcg.iteye.com/blog/1283190

    design-pattern-java.pdf

    树形结构的处理——组合模式(二) 树形结构的处理——组合模式(三) 树形结构的处理——组合模式(四) 树形结构的处理——组合模式(五) 装饰模式-Decorator Pattern 扩展系统功能——装饰模式(一) 扩展系统...

    decorator-design-pattern

    decorator-design-pattern

    quanke#design-pattern-java#扩展系统功能——装饰模式(二)1

    装饰模式的核心在于抽象装饰类的设计,其典型代码如下所示://维持一个对抽象构件对象的引用public Decorator(Component component

    CoreJava-DesignPattern

    CoreJava-DesignPattern 创意设计模式 -- Abstract Factory - Done -- Builder - Done -- Factory Method -- Object Pool -- Prototype - Done -- Singleton - Done 结构设计模式 -- Adapter -- Bridge -- ...

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm”供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) 生成...

    Decorator-Design-Pattern

    2017-2018设计原则 项目1 项目定义 使用我们在课堂上介绍的一种(或可能多种)设计模式解决软件开发问题。 去做 工作说明:使用100-200个字详细地写出您的问题定义。 设计模式:选择对解决您定义的问题很有用的设计...

    36种最新设计模式整理

    Design Pattern: Decorator 模式 41 Design Pattern: Facade 模式 44 Design Pattern: Flyweight 模式 46 Design Pattern: Proxy 模式(一) 48 Design Pattern: Proxy 模式(二) 49 Design Pattern: Chain of ...

    laravel-decorator:使用laravel-decorator包轻松装饰您的方法调用

    composer require imanghafoori/laravel-decorator 什么是"Decorator" :red_question_mark: 装饰器是可调用的,它包装原始装饰的可调用对象,以形成由前两个组成的新可调用对象。 就像一条Python蛇吞下了整个鹿,...

    Java.Design.Patterns.1537192353

    DECORATOR PATTERN FLYWEIGHT PATTERN TEMPLATE METHOD PATTERN MEDIATOR PATTERN CHAIN OF RESPONSIBILITY ... OBSERVER PATTERN STRATEGY PATTERN COMMAND PATTERN VISITOR PATTERN STATE PATTERN ITERATOR ...

    Design.Patterns.Explained.Simply

    Decorator Facade Flyweight Private Class Data Proxy Behavioral patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Null Object Observer State Strategy Template Method ...

    Design Patterns Elements of Reusable Object-Oriented Software

    • What Is a Design Pattern? • Design Patterns in Smalltalk MVC • Describing Design Patterns • The Catalog of Design Patterns • Organizing the Catalog • How Design Patterns Solve Design ...

    《Java Design Patterns》高清完整英文PDF版

    Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....

    Design Patterns in Modern C++--2018

    Implement structural patterns such as adapter, bridge, decorator, facade and more Work with the behavioral patterns such as chain of responsibility, command, iterator, mediator and more Apply ...

    单例模式源码java-DesignPattern:在个人自学阶段的23种设计模式代码的全部实现,全部使用Java编写,其中还包括各个设计模式在

    Decorator-------->装饰模式 Facade-------->外观模式 FlyWeight-------->享元模式 Proxy-------->代理模式 1.3 行为型模式 Visitor-------->访问者模式 Template-------->模板模式 Strategy-----...

    java设计模式源码-DesignPattern:设计模式(Java实现源码)

    欢迎访问DesignPattern项目 DesignPattern项目是设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。 设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。...

    设计模式 design pattern

    2.4.3 Decorator 模式 32 2.5 支持多种视感标准 32 2.5.1 对象创建的抽象 32 2.5.2 工厂类和产品类 33 2.5.3 Abstract Factory模式 35 2.6 支持多种窗口系统 35 2.6.1 我们是否可以使用Abstract Factory 模式 35 ...

    Apress.Pro.Design.Patterns.in.Swift

    The Decorator Pattern Chapter 15. The Composite Pattern Chapter 16. The Facade Pattern Chapter 17. The Flyweight Pattern Chapter 18. The Proxy Pattern Part 4 - The Behavioral Patterns Chapter 19. ...

    C#设计模式-吕震宇

    C#设计模式(12)-Decorator Pattern C#设计模式(11)-Composite Pattern C#设计模式(10)-Adapter Pattern C#设计模式(9)-Prototype Pattern C#设计模式(8)-Builder Pattern C#设计模式(7)-Singleton...

Global site tag (gtag.js) - Google Analytics