"Java design mode" proxy mode

Source: Internet
Author: User

Several scenarios for calculating the run time of methods in a class:

Client:

1  PackageCom.tn.proxy;2 3  Public classClient {4      Public Static voidMain (string[] args) {5         /*Car car=new car ();6 Car.move ();*/7         8         //run time by inheritance calculation method9         /*cartimebyextends ctp=new cartimebyextends ();Ten Ctp.move ();*/ One          A         //calculate the method run time in a class by aggregating -         /*Car car=new car (); - cartimebyaggregate ctba=new cartimebyaggregate (car); the ctba.getcarrunningtime ();*/ -     } -}

Movable:

1  Package Com.tn.proxy; 2 3  Public Interface Movable {4     void move (); 5     void stop (); 6 }

Car:

1  PackageCom.tn.proxy;2 3 ImportJava.util.Random;4 5  Public classCarImplementsMovable {6 7 @Override8      Public voidMove () {9         /**Ten * The two-paragraph comment code in the method adds the log and calculation method execution time to the move () method.  One          */ A         /*long Start=system.currenttimemillis (); - System.out.println ("Car is start moving ..." +start);*/ -System.out.println ("Car is moving ..."); the         Try { -Thread.Sleep (NewRandom (). Nextint (1500)); -}Catch(interruptedexception e) { - e.printstacktrace (); +         } -         /*long End=system.currenttimemillis (); + System.out.println ("Car is stop moving ..." +end); A System.out.println ("Car Running Time is" + (End-start));*/ at     } -  - @Override -      Public voidStop () { -System.out.println ("Car is stopped.")); -     } in  -}

Cartimebyextends:

1  PackageCom.tn.proxy;2 /**3 * Run time of methods in the calculation class by inheritance4  * @authorXiongjiawei5  *6  */7  Public classCartimebyextendsextendscar{8 @Override9      Public voidMove () {Ten         Longstart=System.currenttimemillis (); One         Super. Move (); A         LongEnd=System.currenttimemillis (); -System.out.println ("Car Running time is:" + (end-start)); -     } the}

Cartimebyaggregate:

1  PackageCom.tn.proxy;2 /**3 * Run time by aggregation calculation method4  * @authorXiongjiawei5  *6  */7  Public classCartimebyaggregate {8 car car;9 Ten      Publiccartimebyaggregate (car car) { One          This. car=car; A     } -      -      Public voidGetcarrunningtime () { the         Longstart=System.currenttimemillis (); - Car.move (); -         LongEnd=System.currenttimemillis (); -System.out.println ("Car Running time is:" + (end-start)); +     } -}

The above functions are implemented through static proxies:

Client:

1  PackageCom.tn.proxy;2 3  Public classClient {4      Public Static voidMain (string[] args) {5Car car=NewCar ();6Cartimeproxy ctp=Newcartimeproxy (car);7Carlogproxy clp=NewCarlogproxy (CTP);8 Clp.move ();9         /*running result: Car is packed by time, time is packed in journalTen Logging ... One Start time:1494730233358 A Car is moving ... - End time:1494730234835 - logged. the          */ -System.out.println ("--------------------------------------"); -Movable clp2=Newcarlogproxy (car); -Movable ctp2=NewCartimeproxy (CLP2); + Ctp2.move (); -         /* + Run Result: Time packing log, log packing car A Start time:1494730473747 at Logging ... - Car is moving ... - logged. - End time:1494730474995 -         */ -     } in}

Movable:

1  Package Com.tn.proxy; 2 3  Public Interface Movable {4     void move (); 5 }

Car:

1  PackageCom.tn.proxy;2 3 ImportJava.util.Random;4 5  Public classCarImplementsMovable {6 7 @Override8      Public voidMove () {9System.out.println ("Car is moving ...");Ten         Try { OneThread.Sleep (NewRandom (). Nextint (1500)); A}Catch(interruptedexception e) { - e.printstacktrace (); -         } the     } -}

Cartimeproxy:

1  PackageCom.tn.proxy;2 3  Public classCartimeproxyImplementsmovable{4 movable movable;5      Publiccartimeproxy (movable movable) {6          This. movable=movable;7     }8 @Override9      Public voidMove () {Ten         Longstart=System.currenttimemillis (); OneSystem.out.println ("Start time:" +start); A Movable.move (); -         LongEnd=System.currenttimemillis (); -System.out.println ("End time:" +end); the     } -}

Carlogproxy:

1  PackageCom.tn.proxy;2 3  Public classCarlogproxyImplementsMovable {4 movable movable;5     6      Publiccarlogproxy (movable movable) {7          This. movable=movable;8     }9     Ten @Override One      Public voidMove () { ASystem.out.println ("Logging ..."); - Movable.move (); -System.out.println ("logged.")); the     } -  -}

"Java design mode" proxy mode

Related Article

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.