`

装饰者模式——Decorator

 
阅读更多

装饰者模式—— Decorator

         有时间还是看一下《 Java 设计模式》,临近期末,为了节省时间我直接找了一些资料总结了一下。

 

1. Decorator 好处 / 目的

         A. 如果使用继承 实现功能扩展:

         我们通常使用继承来实现功能扩展,随着这些需要扩展的功能的增多,扩展功能的组合会导致更多的子类的膨胀。

         同时,使用继承实现功能的扩展,我们必须可预见这些拓展功能,这些功能编译时就确定了,是静态的。

         B. 如果使用 Decorator 实现功能扩展:

         如果扩展功能需要由用户动态决定加入的方式和时机, Decorator 提供了“即插即用”的方法,即在运行期间决定何时增加何种功能。

         这样,避免了在层次结构高层有太多的特征, Decorator 模式提供了一种“即插即用”的方法添加职责,他并不试图在一个复杂的可定制的类中支持所有可预见的特征;相反,可以定义一个简单的类,并且用 Decorator 类给他们逐渐添加功能,可以从简单的部件组合出复杂的功能。

 

         总结: The intent of Decorator is to let you compose new variations of an operation at runtime .

 

2. 如何使用 Decorator


         . Decorator 模式

 

         注意到,装饰者类 Decorator 既在①内部将被装饰者 Component 作为成员(保证请求的传递,不必实现 ConcreteComponent 已经实现的功能);又②实现了 Component 接口(保证客户端使用 Decorator 就如同使用 Component 一样)

 

         如何记忆:

         (1)记住Decorator到Component的两条线:关联——为了构造函数能传入Component;继承——为了构造函数能返回Component.

         (2)被装饰者一定要有抽象接口Component:这样才能支持装饰者模式!

 

3. Decorator 举例

         实际上, Java I/O API 就是使用 Decorator 实现的,如果采用继承的方式,将会产生很多很多子类!

         下面仅通过一个简单的例子实现上面图中的功能。

 

客户端代码: 

package decorator;

public class Client {
	public static void main(String[] args) {
		Component component=new ConcreteComponent();
		Component decorator=new Decorator(component);
		
		component.setName("小王");
		System.out.println(component.getName());
		
		decorator.setName("小王");
		System.out.println(decorator.getName());
	}
}
 

被装饰者和被装饰者实现:

package decorator;

public interface Component {
	public void setName(String name);
	public String getName();
}

public class ConcreteComponent implements Component {
	String name;

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name=name;
	}
}

 

装饰者:

package decorator;

public class Decorator implements Component {
	//如果有多个组件,可能用到private List<Component> list;
	private Component component;
	
	public Decorator(Component c){
		component=c;
	}
	
	@Override
	public String getName() {
		/*
		 * 在name前加上"名字是: "——这个效果就是装饰者达到的作用
		 */
		return "名字是: "+component.getName();
	}

	@Override
	public void setName(String name) {
		component.setName(name);
	}
}
 

输出效果:


 

参见《设计模式》板桥里人 http://www.jdon.com 2002/04/28

参见 装饰模式 (Decorator) 解析例子 ,http://tianli.blog.51cto.com/190322/35287

参见《 Java 设计模式》

  • 大小: 10.1 KB
  • 大小: 3.6 KB
分享到:
评论

相关推荐

    装饰模式——装饰模式.doc

    装饰模式.doc 装饰模式.doc 装饰模式.doc

    .Net中的设计模式——Decorator模式

    一、模式概述一个场景是我们要为一个对象动态添加新的职责,这个职责并不修改原有的行为,而是在原有行为基础上添加新的功能,就好比装饰工人为一座新居的墙上涂抹上色彩缤纷的颜料一般。 从我们拥有的面向对象的...

    设计模式代码——c#

    8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 行为型 13. 模板方法(Template Method) ...

    设计模式可复用面向对象软件的基础.zip

    4.4 Decorator(装饰)—对象结构型 模式 115 4.5 FACADE(外观)—对象结构型 模式 121 4.6 Flyweight(享元)—对象结构型 模式 128 4.7 Proxy(代理)—对象结构型 模式 137 4.8 结构型模式的讨论 144 4.8.1 ...

    design-pattern-java.pdf

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

    设计模式--可复用面向对象软件的基础

    4.4 DECORATOR(装饰)——对象结构型模式 4.5 FACADE(外观)——对象结构型模式 4.6 FLYWEIGHT(享元)——对象结构型模式 4.7 PROXY(代理)——对象结构型模式 4.8 结构型模式的讨论 第五章 行为模式 5.1 CHAIN ...

    Pattern-Decorator-Java:设计模式工作坊——装饰者模式

    您必须实现装饰器模式,以便可以在运行时添加提到的服务。 作为开发服务如何工作的示例,在运行时创建一个用加密和缓存装饰的 JSON 策略,以及一个带有日志记录的 XML 策略。 请记住,您必须提供源代码和详细说明所...

    Python设计模式之装饰模式实例详解

    本文实例讲述了Python设计模式之装饰...设计模式——装饰模式 装饰模式(Decorator Pattern):动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活. 特点: 有效的把类的核心职责和装饰功能区

    .net设计模式之装饰模式(Decorator)

    动态的给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生产子类更加灵活——《大话设计模式》; 结构图: 优点: 装饰类和被装饰类可以独立发展,不会相互耦合; 动态的扩展一个对象的功能; 可以对...

    管理系统javasal源码-Design-Patterns-Demo:超全的设计模式——理论+实现demo

    管理系统java sal源码 [toc] 设计模式 源码地址: 博客地址: 分类 ...Pattern)装饰器模式(Decorator Pattern)外观模式(Facade Pattern)享元模式(Flyweight Pattern)代理模式(Proxy Pattern)

    java高手真经 (UML建模+设计模式+面向服务架构) 卷8

    pattern/src/creation/builder //11.4建造者模式 pattern/src/creation/prototype //11.5原型模式 pattern/src/structure/adapter //12.1适配器模式 pattern/src/structure/decorator //12.2装饰器模式 pattern/src/...

    深入浅出设计模式(中文版)

    4.4.2现实中的装饰模式——相架 126 4.4.3C#实例——图书馆中的项目 127 4.4.4Java实例——自定义JButton 131 4.4.5优势和缺陷 133 4.4.6应用情景 134 4.5FacadePattern(外观模式) 134 4.5.1定义 134 4.5.2...

    深入浅出设计模式(中文版电子版)

    4.4.2现实中的装饰模式——相架 126 4.4.3C#实例——图书馆中的项目 127 4.4.4Java实例——自定义JButton 131 4.4.5优势和缺陷 133 4.4.6应用情景 134 4.5FacadePattern(外观模式) 134 4.5.1定义 134 4.5.2...

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

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

    设计模式精解 译者:熊节 程序员必看书籍之一 part2

    设计模式精解(Design Patterns Explained) ...如何实现关键模式——Strategy(策略)、Observer(观察者)、Bridge(桥接)、Decorator(装饰)等等。 共同点/变化点分析、设计模式以及它们如何帮助理解抽象类。

    设计模式精解-掌握设计模式

    本书从一个新的视角描述面向对象设计,将...\r\n 如何实现关键模式——Strategy(策略)、Observer(观察者)、Bridge(桥接)、Decorator(装饰)等等。\r\n 共同点/变化点分析、设计模式以及它们如何帮助理解抽象类。

    设计模式精解 译者:熊节 程序员必看书籍之一 part1

    设计模式精解(Design Patterns Explained) ...如何实现关键模式——Strategy(策略)、Observer(观察者)、Bridge(桥接)、Decorator(装饰)等等。 共同点/变化点分析、设计模式以及它们如何帮助理解抽象类。

    java高手真经 (UML建模+设计模式+面向服务架构) 卷6

    pattern/src/creation/builder //11.4建造者模式 pattern/src/creation/prototype //11.5原型模式 pattern/src/structure/adapter //12.1适配器模式 pattern/src/structure/decorator //12.2装饰器模式 pattern/src/...

Global site tag (gtag.js) - Google Analytics