`
DavyJones2010
  • 浏览: 151808 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Design Pattern: Decorator Pattern

阅读更多

Decorator Pattern

 

1) Motivation:

    Extending an object's functionality can be done statically (at compile time) by using inherientence. However it might be necessary to extend an object's functionality dynamically(at runtime) as an object is used.

 

    Consider the typical example of a graphical window. To extend the functionality of a graphical window, for example by adding a frame to the window, would require extending the window class to create a FrameWindow class. To create a FrameWindow, it is necessary to create an object of FrameWindow class. However, it would be impossible to start with a plain window and to extend its functionality at runtime to become a framed window.

 

2) Intent:

    The intent of this pattern is to add additional responsibilty dynamically(at runtime) to an object.

 

3) Implementation:

 


    1> Component:

            Interface for objects that can have responsibilities added to them dynamically.

    2> ConcreteComponent:

            Defines an object to which additional responsibilites can be added.

    3> Decorator:

            Maintains a reference to a Component object and defines an interface that conforms to Component's interface.

    4> ConcreteDecorator:

            Extends the functionality of the Component by adding state or adding behavior. 

 

4) Description:

    The decorator pattern applies when there is a need to dynamically add as well as remove responsibilites to a class, and when subclassing would be impossible due to the large number of subclasses that could result.

 

5) Applicability & Examples:

    Example: Extending capabilities of a Graphical Window at runtime



package edu.xmu.oop.decorator;

public interface Window {
    public void renderWindow();
}
package edu.xmu.oop.decorator;

public class SimpleWindow implements Window {
    public void renderWindow() {
	System.out.println(String.format("Start render for: [%s]", this));
	System.out.println(String.format("Finished render for: [%s]", this));
    }
}
package edu.xmu.oop.decorator;

public class DecoratedWindow implements Window {
    private Window windowReference;

    public DecoratedWindow(Window windowReference) {
	super();
	this.windowReference = windowReference;
    }

    public void renderWindow() {
	windowReference.renderWindow();
    }
}
package edu.xmu.oop.decorator;

public class ScrollableWindow extends DecoratedWindow {

    private String scrollBarObjectRepresentation;

    public ScrollableWindow(Window windowReference,
	    String scrollBarObjectRepresentation) {
	super(windowReference);
	this.scrollBarObjectRepresentation = scrollBarObjectRepresentation;
    }

    @Override
    public void renderWindow() {
	renderScrollBar();
	super.renderWindow();
    }

    private void renderScrollBar() {
	System.out.println(String.format("Start renderScrollBar for: [%s]",
		this));
	System.out.println(scrollBarObjectRepresentation);
	System.out.println(String.format("Finished renderScrollBar for: [%s]",
		this));
    }
}
package edu.xmu.oop.decorator;

public class FramableWindow extends DecoratedWindow {

    public FramableWindow(Window windowReference) {
	super(windowReference);
    }

    @Override
    public void renderWindow() {
	addFrame();
	super.renderWindow();
    }

    private void addFrame() {
	System.out.println(String.format("Start addFrame for: [%s]", this));
	System.out.println(String.format("Finished addFrame for: [%s]", this));
    }
}
package edu.xmu.oop.decorator;

import org.junit.Test;

public class WindowTest {
    private Window window;

    @Test
    public void renderWindowTest() {
	window = new SimpleWindow();
	window.renderWindow();
	System.out.println("======================================");

	window = new FramableWindow(window);
	window.renderWindow();
	System.out.println("======================================");

	window = new ScrollableWindow(window, "ScrollBar");
	window.renderWindow();
    }
}

 

    Example: Java I/O Stream

 

package edu.xmu.oop.decorator;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

import org.junit.Test;

@SuppressWarnings("deprecation")
public class InputStreamTest {
    @Test
    public void testRead() throws IOException {
	LineNumberInputStream inputStream = new LineNumberInputStream(
		new BufferedInputStream(new FileInputStream(new File(
			"src/test/resources/test.dat"))));
	while (-1 != inputStream.read()) {
	    System.out.println(inputStream.getLineNumber());
	}
	inputStream.close();
    }
}

 

6) Related Patterns:

    1> Adapter Pattern: A decorator is different from an adapter in that a Decorator changes object's responsibilites, while an Adapter changes object interfaces.

    2> Composite Pattern: A decorator can be viewed as a degenerate composite with only one component. However, a decorator adds additional responsibilites.

 

7) Consequences:

    1> Decoration is more convenient for adding functionalities to objects instead of entire class at runtime. With Decoration it is also possible to remove the added functionalities dynamically.

    2> Decoration added functionalities to object at runtime which would make debugging system functionality harder which just like Proxy Pattern.

 

 

Reference Links:

1) http://www.oodesign.com/decorator-pattern.html

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

3) http://stackoverflow.com/questions/3068912/what-is-the-most-used-pattern-in-java-io

4) http://oreilly.com/catalog/hfdesignpat/chapter/ch03.pdf

 

  • 大小: 38.4 KB
  • 大小: 32.4 KB
  • 大小: 30.9 KB
分享到:
评论

相关推荐

    Design Pattern - Decorator

    装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许在运行时给对象添加新的行为或职责,同时保持对象的接口不变。这种模式的核心在于,它动态地将责任附加到对象上,通过将对象包装在一个装饰类中来扩展...

    design pattern

    在给定的压缩包文件中,包含了九种经典的设计模式示例,它们分别是:单例模式(Singleton)、策略模式(StrategyPattern)、适配器模式(AdapterPattern)、装饰者模式(DecoratorPattern)、抽象工厂模式...

    DesignPattern::pencil:设计模式_java实现以及详解

    本资源“DesignPattern::pencil:设计模式_java实现以及详解”提供了一套详细的学习材料,帮助开发者理解和应用设计模式。 该资源的作者是“养码青年-Style”,他通过这个项目记录了自己的设计模式学习过程。鼓励...

    FOAD-DesignPattern:FOAD-DesignPattern

    6. 装饰模式(Decorator Pattern): 装饰模式允许动态地给一个对象添加一些额外的职责,提供了比继承更灵活的扩展对象功能的方式。在C#中,装饰模式通常通过继承并组合使用,以增加对象的功能,而不会破坏其原有的...

    Design*Pattern*Framework*4.5

    "Design*Pattern*Framework*4.5" 可能指的是一个基于 .NET Framework 4.5 的设计模式实现或教程集合。 设计模式是经验丰富的软件开发者的智慧结晶,它们被分为三类:创建型、结构型和行为型。创建型模式涉及对象的...

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

    java 设计模式 源码 欢迎访问DesignPattern项目 ...装饰器模式(decoratorPattern) 外观模式(facadePattern) 享元模式(flyweightPattern) 代理模式(proxyPattern) 责任链模式(chainPattern) 命令模式(commandPatter

    DesignPattern:C#设计模式示例

    "DesignPattern:C#设计模式示例"这个资源很可能是包含多个C#实现的设计模式示例代码库。 设计模式通常分为三类:创建型、结构型和行为型。每种模式都解决了特定场景下的问题,并提供了良好的代码组织和扩展性。 ...

    DesignPattern:设计模式小Demo

    设计模式是软件工程中的一种最佳实践,用于解决在软件设计中常见的...以上就是这个DesignPattern小Demo中可能会涵盖的设计模式,通过这些模式的实例,你可以更好地理解和应用它们到实际项目中,提升你的Java编程能力。

    DesignPattern:这是针对不同设计人员的测试项目

    这个名为"DesignPattern:这是针对不同设计人员的测试项目"的压缩包,显然是一个与设计模式相关的学习或测试资源,特别关注的是Java语言的应用。Scott可能是该项目的创建者或者主要贡献者,而“调整”可能指的是对...

    design pattern tutorial

    10. 装饰器模式(Decorator Pattern):动态地给一个对象添加一些额外的职责。它提供了一种灵活的扩展一个对象功能的方式,而不需要使用继承。 11. 外观模式(Facade Pattern):提供了一个统一的接口,用来访问子...

    DesignPattern:有关设计模式的一些演示

    在这个"DesignPattern-master"的项目中,可能包含了以上提到的各种设计模式的实现示例,每个模式可能有一个或多个Java类来演示。通过阅读和分析这些示例,开发者可以更好地理解设计模式的工作原理,并能在实际项目中...

    DesignPattern:来自 http 的设计模式参考

    在"DesignPattern-master"这个压缩包中,可能包含了这些模式的Java实现示例,以及相关的解释和用法说明。通过学习和应用这些模式,开发者可以提高代码质量,降低维护成本,并提升软件系统的可扩展性。对于任何Java...

    DesignPattern:设计模式.net源代码

    在"DesignPattern-master"这个压缩包中,你可能找到的文件结构和内容包括: 1. **单例模式(Singleton)**:确保一个类只有一个实例,并提供一个全局访问点。在.NET中,通常使用静态属性或双重检查锁定来实现。 2....

    DesignPattern:设计模式

    DesignPattern-master这个压缩包可能包含了一个关于设计模式的项目或者教程资源。 设计模式分为三类:创建型模式(Creational Patterns)、结构型模式(Structural Patterns)和行为型模式(Behavioral Patterns)...

    designpattern:Head First 设计模式练习

    此外,装饰器模式(Decorator)允许动态地给对象添加新的行为或职责,而不影响其其他对象。Java的IO流就是装饰器模式的一个经典应用,如BufferedInputStream和DataInputStream。 适配器模式(Adapter)将两个不兼容...

    designPattern:设计模式相关代码实现

    在"designPattern-master"这个项目中,你可以期待找到以上所有或部分设计模式的Java代码实现,这将是一个宝贵的资源,可以帮助你深入理解和掌握这些模式的实际运用,提升你的编程技能。通过阅读和实践这些代码,你...

    DesignPattern:设计模式演示程序

    这个名为"DesignPattern"的压缩包文件很可能包含了一个Java实现的各种设计模式的示例程序。 在这个"DesignPattern-master"目录中,我们可以期待找到一系列与设计模式相关的Java源代码文件(.java),每个文件或...

    java源码解读-DesignPattern:Android源码设计模式解析与实战读书笔记源代码

    DesignPattern Android源码设计模式解析与实战读书笔记源代码 说明: 包名factorypattern.normal表示的是工厂方法模式的普通用法 包名factorypattern.practices表示的是工厂方法模式的常用 包名observerpattern表示...

    designPattern:코딩사전님의정정정리

    "designPattern-main"可能包含了关于这些设计模式的详细解释、示例代码以及可能的改进和修正。通过学习这个压缩包中的资料,开发者不仅可以深入理解各种设计模式,还能了解到"정정전"对这些模式的独特见解和实践经验...

    阅读java源码-JavaDesignPattern:23种设计模式Java实现

    在“阅读java源码-JavaDesignPattern:23种设计模式Java实现”中,我们将深入探讨这23种设计模式的Java实现。 1. **创建型模式**(Creational Patterns): - **单例模式(Singleton)**:确保一个类只有一个实例,...

Global site tag (gtag.js) - Google Analytics