设计模式之--装饰器模式的硬编码的静态装饰和链化
来源:互联网 发布:ubuntu16 caffe cuda 编辑:程序博客网 时间:2024/05/22 15:32
首先有一个简单业务接口:
public interface IMyBusinessObject {
public String doExecute(String in);
}
public String doExecute(String in);
}
这个接口有一个业务对象类的实现。
public class MyBusinessObject implements IMyBusinessObject {
public String doExecute(String in) {
System.out.println("Here in MyBusinessObject doExecute: input :" + in);
return in;
}
}
public String doExecute(String in) {
System.out.println("Here in MyBusinessObject doExecute: input :" + in);
return in;
}
}
public abstract class ADecorator implements IMyBusinessObject {
protected IMyBusinessObject target;
public void setTarget(IMyBusinessObject target) {
this.target = target;
}
public ADecorator(){};
public ADecorator(IMyBusinessObject target_) {
setTarget(target_);
}
}
protected IMyBusinessObject target;
public void setTarget(IMyBusinessObject target) {
this.target = target;
}
public ADecorator(){};
public ADecorator(IMyBusinessObject target_) {
setTarget(target_);
}
}
public class DebugConcreteDecorator extends ADecorator {
public DebugConcreteDecorator(IMyBusinessObject target_) {
super(target_);
}
public String doExecute(String in) {
System.out
.println("DebugConcreteDecorator: before method : doExecute ");
String ret = target.doExecute(in);
System.out.println("DebugConcreteDecorator: after method : doExecute ");
return ret;
}
}
public DebugConcreteDecorator(IMyBusinessObject target_) {
super(target_);
}
public String doExecute(String in) {
System.out
.println("DebugConcreteDecorator: before method : doExecute ");
String ret = target.doExecute(in);
System.out.println("DebugConcreteDecorator: after method : doExecute ");
return ret;
}
}
现在在客户端,我们调用业务对象:
IMyBusinessObject aIMyBusinessObject = new MyBusinessObject();
IMyBusinessObject wrappedObject = new DebugConcreteDecorator(
aIMyBusinessObject);
wrappedObject.doExecute("Hello World");
IMyBusinessObject wrappedObject = new DebugConcreteDecorator(
aIMyBusinessObject);
wrappedObject.doExecute("Hello World");
DebugConcreteDecorator: before method : doExecute
Here in MyBusinessObject doExecute: input :Hello World
DebugConcreteDecorator: after method : doExecute
Here in MyBusinessObject doExecute: input :Hello World
DebugConcreteDecorator: after method : doExecute
public class AnotherConcreteDecorator extends ADecorator {
public AnotherConcreteDecorator(
IMyBusinessObject target_) {
super(target_);
}
public String doExecute(String in) {
System.out
.println("AnotherConcreteDecorator: Going to execute method : doExecute");
in = in + " Modified by AnotherConcreteDecorator";
String ret = target.doExecute(in);
System.out
.println("AnotherConcreteDecorator: After execute method : doExecute");
return ret;
}
}
public AnotherConcreteDecorator(
IMyBusinessObject target_) {
super(target_);
}
public String doExecute(String in) {
System.out
.println("AnotherConcreteDecorator: Going to execute method : doExecute");
in = in + " Modified by AnotherConcreteDecorator";
String ret = target.doExecute(in);
System.out
.println("AnotherConcreteDecorator: After execute method : doExecute");
return ret;
}
}
上面的代码片段通过在业务方法输入的字符参数后增加(" Modified by AnotherConcreteDecorator")字符串,从而实现对其进行了修改。如果我们想链化装饰器,在客户端,我们可以编写如下的代码:
IMyBusinessObject aIMyBusinessObject = new MyBusinessObject();
IMyBusinessObject wrappedObject = new AnotherConcreteDecorator(
new DebugConcreteDecorator(aIMyBusinessObject));
wrappedObject.doExecute("Hello World");
IMyBusinessObject wrappedObject = new AnotherConcreteDecorator(
new DebugConcreteDecorator(aIMyBusinessObject));
wrappedObject.doExecute("Hello World");
在上面的代码片段中,我在创建一个DebugConcreteDecorator实例时,向其传递了一个实际的业务对象实例。然后用一个刚才定义的notherConcreteDecorator实例去包装DebugConcreteDecorator实例。AntherConcreteDecorator首先在输入参数后增加字符串对其进行修改,然后调用DebugConcreteDecorator实例的doExecute()方法。这时,DebugConcreteDecorator会记录doExectute()方法的输出的条目,然后调用doExecute()去调用实际业务对象的doExecute()方法。
它的返回路径以相反的顺序。在从实际的业务对象(MyBusinessObject)doExecute()方法返回之后,DebugConcreteDecorator余下代码将被执行。于是,调用返回至AnotherConcreteDecorator实例并执行余下的部份代码。
输出结果如下:
AnotherConcreteDecorator: Going to execute method : doExecute
DebugConcreteDecorator: before method : doExecute
Here in MyBusinessObject doExecute: input :Hello World Modified by AnotherConcreteDecorator
DebugConcreteDecorator: after method : doExecute
AnotherConcreteDecorator: After execute method : doExecute
DebugConcreteDecorator: before method : doExecute
Here in MyBusinessObject doExecute: input :Hello World Modified by AnotherConcreteDecorator
DebugConcreteDecorator: after method : doExecute
AnotherConcreteDecorator: After execute method : doExecute
- 设计模式之--装饰器模式的硬编码的静态装饰和链化
- 设计模式之装饰器模式的学习思考
- 装饰模式设计-装饰类和继承类的区别
- 设计模式学习之装饰模式:IO流的装饰器
- java之装饰设计模式和继承的简单区别
- java之装饰设计模式和继承的简单区别
- java 之 装饰设计模式 和继承的优缺点
- 装饰设计模式和继承的区别
- PHP设计模式-装饰器模式
- PHP设计模式-装饰器模式
- 设计模式之装饰器
- 设计模式之装饰器
- 设计模式之装饰器
- 学会的模式-设计模式之Decorator装饰模式
- 设计模式之--装饰模式与代理模式的区别
- 设计模式--代理模式--以及和装饰模式的区别
- 设计模式之装饰
- 设计模式之装饰
- 马丽娟-赵本山夫人
- 大话西游灯谜答案
- 怎么解决VS2008 对已引用的脚本禁用了Jscript IntelliSense 的问题
- [Web开发] 微软的RSS协议扩展 - FeedSync 介绍 (1)
- 如何摆脱配置管理的尴尬局面
- 设计模式之--装饰器模式的硬编码的静态装饰和链化
- [Web开发] 微软的RSS协议扩展 - FeedSync 介绍 (2)
- 优秀思维方式——每日一语(2008-02-15)
- oracle学习第一课 基本command
- SQL基本语句
- NickLee.FortuneBase(2008.2)
- 农历08年的第一天班
- 永远别向复杂低头
- java中四种操作xml方式的比较浅谈