1、IKeeper.java
package com.ssm.cts.pattern.proxy; public interface IKeeper { public void careForBoy(); }
2、Keeper.java
package com.ssm.cts.pattern.proxy; public class Keeper implements IKeeper { private String name = ""; public Keeper(String _name) { this.name = _name; } public void careForBoy() { System.out.println(this.name + "带娃"); } }
3、KeeperProxy.java
package com.ssm.cts.pattern.proxy; public class KeeperProxy implements IKeeper { private IKeeper keeper = null; public KeeperProxy(IKeeper _keeper) { this.keeper = _keeper; } public void careForBoy() { System.out.println("静态代理前置内容:买菜"); this.keeper.careForBoy(); System.out.println("静态代理后置内容:洗碗"); } }
4、Demo.java
package com.ssm.cts.pattern.proxy; import java.text.SimpleDateFormat; import java.util.Date; public class Demo { // 测试 public static void main(String[] args) { // 定义一个监护人 IKeeper keeper = new Keeper("蒋智昊"); // 然后再定义一个保姆 IKeeper proxy = new KeeperProxy(keeper); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 开始 System.out.println("开始时间是:" + sdf.format(new Date())); // 带娃 proxy.careForBoy(); // 结束 System.out.println("结束时间是:" + sdf.format(new Date()) ); } }