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