1.Proxy模式
代理模式支持将某些操作从实际的对象中分离出来,通过它的代理类提供处理。这样便于修改和管理这些特定的操作。
下面示例一个代理模式的实现。
<<interface>>Subject.java
package com.zj.proxy;
public interface Subject {
void operation1();
void operation2(String arg);
}
现实类RealSubject.java
package com.zj.proxy;
public class RealSubject implements Subject {
public void operation1() {
System.out.println("Realer do operation1");
}
public void operation2(String arg) {
System.out.println("Realer do operation2 with " + arg);
}
}
代理类ProxySubject.java
package com.zj.proxy;
public class ProxySubject implements Subject {
private Subject proxied;// 被代理对象
public ProxySubject(Subject proxied) {
this.proxied = proxied;
}
public void operation1() {
System.out.println("Proxyer do operation1");
proxied.operation1();
}
public void operation2(String arg) {
System.out.println("Proxyer do operation2 with " + arg);
proxied.operation2(arg);
}
}