Difference between decorator and proxy patterns
来源:互联网 发布:显卡性能测试软件 编辑:程序博客网 时间:2024/06/07 17:35
I had an interesting discussion with a colleague of mine regarding the differences between the proxy and the decorator pattern which made me give it some thought. To the untrained eye (that includes my eye!), they seem exactly the same. Infact you sometimes wonder, why the heck are there two names for the same pattern. As we’ll soon see, they are anything but the same.
I think we’re all aware that the decorator pattern is used to add behavior to existing code. This does not stop at one kind of behavior but any number of different things that we want to do. A proxy on the other hand simply delegates all calls to the underlying object delaying costly operations till they are absolutely neccessary. This basically means that what a proxy can do is decided at compile time and cannot be changed by the calling code after instantiation. Using the decorator pattern the behavior of the underlying object can be changed at runtime by adding multiple decorators to it. This behavior addition takes place at runtime depending on say user input. To put it simply, proxy is compile time, decorator is runtime.
Another very important distinction is that decorators never ever instantiate their surrogate objects. Decorators are always passed their surrogates in their constructors and perform actions on these surrogates.
Proixes on the other hand always obtain an instance of their surrogates (either via constructors or factories). Proxies therefore never really expose their underlying objects but decorators do. For example, consider a badly written legacy file loader class which reads the entire contents of a file. File loader objects are frequently created but rarely used leading to massive memory problem. To resolve this we can use a proxy.
class
FileLoader
implements
Loader {
private
File file;
public
FileLoader(File file) {
this
.file = file;
readContents(file);
}
public
long
size() {
file.length();
}
public
byte
[] contents() {
// return contents
}
}
class
FileLoaderProxy
implements
Loader {
private
File file;
private
FileLoader loader =
null
;
public
FileLoaderProxy(File file) {
this
.file = file;
readContents(file);
}
public
long
size() {
file.length();
}
public
byte
[] contents() {
if
(loader ==
null
)
loader =
new
FileLoader(file);
return
loader.contents();
}
}
- Difference between decorator and proxy patterns
- The difference between Proxy and CGLIB
- The difference between Proxy and CGLIB
- Difference between proxy server and reverse proxy server
- What is the difference between Facade and Gateway design patterns?
- The difference between (()) and ().
- difference between "<" and "<<"
- What is the Difference Between Proxy Types?
- Difference Between typedef and #define
- Difference between PCDATA and CDATA
- Difference between CComPtr and CComQIPtr
- Difference between RVCT and GCCE
- Difference between RVCT and GCCE
- difference between slist and list.
- difference between stack and heap
- Difference between replace and translate
- Difference between PrintWriter and PrintStream
- difference between Cookie and Session
- 3gp文件格式研究
- [移动互联网]未来三年的移动互联网创业
- spring 2
- 推荐6款常用的Java开源报表制作工具
- IE7、FF DIV高度不能自适应问题的解决方案
- Difference between decorator and proxy patterns
- 经典SQL语句大全 提升
- WebLogic Server 10.3.2.0 SSL 配置及 SSL 协议传输的 WebSevice 调用
- 不同平台下 sleep区别用法
- 大数据: 发展还是变革?
- Python之强大的日志模块
- 评“马无夜草不肥:程序员做业余项目的重要性”
- vmnet1 and vmnet8
- WPF编程学习——样式(1)