package design.agencyAndAdornment.demo;
public class Main {
public static void main (String[] args) {
System.out .println("代理模式" );
HandleAgency h1 = new HandleAgency();
h1.handle();
System.out .println("" );
System.out .println("装饰者模式" );
HandleAdornment h2 = new HandleAdornment(new HandleB());
h2.handle();
}
}
package design.agencyAndAdornment.demo;
public interface Handle {
public void handle ();
}
package design.agencyAndAdornment.demo;
public class HandleA implements Handle {
public HandleA () {
System.out.println("A处理类启动中" );
try {
Thread.sleep(1000 );
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("A处理类启动完成" );
}
@Override
public void handle () {
System.out.println("A处理类处理进行处理" );
}
}
package design.agencyAndAdornment.demo;
public class HandleB implements Handle {
public HandleB () {
System.out.println("B处理类启动中" );
try {
Thread.sleep(1000 );
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("B处理类启动完成" );
}
@Override
public void handle () {
System.out.println("B处理类处理进行处理" );
}
}
package design.agencyAndAdornment.demo;
public class HandleAgency {
private HandleA handle = null ;
public void handle () {
System.out .println("代理处理类进行专长处理" );
if (handle == null ) {
handle = new HandleA();
}
handle.handle();
}
}
package design.agencyAndAdornment.demo;
public class HandleAdornment {
private Handle handle = null ;
public HandleAdornment (Handle handle) {
this .handle = handle;
}
public void handle () {
handle.handle();
}
}