[ThinkingInJava] 33. Dynamic proxy mechanism, thinkinginjava33

Source: Internet
Author: User

[ThinkingInJava] 33. Dynamic proxy mechanism, thinkinginjava33

/*** Book: Thinking In Java * function: Dynamic proxy 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 {// implementation Interface @ Overridepublic void doSomething () {print ("doSomething") ;}@ Overridepublic void somethingElse (String arg) {print ("somthingElse" + arg );}} class SimpleProxy implements Interface {// after two interfaces are implemented, a variable private Interface proxied; public SimpleProxy (Interface proxied) {this. proxied = proxied;} public void doSomething () {print ("SimpleProxy doSomething"); // first implement your own functions, and then call the function proxied of the passed interface. 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 }}


So I said, maybe my understanding is not deep enough. I think dynamic proxy is the combination of classes, and another class is used as the private variable in one class, to implement various functions, you can also execute the various functions of the proxy class, you can intercept various types, you can determine whether to execute the methods of this Member class, or execute


Output:

DoSomething obj1
SomthingElse bonobo obj1
SimpleProxy doSomething obj1
DoSomething obj1
SimpleProxy somethingElse bonobo obj1
SomthingElse bonobo obj1









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.