码迷,mamicode.com
首页 > 编程语言 > 详细

【JAVA】 代理Proxy的实现

时间:2014-12-26 11:14:41      阅读:448      评论:0      收藏:0      [点我收藏+]

标签:java   代理   proxy   

假设没有中间代理层:

接口

/**
 * 
 */
  
package com.tree.demo.proxy;  
/**  
 * ClassName:ITalkInterface <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2014年12月25日 下午3:41:56 <br/>  
 * @author   chiwei  
 * @version    
 * @since    JDK 1.6  
 * @see        
 */
public interface ITalkInterface {

	public String talk(String msg,String who);
	
}
  
实现类

package com.tree.demo.proxy;

/**
 * ClassName:Student <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2014年12月25日 下午3:46:19 <br/>
 * 
 * @author chiwei
 * @version
 * @since JDK 1.6
 * @see
 */
public class Student implements ITalkInterface {

	private String school;

	private String grade;

	private int age;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Student() {

	}

	public Student(String school, String grade, int age) {
		super();
		this.school = school;
		this.grade = grade;
		this.age = age;
	}

	@Override
	public String talk(String msg, String who) {

		// TODO Auto-generated method stub
		return "Hi " + who + ",I am a Student" + ",I'm in School "
				+ this.getSchool() + ",grade " + this.getGrade() + ","
				+ this.getAge() + " years old ! I want to say " + msg;
	}

}
实现类

package com.tree.demo.proxy;

/**
 * ClassName:Worker <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2014年12月25日 下午3:51:16 <br/>
 * 
 * @author chiwei
 * @version
 * @since JDK 1.6
 * @see
 */
public class Worker implements ITalkInterface {

	private String factory;

	private double salary;

	public String getFactory() {
		return factory;
	}

	public void setFactory(String factory) {
		this.factory = factory;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public Worker() {

	}

	public Worker(String factory, double salary) {
		super();
		this.factory = factory;
		this.salary = salary;
	}

	@Override
	public String talk(String msg, String who) {

		// TODO Auto-generated method stub
		return "Worker: " + who + " say " + msg + ",factory:"
				+ this.getFactory() + ",salary:" + this.getSalary();
	}

}
从以上代码,我们看到学生和工人都实现了talk接口,也都重写了额alk方法
这是一个基本的工厂模式!!!!详情请见: http://blog.csdn.net/simonchi/article/details/11921409


这时候如果说学生或者工人都想唱歌怎么办呢?

难道talk接口添加唱歌方法,然后学生和工人实现类都实现唱歌方法???而且还是必须得实现唱歌方法,不唱都不行啊!!!这是不是很坑爹呢?????


这时候我们就需要一个中间代理层来解决这个问题了!!!

代理类

 
package com.tree.demo.proxy;  
/**  
 * ClassName:TalkProxy <br/>  
 * Function: TODO ADD FUNCTION. <br/>  
 * Reason:   TODO ADD REASON. <br/>  
 * Date:     2014年12月25日 下午3:55:31 <br/>  
 * @author   chiwei  
 * @version    
 * @since    JDK 1.6  
 * @see        
 */
public class TalkProxy implements ITalkInterface {

	ITalkInterface talk;
	
	public TalkProxy(ITalkInterface talk) {
		System.out.println(talk.getClass().getName());
		this.talk = talk;
	}
	
	@Override
	public String talk(String msg, String who) {

		// TODO Auto-generated method stub  
		return talk.talk(msg, who);
	}
	
	public String sing(String song) {
		return "I am singing a song named " + song;
	}

}
  
注意此代理类的结构,实现了接口,并且通过构造方法去初始化内部的接口变量,而且在代理类中实现了唱歌!!!

public static void main(String[] args) {

		// TODO Auto-generated method stub  
		Student s1 = new Student("TsingHua","1",22);
		System.out.println(s1.talk("s1", "Jim"));
		Worker w1 = new Worker("China Mobile", 2000.0);
		System.out.println(w1.talk("w1", "Tom"));
		
		TalkProxy tp = new TalkProxy(s1);
		System.out.println(tp.sing("我的中国梦"));
		
		TalkProxy tp2 = new TalkProxy(w1);
		System.out.println(tp2.sing("咱们老百姓"));
		
		
	}

这时候可以看到当学生想唱歌的时候,通过代理类来实现,将学生类对象传给代理类的构造方法即可!!这样当谁想唱歌,谁就代理下就行了!!


这里的代理类的作用也就是代理了一个唱歌的方法,想唱歌就不用再去修改接口,修改实现类了,很简单,很灵活!

如果你还想骂人,就在代理类里添加一个骂人的方法,谁想骂人就传谁进去就行了!!!







































【JAVA】 代理Proxy的实现

标签:java   代理   proxy   

原文地址:http://blog.csdn.net/simonchi/article/details/42167923

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!