`

Decorator Pattern(装饰模式)

阅读更多

定义:  动态的给一个对象增加其他职责(Responsibility),就增加对象功能来说,装饰模式比生成子类实现更灵活。

 

Sitemesh框架就是应用了装饰模式,Sithmesh是一个非常优秀的页面修饰框架,使用它可以动态的给页面加一些装饰,可以完全的代替所有的include指令。

 

装饰模式中类和对象的关系为:

  1. Component抽象构件:定义对象的接口,可以给这些对象动态增加职责(方法)

  2. ConcreteComponent 具体构件:定义具体的对象,Decorator可以给它增加额外的职责(方法)

  3. Decorator 装饰角色: 持有具体构件类的对象,以便执行原有功能

  4. ConcreteDecorator具体装饰: 具体扩展的功能在这里

 

应用情景:

  1. 你想透明的并且动态的给对象增加心的职责(方法),而有不会影响其他的对象

  2. 你给对象增加的职责在未来会放生变化

 

下面就是一个简单的装饰模式的例子:以吃饭为例,现在要增加一些功能,在吃饭前加听音乐,吃饭后加唱歌(当然这也可以用动态代理来实现)

 

抽象构件

package com.lbx.component;

/*
 * 抽象构件
 */

public interface Component {
	
	public void eat();
	
}

 

具体构件

package com.lbx.conceteComponent;

import com.lbx.component.Component;

/**
 * 具体构件
 * @author Administrator
 *
 */

public class ConcreteComponent implements Component {

	public void eat() {
		System.out.println("吃饭");
	}

}

 

 

装饰角色

package com.lbx.decorator;

import com.lbx.component.Component;

/**
 * 装饰角色
 * @author Administrator
 *
 */

public class Decorator implements Component {

	private Component component;

	protected Decorator(Component component) {

		this.component = component;

	}

	@Override
	public void eat() {
		// TODO Auto-generated method stub
		this.component.eat();
	}

}

 

 

具体装饰1

package com.lbx.concreteDecorator;

import com.lbx.component.Component;
import com.lbx.decorator.Decorator;

/**
 * 具体装饰(这里演示了两种扩展的情况,听音乐+吃饭和吃饭+唱歌)
 * @author Administrator
 *
 */

public class ConcreteDecoratorListen extends Decorator {

	public ConcreteDecoratorListen(Component component) {
		super(component);
	}
	
	public void eat(){
		this.listen("听音乐");    //执行增加的功能
		super.eat();
	}
	
	private void listen(Object obj){
		System.out.println(obj);
	}

}

 

 

 

具体装饰2

package com.lbx.concreteDecorator;

import com.lbx.component.Component;
import com.lbx.decorator.Decorator;

public class ConcreteDecoratorSing extends Decorator {

	public ConcreteDecoratorSing(Component component) {
		super(component);
		// TODO Auto-generated constructor stub
	}

	public void eat() {

		super.eat();

		System.out.println(sing());   // 执行扩展功能

	}

	private String sing() {

		return "唱歌";

	}

}

 

 

测试方法 

package com.lbx.test;

import com.lbx.component.Component;
import com.lbx.conceteComponent.ConcreteComponent;
import com.lbx.concreteDecorator.ConcreteDecoratorListen;
import com.lbx.concreteDecorator.ConcreteDecoratorSing;

public class Test {
	
	public static void main(String[] args) {
		Component component = new ConcreteComponent();
		
		ConcreteDecoratorListen c = new ConcreteDecoratorListen(component);
		c.eat();
		
		ConcreteDecoratorSing c2 = new ConcreteDecoratorSing(component);
		c2.eat();
		
	}
	
}

 

 

 

 

 

分享到:
评论

相关推荐

    设计模式之装饰模式(Decorator Pattern)

    装饰模式(Decorator Pattern)是一种结构型设计模式,它在不改变原有对象的基础上,通过包裹一个对象并为其添加新的行为或责任,实现对对象功能的扩展。这种模式在软件开发中非常常见,尤其当需要在运行时动态改变...

    装饰器模式(Decorator Pattern) 1. 装饰器模式简介 1.1 问题引入 1.2 装饰器模式的解决方案 2. 装饰器模式的定义和原理 2.1 定义 2.2 基本原理 2.3 装饰器模式

    装饰器模式(Decorator Pattern) 1. 装饰器模式简介 1.1 问题引入 1.2 装饰器模式的解决方案 2. 装饰器模式的定义和原理 2.1 定义 2.2 基本原理 2.3 装饰器模式的特点 3. 装饰器模式的UML类图和基本结构 3.1 UML...

    通过C#实现设计模式-装饰模式(DecoratorPattern).rar

    装饰模式(Decorator Pattern)是一种结构型设计模式,它允许你向一个现有的对象添加新的功能,同时又不改变其结构。装饰模式通过创建一个装饰类,该类包装了原始类的实例,并在调用原始类方法之前或之后添加额外的...

    装饰者模式(Decorator Pattern)原理图

    装饰者模式(Decorator Pattern)是一种结构型设计模式,它的定义是在不改变原有对象结构的基础上,动态地给该对象增加一些职责(即增加其额外功能)。这种模式允许向一个现有的对象添加新的功能,同时又不改变其...

    C#设计模式之Decorator 装饰模式

    装饰模式(Decorator Pattern)是设计模式中的一种结构型模式,它在不改变原有对象的基础上,通过添加额外的职责来扩展对象的功能。在C#中,装饰模式尤其适用于那些需要动态地增加或减少对象功能的情况,避免了使用...

    DecoratorPattern.rar

    在"DecoratorPattern.rar"这个压缩包中,包含了装饰器模式的简单案例demo和一个使用MindMaster绘制的脑图。通过这个案例,我们可以深入理解装饰器模式的工作原理和应用场景。 1. 装饰器模式的基本结构: - ...

    Head First 设计模式 (三) 装饰者模式(decorator pattern) C++实现

    装饰者模式(Decorator Pattern)是一种结构型设计模式,它允许我们向对象添加新的行为或职责,而无需修改对象的原始代码。在C++中实现装饰者模式,可以让我们灵活地扩展对象的功能,同时保持代码的可读性和可维护性...

    java Decorator装饰模式例子

    装饰模式(Decorator Pattern)是设计模式中的一种结构型模式,它允许在运行时给对象添加新的行为或职责,而无需改变对象的类。在Java中,装饰模式通常通过继承和组合来实现,使得代码具有更好的扩展性和灵活性。...

    03_DecoratorPattern 小菜扮靓

    在"03_DecoratorPattern 小菜扮靓"这个主题中,我们可以推断作者可能以轻松幽默的方式介绍了装饰器模式的概念。"小菜"可能是用来比喻待装饰的对象,"扮靓"则表示通过装饰增强其功能或特性。博文链接指向了iteye博客...

    c++-设计模式之装饰模式(Decorator)

    装饰模式(Decorator Pattern)是一种结构型设计模式,允许在不改变对象接口的情况下,动态地为对象添加额外的职责或功能。装饰模式通常用于需要扩展对象功能而又不希望使用子类化的场景。 装饰模式的组成 组件接口...

    装饰器(Decorator)模式

    在《Element of Reusable Object-Oriented Software》中,GOF 对装饰器模式的用意进行了概述:Decorator Pattern――Attaches additional responsibilities to an object dynamically. Decorators provide a ...

    DecoratorPattern.zip

    装饰者模式(Decorator Pattern)是软件工程中一种用于动态地给对象添加职责的设计模式。它允许我们独立于对象的类来扩展对象的功能,无需修改原有代码就能增加新功能,遵循“开闭原则”。这种模式是一种结构型设计...

    Decorator pattern

    装饰者模式是一种设计模式,属于结构型模式,它允许在运行时给对象添加新的行为或责任,而无需修改对象的源代码。这种模式的核心在于,它动态地将责任附加到对象上,通过将对象包裹在一个装饰类中来扩展其功能。在...

    Decorator Pattern

    装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许我们向对象添加新的行为或职责,而无需修改对象的源代码。这种模式的核心思想是通过将功能包装在对象的外围来扩展其行为,而不是通过继承来实现。装饰...

    java 装饰模式(Decorator Pattern)详解及实例代码

    装饰模式(Decorator Pattern)是一种结构型设计模式,它可以在不修改原有对象的基础上,通过添加新的职责来扩展对象的功能。装饰模式的核心在于它定义了一个与原类一致的接口,使得装饰类和原类可以互相替换,而...

    java 装饰模式(Decorator Pattern)详解

    装饰模式(Decorator Pattern)是一种结构型设计模式,它在不修改已有对象的代码情况下,通过增加额外的行为来扩展对象的功能。这种模式的核心在于装饰类与被装饰类具有相同的接口,这样客户端可以像处理原始对象...

    [结构型模式] head first 设计模式之装饰者模式(decorator)

    装饰者模式(Decorator Pattern)是结构型设计模式之一,它允许在运行时向对象添加新的行为或职责,而无需修改对象的源代码。这个模式的名字来源于装饰艺术,它通过添加额外的装饰来增强一个物体的外观,同样地,...

Global site tag (gtag.js) - Google Analytics