The proxy mode of Java design mode proxy--static proxy

Source: Internet
Author: User
Tags ticket

Very happy, now have time to write their own blog, now roughly write about the proxy mode it ...

The same, as in previous sources, are learned from Marco Video.

The so-called agent is one person or one body acting on behalf of another person or another body. In some cases, a client does not want or cannot refer directly to an object, whereas a proxy object can act as an intermediary before the client and the target object.
The proxy mode provides a proxy object for an object, and the proxy object controls the reference to the original object. Life Example: The New Year overtime more busy, no time to buy train tickets, then you can make a call to the nearby ticketing center, ask them to help you buy a home train ticket, of course, this will add additional labor costs. But to clear the ticket center itself does not sell tickets, only the railway station to really sell tickets, ticket center to sell you tickets in fact through the railway station to achieve. This is important! Above This example, you are "customer", ticket Center is "Agent role", the railway station is "real role", sell tickets called "Abstract sell Ticket"! (Excerpt from the network)

Talk less, everything is in the code. Post code:

Can be an interface, or it can be an abstract class public interface moveable {void move ();} An implementation class, a real role, that is, the object being proxied import Java.util.random;public class Tank implements moveable {@Overridepublic void move () { System.out.println ("Tank moving ..."); try {thread.sleep (New Random (). Nextint (10000));} catch (Interruptedexception e) {e.printstacktrace ();}}} Agent tank time, proxy object, that is, Agent role public class Tanktimeproxy implements moveable {public    tanktimeproxy (moveable t) {        Super ();        THIS.T = t;    }    Moveable T;    @Override public    void Move () {        Long start = System.currenttimemillis ();        System.out.println ("StartTime:" + start);        T.move ();        Long end = System.currenttimemillis ();        System.out.println ("Time:" + (End-start));}    }

Test class

public class Test3 {public static void main (string[] args) {Tanktimeproxy TTP =  new Tanktimeproxy (New Tank ()); Ttp.move ();}} Output Starttime:1437465854554tank moving...time:3518

"Proxy mode allows the use of abstract classes or interfaces as an" abstract role ", each" Proxy role "proxy a" real role ", if you want to proxy" real role "more, this will inevitably cause a lot of" agent role "caused by the rapid expansion of code, in fact, its internal structure is similar, if at runtime can be dynamically generated" Agent role "just fine. (Excerpt from the network)

Later, I will write the dynamic agent.

The proxy mode of Java design mode proxy--static proxy

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.