代理模式

代理模式是一种很巧妙的设计方式,通过设置代理可以达到不暴露实际的动作类,代理模式主要使用了Java的多态,干活的是被代理类,代理类是显式调用的一方下面来看代码:首先创建一个Action的接口

package com.wzl.proxy;

/**
 * @author wuzhilang
 * @Title: Action
 * @ProjectName design_pattern
 * @Description:
 * @date 2019/1/3115:11
 */
public interface Action {
	public void eat();
	public void swim();
}

我在这个类里面创建了两个方法,eat和swim,下面是实现这个接口的人

package com.wzl.proxy;

/**
 * @author wuzhilang
 * @Title: Me
 * @ProjectName design_pattern
 * @Description: TODO
 * @date 2019/1/3115:23
 */
public class Me implements Action {
	@Override
	public void eat() {
		System.out.println("我吃肉");
	}

	@Override
	public void swim() {
		System.out.println("我游泳");
	}
}
package com.wzl.proxy;

/**
 * @author wuzhilang
 * @Title: You
 * @ProjectName design_pattern
 * @Description: 代理模式的核心
 * @date 2019/1/3115:13
 */
public class You implements Action {
	private Action kindWomen;
	public You(){
		this.kindWomen = new Me();
	}
	public You(Action kindWomen){
		this.kindWomen=kindWomen;
	}
	@Override
	public void eat() {
		this.kindWomen.eat();
	}

	@Override
	public void swim() {
		this.kindWomen.swim();

	}
}

这里Me完成的是eat()和swim(),You在这个里面的作用主要是关联Me的动作,You代理了我的存在最后就是测试类

package com.wzl.proxy;

/**
 * @author wuzhilang
 * @Title: ProxyTest
 * @ProjectName design_pattern
 * @Description: TODO
 * @date 2019/1/3117:14
 */
public class ProxyTest
{
	public static void main(String[] args) {
		You you = new You();
		you.eat();
		you.swim();
	}
}

结果的输出是:

"C:\Program Files\Java\jdk1.8.0_111\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\lib\idea_rt.jar=51884:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar;D:\360Downloads\production\design_pattern;D:\Program Files\Maven\Repositories\org\junit\jupiter\junit-jupiter-api\5.4.0-RC1\junit-jupiter-api-5.4.0-RC1.jar;D:\Program Files\Maven\Repositories\org\apiguardian\apiguardian-api\1.0.0\apiguardian-api-1.0.0.jar;D:\Program Files\Maven\Repositories\org\opentest4j\opentest4j\1.1.1\opentest4j-1.1.1.jar;D:\Program Files\Maven\Repositories\org\junit\platform\junit-platform-commons\1.4.0-RC1\junit-platform-commons-1.4.0-RC1.jar" com.wzl.proxy.ProxyTest
我吃肉
我游泳

Process finished with exit code 0

通过最后的结果,可以看出,我们虽然是You在调用接口,最后的Action的动作实体确是Me。

01-31 22:06