【java設計模式】代理模式

來源:互聯網
上載者:User

標籤:ack   pack   運行時   rri   tar   random   div   cart   rup   

計算類中方法已耗用時間的幾種方案:

Client:

 1 package com.tn.proxy; 2  3 public class Client { 4     public static void main(String[] args) { 5         /* Car car=new Car(); 6         car.move(); */ 7          8         //通過繼承計算方法的已耗用時間 9         /* CarTimeByExtends ctp=new CarTimeByExtends();10         ctp.move(); */11         12         //通過彙總計算類中方法已耗用時間13         /* Car car=new Car();14         CarTimeByAggregate ctba=new CarTimeByAggregate(car);15         ctba.getCarRunningTime(); */16     }17 }

Movable:

1 package com.tn.proxy;2 3 public interface Movable {4     void move();5     void stop();6 }

Car:

 1 package com.tn.proxy; 2  3 import java.util.Random; 4  5 public class Car implements Movable { 6  7     @Override 8     public void move() { 9         /**10          * 方法內的兩段注釋代碼為給move()方法增加日誌及計算方法執行時間。11          */12         /* long start=System.currentTimeMillis();13         System.out.println("Car is start moving..."+start); */14         System.out.println("Car is moving...");15         try {16             Thread.sleep(new Random().nextInt(1500));17         } catch (InterruptedException e) {18             e.printStackTrace();19         }20         /* long end=System.currentTimeMillis();21         System.out.println("Car is stop moving..."+end);22         System.out.println("Car running time is "+(end-start)); */23     }24 25     @Override26     public void stop() {27         System.out.println("Car is stopped.");28     }29 30 }

CarTimeByExtends:

 1 package com.tn.proxy; 2 /** 3  * 通過繼承計算類中方法的已耗用時間 4  * @author xiongjiawei 5  * 6  */ 7 public class CarTimeByExtends extends Car{ 8     @Override 9     public void move() {10         long start=System.currentTimeMillis();11         super.move();12         long end=System.currentTimeMillis();13         System.out.println("Car running time is:"+(end-start));14     }15 }

CarTimeByAggregate:

 1 package com.tn.proxy; 2 /** 3  * 通過彙總計算方法已耗用時間 4  * @author xiongjiawei 5  * 6  */ 7 public class CarTimeByAggregate { 8     Car car; 9 10     public CarTimeByAggregate(Car car){11         this.car=car;12     }13     14     public void getCarRunningTime(){15         long start=System.currentTimeMillis();16         car.move();17         long end=System.currentTimeMillis();18         System.out.println("Car running time is:"+(end-start));19     }20 }

 通過靜態代理實現以上功能:

Client:

 1 package com.tn.proxy; 2  3 public class Client { 4     public static void main(String[] args) { 5         Car car=new Car(); 6         CarTimeProxy ctp=new CarTimeProxy(car); 7         CarLogProxy clp=new CarLogProxy(ctp); 8         clp.move(); 9         /*    運行結果:Car被時間封裝,時間被日誌封裝10              logging...11             start time:149473023335812             Car is moving...13             end time:149473023483514             logged.15          */16         System.out.println("--------------------------------------");17         Movable clp2=new CarLogProxy(car);18         Movable ctp2=new CarTimeProxy(clp2);19         ctp2.move();20         /*21         運行結果:時間封裝日誌,日誌封裝car22         start time:149473047374723         logging...24         Car is moving...25         logged.26         end time:149473047499527         */28     }29 }

Movable:

1 package com.tn.proxy;2 3 public interface Movable {4     void move();5 }

Car:

 1 package com.tn.proxy; 2  3 import java.util.Random; 4  5 public class Car implements Movable { 6  7     @Override 8     public void move() { 9         System.out.println("Car is moving...");10         try {11             Thread.sleep(new Random().nextInt(1500));12         } catch (InterruptedException e) {13             e.printStackTrace();14         }15     }16 }

CarTimeProxy:

 1 package com.tn.proxy; 2  3 public class CarTimeProxy implements Movable{ 4     Movable movable; 5     public CarTimeProxy(Movable movable){ 6         this.movable=movable; 7     } 8     @Override 9     public void move() {10         long start=System.currentTimeMillis();11         System.out.println("start time:"+start);12         movable.move();13         long end=System.currentTimeMillis();14         System.out.println("end time:"+end);15     }16 }

CarLogProxy:

 1 package com.tn.proxy; 2  3 public class CarLogProxy implements Movable { 4     Movable movable; 5      6     public CarLogProxy(Movable movable){ 7         this.movable=movable; 8     } 9     10     @Override11     public void move() {12         System.out.println("logging...");13         movable.move();14         System.out.println("logged.");15     }16 17 }

 

【java設計模式】代理模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.