Assume there is no intermediate agent layer:
Interface
/** * * */package com.tree.demo.proxy; /** * classname:italkinterface <br/> * Function:todo ADD Function. <br/> * Reason: TODO ADD REASON. <br/> * Date: December 25, 2014 PM 3:41:56 <br/> * @author chiwei * @version * @ Since JDK 1.6 * @see */public interface Italkinterface {public string talk (String msg,string who);
Implementation Class
Package com.tree.demo.proxy;/** * Classname:student <br/> * Function:todo ADD Function. <br/> * Reason:todo ADD Reason. <br/> * date:2014 December 25 pm 3:46:19 <br/> * * @author Chiwei * @version * @since JDK 1.6 * @see */public Clas S 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.) {this.age = age;} Public Student () {}public Student (string school, string grade, Int. age) {super (); this.school = School;this.grade = Grade;t His.age = age;} @Overridepublic string Talk (String msg, string who) {//TODO auto-generated method Stubreturn ' Hi ' + who + ', I am a Stude NT "+", I ' m in School "+ this.getschool () +", Grade "+ this.getgrade () +", "+ this.getage () +" years old! I WANT to say "+ msg;}}
Implementation Class
package com.tree.demo.proxy;/** * Classname:worker <br/> * Function:todo ADD Function. <br/> * Reason:todo ADD Reason. <br/> * date:2014 December 25 pm 3:51:16 <br/> * * @author Chiwei * @version * @since JDK 1.6 * @see */public Clas S Worker implements Italkinterface {private String factory;private double salary;public String getfactory () {return factor Y;} 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;} @Overridepublic string Talk (String msg, string who) {//TODO auto-generated method Stubreturn ' Worker: ' + who + ' say ' + msg + ", Factory:" + this.getfactory () + ", Salary:" + this.getsalary ();}}
From the code above, we see that both the student and the worker have implemented the talk interface and have rewritten the amount Alk method
This is a basic factory model!!!! For details, see: http://blog.csdn.net/simonchi/article/details/11921409
What if a student or a worker wants to sing?
Does the talk interface add singing methods, and then the students and workers implement the class to achieve the singing method??? And still have to achieve the singing method, do not sing all AH!!! This is not very pit Daddy?????
At this time we need an intermediary agent layer to solve this problem!!!
proxy class
Package com.tree.demo.proxy; /** * classname:talkproxy <br/> * Function:todo add Function. <br/> * Reason: TODO Add REASON. <br/> * Date: December 25, 2014 PM 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;} @Overridepublic 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;}}
Notice the structure of this proxy class, implement the interface, and through the construction method to initialize the internal interface variables, and in the proxy class to achieve the singing!!!
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 ("My Chinese Dream")); Talkproxy TP2 = new Talkproxy (W1); System.out.println (tp2.sing ("Our Common people"));
At this time can see when students want to sing, through the proxy class to achieve, the student class object to the construction method of the proxy class can!! So when who wants to sing, who will act under the line!!
The role of the proxy class here is the agent of a singing method, want to sing will not have to modify the interface, modify the implementation class, very simple, very flexible!
If you still want to curse, in the proxy class to add a way to curse, who want to curse on the pass who go in on the line!!!
Implementation of "JAVA" proxy proxies