Java-設計模式之代理模式

來源:互聯網
上載者:User

標籤:main   第一個   string   void   targe   current   interrupt   延遲   port   

1.什麼是代理模式?

  代理模式就是為其他對象提供一種代理,以控制對這個對象的訪問。例如我們在購買火車票的時候,在有些地方有火車票的代售點,這些代售點可以協助火車站來實行購票的功能,像這種模式就是代理模式。在Java中,代理模式啟動中介的作用,可以去掉功能服務或者增加額外的服務。

2.代碼模式的分類

  A、遠程代理:為不同地理的對象提供區域網路代表對象,相似於用戶端和伺服器一樣的模式。

  B、虛擬代理:根據需要將資源消耗很大的對象進行延遲,真正需要的時候進行建立。例如,在我們瀏覽網頁的時候,有時候需要載入很大的圖片,在圖片為載入成功之前,我們可以使用一張臨時的圖片來暫時的顯示。

  C、保護代理:就是控制一個對象的存取權限。例如,我們在瀏覽文章的時候的時候,當我們還沒有註冊時候,只能進行瀏覽的功能,當我們註冊過後就可以進行發帖的功能。

  D、智能引用代理:就是給目標對象提供額外的服務。相當於就是給代理增加了一些功能或者服務。

3.代理模式的實現方式

  主要有兩種實現方式:一是靜態代理,二是動態代理

 A、靜態代理:

   代理和被代理對象在代理之前是確定的。他們都實現了相同的介面或者繼承了相同的抽象類別。

   通過繼承來實現代理模式:

  

1 package com.example.DesignPattern.ProxyPattern;2 3 public interface MoveAble {4     public void move();5 }
 1 package com.example.DesignPattern.ProxyPattern; 2  3 import java.util.Random; 4  5 public class Car implements MoveAble{ 6  7     @Override 8     public void move() { 9         try {10             System.out.println("汽車行駛中...");11             Thread.sleep(new Random().nextInt(1000));12         } catch (InterruptedException e) {13             e.printStackTrace();14         }15     }16 17 }
 1 package com.example.DesignPattern.ProxyPattern; 2  3 public class Car1 extends Car { 4     @Override 5     public void move() { 6         long startTime = System.currentTimeMillis(); 7         System.out.println("汽車開始行駛..."); 8         super.move(); 9         long endTime = System.currentTimeMillis();10         System.out.println("汽車結束行駛..行駛時間:" + (endTime - startTime) + "毫秒");11     }12 }

  通過彙總的方式來實現代理模式:

 1 package com.example.DesignPattern.ProxyPattern; 2  3  4  5 public class Car2 implements MoveAble{ 6     private Car mCar = null; 7     public Car2(Car car) 8     { 9         mCar = car;10     }11     @Override12     public void move() {13         long startTime = System.currentTimeMillis();14         System.out.println("汽車開始行駛...");15         mCar.move();16         long endTime = System.currentTimeMillis();17         System.out.println("汽車結束行駛..行駛時間:" + (endTime - startTime) + "毫秒");18     }19 20 }

  B.使用JDK動態代理:

 1 package com.example.DesignPattern.AgentPattern; 2  3 import java.lang.reflect.InvocationHandler; 4 import java.lang.reflect.Method; 5  6 public class TimeHandler implements InvocationHandler{ 7     private Object target = null; 8     public TimeHandler(Object target) 9     {10         this.target = target;11     }12     /**13      * proxy:被代理的對象14      * method:被代理對象的方法15      * args:代理方法的對象16      * 17      * 傳回值:代理的方法的傳回值18      */19     @Override20     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {21         method.invoke(target);22         return null;23     }24 25 }
 1 package com.example.DesignPattern.AgentPattern; 2  3 import java.lang.reflect.Proxy; 4  5 import com.example.DesignPattern.ProxyPattern.Car; 6 import com.example.DesignPattern.ProxyPattern.MoveAble; 7  8 public class Test { 9     public static void main(String[] args) {10         Car car = new Car();11         TimeHandler handler = new TimeHandler(car);12         Class<?> cls = car.getClass();13         /**14          * 第一個參數:被代理類的類載入器15          * 第二個參數:被代理類實現介面16          * 第三個參數:InvocationHandler對象,需要我們手動建立一個子類,來實現功能17          */18         MoveAble m = (MoveAble) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), handler);19         m.move();20     }21 }

   JDK動態代理的實現步驟:

  1.建立一個實現介面InvocationHandler的類,它必須實現invoke方法。

  2.建立被代理的類以及介面

  3.調用Proxy的靜態方法,建立一個代理類:newProxyInstance(ClassLoader load, Class []intefeaces, InvolcationHandler h);

  4.通過代理調用方法

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.