`
raymond.chen
  • 浏览: 1433495 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

装饰模式(Decorator Pattern)

 
阅读更多

动态给一个对象添加一些额外的职责。使用Decorator模式相比用生成子类方式达到功能的扩充显得更为灵活。

 

通过采用组合、而非继承,Decorator模式实现了在运行时动态的扩展对象功能的能力,而且可以根据需要扩展多个功能。避免了单独使用继承带来的灵活性差和多子类衍生问题。

 

Component类在Decorator模式中充当抽象接口的角色,不应该去实现具体的行为。而且Decorator类对于Component类应该透明——换言之Component类无需知道Decorator类,Decorator类是从外部来扩展Component类的功能。

 

Decorator类在接口上表现为is-a Component的继承关系,即Decorator类继承了Component类所具有的接口。但在实现上又表现为has-a Component的组合关系,即Decorator类又使用了另外一个Component类。我们可以使用一个或多个Decorator对象来装饰一个Component对象,且装饰后的对象仍然是一个Component对象。

  

Component类源码:

public interface Component {
	public void operation();
}

 

ConcreteComponent类源码:

public class ConcreteComponent implements Component {
	public void operation(){
		System.out.println("ConcreteComponent");
	}
}

 

Decorator类源码:

public abstract class Decorator implements Component {
	private Component component;

	public Decorator(Component component){
		this.component = component;
	}

	public void operation(){
		component.operation();
	}
}

 

ConcreteDecoratorA类源码:

public class ConcreteDecoratorA extends Decorator {
	public ConcreteDecoratorA(Component component){
		super(component);
	}

	public void operation(){
		super.operation();
		this.newMethod();
	}

	public void newMethod(){
		System.out.println("ConcreteDecoratorA: new method");
	}
}

 

ConcreteDecoratorB类源码:

public class ConcreteDecoratorB extends Decorator {
	public ConcreteDecoratorB(Component component){
		super(component);
	}
	
	public void operation(){
		super.operation();
		this.newMethod();
	}
	
	public void newMethod(){
		System.out.println("ConcreteDecoratorB: new method");
	}
}

 

Client类源码:

public class Client {
	public static void main(String[] args) {
		Component comp = new ConcreteComponent();
		
		ConcreteDecoratorA da = new ConcreteDecoratorA(comp);
		da.operation();
		
		ConcreteDecoratorB db = new ConcreteDecoratorB(comp);
		db.operation();
	}
}

 

 

  • 大小: 44.3 KB
0
0
分享到:
评论

相关推荐

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

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

    Java24种设计模式,Java24种设计模式,24种设计模式,学会了这24种设计模式,可以打遍天下无敌手,设计模式非常重要

    13、装饰模式DECORATOR PATTERN 14、迭代器模式ITERATOR PATTERN 15、组合模式COMPOSITE PATTERN 16、观察者模式OBSERVER PATTERN 17、责任链模式 18、访问者模式VISITOR PATTERN 19、状态模式 20、原型模式 21...

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

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

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

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

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

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

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

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

    DecoratorPattern.rar

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

    03_DecoratorPattern 小菜扮靓

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

    java Decorator装饰模式例子

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

    DecoratorPattern.zip

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

    PHP设计模式(八)装饰器模式Decorator实例详解【结构型】

    装饰器模式(Decorator Pattern)是一种结构型设计模式,主要用于在运行时动态地给对象添加新的职责或行为,而不必改变现有对象的类定义。在面向对象编程中,装饰器模式提供了一种相对于继承更加灵活的方式来增强或...

    装饰器(Decorator)模式

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

    java 装饰模式(Decorator Pattern)详解

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

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

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

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

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

    Decorator pattern

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

    Decorator Pattern

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

    详解java装饰模式(Decorator Pattern)

    【Java装饰模式(Decorator Pattern)】装饰模式是一种结构型设计模式,它允许在不修改已有对象的基础上,通过添加额外的功能来扩展对象的行为。这种模式的关键在于装饰类与被装饰类有相同的接口,使得它们可以互换...

Global site tag (gtag.js) - Google Analytics