"Thinkinginjava" 33, dynamic agent mechanism

Source: Internet
Author: User

/*** book: "Thinking In Java" * Features: Dynamic agent mechanism * file: simpleproxydemo.java* time: April 15, 2015 21:41:39* Author: cutter_point*/package Lesson14typeinformation;import static Net.mindview.util.print.*;interface interface{//One interface provides two methods void DoSomething () ; void SomethingElse (String arg);} Class Realobject implements interface{//implements the interface @overridepublic void DoSomething () {print ("dosomething");} @Overridepublic void SomethingElse (String arg) {print ("Somthingelse" + arg);}} Class Simpleproxy implements interface{//implements two interfaces, a variable is added to the private Interface proxied;public Simpleproxy (Interface proxied) {this.proxied = proxied;}  public void DoSomething () {print ("Simpleproxy dosomething");//implement own function first, then call the function of the interface passed in proxied.dosomething ();}    public void SomethingElse (String arg) {print ("Simpleproxy somethingelse" + arg);  Proxied.somethingelse (ARG);    }}public class Simpleproxydemo {public static void consumer (Interface iface) {iface.dosomething (); Iface.somethingelse ("Bonobo");} public static void Main (string[] args) {Consumer (new Realobject ()); Consumer (new Simpleproxy (New Realobject ()));//Dynamic Proxy}} 


I just said, maybe my understanding is not deep enough, I think the dynamic agent is the use of the combination of classes, using a class inside the use of another class as a private variable, to achieve various functions, but also can execute the agent of the various functions of the class, can be various interception, you can determine whether or not to execute this member class method, Or do that.


Output:

DoSomething obj1
Somthingelse Bonobo obj1
Simpleproxy dosomething obj1
DoSomething obj1
Simpleproxy somethingelse Bonobo obj1
Somthingelse Bonobo obj1









"Thinkinginjava" 33, dynamic agent mechanism

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.