Java proxy mode

Source: Internet
Author: User

The primary role of the proxy mode is to provide a proxy for other objects to control access to the object. In some cases, an object does not want or cannot directly reference another object, whereas a proxy object can act as an intermediary between the client and the target object. The idea of proxy mode is to provide additional processing or different operations to insert a proxy object between the actual object and the caller. These additional operations typically require communication with the actual object.    application Scenario Editing For example, suppose that a set of objects implement the same interface, the same method is implemented, but there are some objects in this set of objects that require a separate method, the traditional dumb way is to add this single method to each application, but the code reusability is low and the coupling is high. If the proxy method is a good solution to this problem.   Code Sample Edit Example Description Suppose there is a italk interface, there is an empty method of talk (speaking), all people objects implement (implements) this interface, implement the Talk () method, there are many places in the front end of people instantiation, To execute the talk method, and later found that some of these front-end in addition to talking to sing (sing), then we can neither add the Sing () method in the Italk interface, nor add sing method at each front end, we only add a proxy class Talkproxy, This proxy class implements the talk and Sing methods, and then invokes the proxy class on the client that needs the Sing method, the code is as follows: interface class Italkpublic interface Italk {public void Talk (String msg);} Implementation class Peoplepublic class people implements Italk {public String username;public string Age;public string GetName () {return US Ername;} public void SetName (String name) {this.username= name;} Public String Getage () {return age;} public void Setage (String age) {this.age = age;} Public people (string name1, String age1) {this.username= name1;this.age = age1;} public void Talk (String msg) {System.out.println (msg+"Hello, I Am" +username+ ", my Age is" +age ";}} Proxy class Talkproxypublic class Talkproxy implements Italk {Italk talker;public talkproxy (Italk talker) {//super (); this.talker =talker;} public void Talk (String msg) {talker.talk (msg);} public void Talk (String msg,string singname) {talker.talk (msg); Sing (singname);} private void Sing (String singname) {System.out.println ("singing:" +singname);}}} Application end Myproxytestpublic class Myproxytest {/** proxy mode * @param args*/public static void Main (string[] args) {/////Do not need to perform additional methods Italk p Eople1=new people ("The Lake Sea scattered people", "" ");p Eople1.talk (" No ProXY Test "); System.out.println ("-----------------------------");//Talkproxy talker=new Talkproxy (PEOPLE1) that requires additional methods to be performed; Talker.talk ("ProXY Test", "Qi Li Xiang");}}

Java proxy mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.