02.public class Proxy implements Subject{
03.
04. private Subject subject;
05. public Proxy(){
06. //关系在编译时确定
07. subject = new RealSubject();
08. }
09. public void doAction(){
10. ….
11. subject.doAction();
12. ….
13. }
14.}
01.//代理的客户
02.public class Client{
03. public static void main(String[] args){
04. //客户不知道代理委托了另一个对象
05. Subject subject = new Proxy();
06. …
07. }
08.}
01.//装饰器模式
02.public class Decorator implements Component{
03. private Component component;
04. public Decorator(Component component){
05. this.component = component
06. }
07. public void operation(){
08. ….
09. component.operation();
10. ….
11. }
12.}
01.//装饰器的客户
02.public class Client{
03. public static void main(String[] args){
04. //客户指定了装饰者需要装饰的是哪一个类
05. Component component = new Decorator(new ConcreteComponent());
06. …
07. }
08.} |