https://stackoverflow.com/questions/18618779/differences-between-proxy-and-decorator-pattern

https://powerdream5.wordpress.com/2007/11/17/the-differences-between-decorator-pattern-and-proxy-pattern/

Decorator Pattern focuses on dynamically adding functions to an object, while Proxy Pattern focuses on controlling access to an object.

Relationship between a Proxy and the real subject is typically set at compile time, Proxy instantiates it in some way, whereas Decorator is assigned to the subject at runtime, knowing only subject’s interface.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//Proxy Pattern
public class Proxy implements Subject{

private Subject subject;
public Proxy(){
//the relationship is set at compile time
subject = new RealSubject();
}
public void doAction(){
….
subject.doAction();
….
}
}

//client for Proxy
public class Client{
public static void main(String[] args){
//the client doesn’t know the Proxy delegate another object
Subject subject = new Proxy();

}
}
//Decorator Pattern
public class Decorator implements Component{
private Component component;
public Decorator(Component component){
this.component = component
}
public void operation(){
….
component.operation();
….
}
}

//client for Decorator
public class Client{
public static void main(String[] args){
//the client designate which class the decorator decorates
Component component = new Decorator(new ConcreteComponent());

}
}