Proxy

Proxy Pattern:对其他对象提供一种代理以控制对这个对象的访问,它的主要作用是为其他对象提供一种代理以控制对这个对象的访问。在某些情况下,一个对象不想或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用

1)

Subject.java

 

package com.flyingh.isubject;

public interface Subject {
	void fun1();

	void fun2();

	void fun3();
}

 

 RealSubject.java

 

package com.flyingh.subject;

import com.flyingh.isubject.Subject;

public class RealSubject implements Subject {

	@Override
	public void fun1() {
		// TODO Auto-generated method stub
		System.out.println("fun1()");
	}

	@Override
	public void fun2() {
		// TODO Auto-generated method stub
		System.out.println("fun2()");
	}

	@Override
	public void fun3() {
		// TODO Auto-generated method stub
		System.out.println("fun3()");
	}

}

 

 Proxy.java

 

package com.flyingh.subject;

import com.flyingh.isubject.Subject;

public class Proxy implements Subject {
	private Subject realSubject;

	public Proxy(Subject realSubject) {
		super();
		this.realSubject = realSubject;
	}

	public void before() {
		System.out.println("before");
	}

	public void after() {
		System.out.println("after");
	}

	@Override
	public void fun1() {
		// TODO Auto-generated method stub
		before();
		realSubject.fun1();
	}

	@Override
	public void fun2() {
		// TODO Auto-generated method stub
		before();
		realSubject.fun2();
		after();
	}

	@Override
	public void fun3() {
		// TODO Auto-generated method stub
		realSubject.fun3();
		after();
	}

}

 

 Client.java

 

package com.flyingh.client;

import com.flyingh.isubject.Subject;
import com.flyingh.subject.Proxy;
import com.flyingh.subject.RealSubject;

public class Client {
	public static void main(String[] args) {
		Subject proxy = new Proxy(new RealSubject());
		proxy.fun1();
		proxy.fun2();
		proxy.fun3();
	}
}

 

 程序运行结果如下:

 

before
fun1()
before
fun2()
after
fun3()
after

 

 2)Dynamic Proxy:

(1):

Subject.java

 

package com.flyingh.isubject;

public interface Subject {
	void print();
}

 

 RealSubject.java

 

package com.flyingh.subject;

import com.flyingh.isubject.Subject;

public class RealSubject implements Subject {

	@Override
	public void print() {
		// TODO Auto-generated method stub
		System.out.println("hello world!!!");
	}

}

 DynaProxy.java

 

package com.flyingh.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynaProxy implements InvocationHandler {
	private Object obj;

	public DynaProxy(Object obj) {
		super();
		this.obj = obj;
	}

	public Object newProxyInstance() {
		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
				.getClass().getInterfaces(), this);
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		// TODO Auto-generated method stub
		before();
		Object retObj = method.invoke(obj, args);
		after();
		return retObj;
	}

	private void after() {
		// TODO Auto-generated method stub
		System.out.println("after()");
	}

	private void before() {
		// TODO Auto-generated method stub
		System.out.println("before()");
	}

}
Client.java

 

package com.flyingh.client;

import com.flyingh.isubject.Subject;
import com.flyingh.proxy.DynaProxy;
import com.flyingh.subject.RealSubject;

public class Client {
	public static void main(String[] args) {
		Subject proxy = (Subject) new DynaProxy(new RealSubject())
				.newProxyInstance();
		proxy.print();
	}
}


 程序运行结果如下:

 

before()
hello world!!!
after()
(2):
Subject.java
package com.flyingh.subject;

public class Subject {
	public void print() {
		System.out.println("hello world!!!");
	}
}
 CglibProxy.java
package com.flyingh.proxy;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibProxy implements MethodInterceptor {
	private Object obj;

	public CglibProxy(Object obj) {
		super();
		this.obj = obj;
	}

	public Object newProxyInstance() {
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(obj.getClass());
		enhancer.setCallback(this);
		return enhancer.create();
	}

	@Override
	public Object intercept(Object obj, Method method, Object[] args,
			MethodProxy proxy) throws Throwable {
		// TODO Auto-generated method stub
		before();
		Object retObj = proxy.invoke(this.obj, args);
		after();
		return retObj;
	}

	private void after() {
		// TODO Auto-generated method stub
		System.out.println("after()");
	}

	private void before() {
		// TODO Auto-generated method stub
		System.out.println("before()");
	}

}
 Client.java
package com.flyingh.client;

import com.flyingh.proxy.CglibProxy;
import com.flyingh.subject.Subject;

public class Client {
	public static void main(String[] args) {
		Subject proxy = (Subject) new CglibProxy(new Subject())
				.newProxyInstance();
		proxy.print();
	}
}
 程序运行结果如下:
before()
hello world!!!
after()
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值