此文章问转载
decorator 模式的功能是:给一个对象添加一些额外的职责(操作),虽然此功能可以用继承实现,但装饰模式比生成子类更灵活些。
装饰的意思:就是包装一下。把另的对象包装一下。我这里只简单示例下怎么使用。
业务接口 Component:
package com.chenlb.dp.decorator;
/**
* 业务接口
*/
public interface Component {
void operation();
}
具体业务 ConcreteComponent:
package com.chenlb.dp.decorator;
/**
* 具体业务类.
*/
public class ConcreteComponent implements Component {
public void operation() {
System.out.println("I'm "+this.getClass().getName());
}
}
装饰 Decorator:
package com.chenlb.dp.decorator;
/**
* 装饰类.
*/
public class Decorator implements Component {
private Component component;
public Decorator(Component component) {
this.component = component;
}
public void operation() {
component.operation();
}
}
执行前装饰 BeforeDecorator:
package com.chenlb.dp.decorator;
/**
* 在业务执行前加点额外的操作.
*/
public class BeforeDecorator extends Decorator {
public BeforeDecorator(Component component) {
super(component);
}
public void operation() {
before();
super.operation();
}
private void before() {
System.out.println("before: I'm "+this.getClass().getName());
}
}
执行后装饰 AfterDecorator:
package com.chenlb.dp.decorator;
/**
* 在业务执行完后添加额外的操作.
*/
public class AfterDecorator extends Decorator {
public AfterDecorator(Component component) {
super(component);
}
public void operation() {
super.operation();
after();
}
private void after() {
System.out.println("after: I'm "+this.getClass().getName());
}
}
运行示例 Demo:
package com.chenlb.dp.decorator;
public class Demo {
public static void main(String[] args) {
Component component = new ConcreteComponent();
component.operation();
System.out.println("----------------------------------------");
component = new BeforeDecorator(new ConcreteComponent());
component.operation();
System.out.println("----------------------------------------");
component = new AfterDecorator(new ConcreteComponent());
component.operation();
System.out.println("----------------------------------------");
component = new AfterDecorator(new BeforeDecorator(new ConcreteComponent()));
component.operation();
System.out.println("----------------------------------------");
component = new BeforeDecorator(new AfterDecorator(new ConcreteComponent()));
component.operation();
}
}
分享到:
相关推荐
JAVA-设计模式-结构型模式-装饰模式
设计模式--装饰者模式java例子
计算机后端-Java-图解java设计模式072 装饰者模式(2)-.avi
计算机后端-Java-图解java设计模式074 装饰者模式(4)-.avi
计算机后端-Java-图解java设计模式071 装饰者模式(1)-.avi
装饰工程-装饰工程系统-装饰工程系统源码-装饰工程管理系统-装饰工程管理系统java代码-装饰工程系统设计与实现-基于springboot的装饰工程系统-基于Web的装饰工程系统设计与实现-装饰工程网站-装饰工程网站代码-装饰...
装饰工程-装饰工程系统-装饰工程系统源码-装饰工程管理系统-装饰工程管理系统java代码-装饰工程系统设计与实现-基于springboot的装饰工程系统-基于Web的装饰工程系统设计与实现-装饰工程网站-装饰工程网站代码-装饰...
计算机后端-Java-图解java设计模式073 装饰者模式(3).avi
计算机后端-Java-图解java设计模式075 装饰者模式(5).avi
计算机后端-Java-图解java设计模式076 装饰者模式(6).avi
本章可以称为“给爱用继承的人一个全新的设计眼界”。我们即将再度探讨典型滥用问题。你将在本章学到如何使用对象组合的方式,做到在运行时装饰类。为什么呢?一旦你熟悉了装饰者的技巧...——《Head First 设计模式》
装饰工程管理系统-装饰工程管理系统的设计与实现代码-java-springboot-基于springboot的装饰工程管理系统项目-代码-源码-项目-系统-毕设-网站 1、技术栈:java,springboot,vue,ajax,maven,mysql,MyBatisPlus等 ...
java常用设计模式-装饰器模式
JAVA设计模式例程-装饰模式-的例程,看完以后基本上能对JAVA上实现装饰模式能有所了解,个人认为常各种模式并不是为了死套各种模式,而应该是灵活的在项目中结合实际情况而用各种模式的组合应用,呵呵!但前提是得对...
23个设计模式之一的装饰模式,java编写的代码示例,极客学院课程的设计模式下载的课件资料,附上Head First设计模式第三版的pdf,另外还有UML基础,案例与应用的pdf
装饰工程管理系统的设计与实现代码-java-springboot-基于springboot的装饰工程管理系统项目-代码-源码-项目-系统-毕设-网站 1、技术栈:java,springboot,vue,ajax,maven,mysql,MyBatisPlus等 2、系统的实现 ...