java 代理模式(靜態代理+動態代理)

來源:互聯網
上載者:User

靜態代理:

ISubject:

/** * @author com.tiantian * @version 建立時間:2012-11-20 下午1:49:29 */public interface ISubject {    public void request();}

RealSubject(真實角色):

/** * @author com.tiantian * @version 建立時間:2012-11-20 下午1:51:37 */public class RealSubject implements ISubject{    @Override    public void request() {        System.out.println("realSubject requesting");    }}

ProxySubject(代理類):

/** * @author com.tiantian * @version 建立時間:2012-11-20 下午1:52:22 */public class ProxySubject implements ISubject{    private RealSubject realSubject;    public ProxySubject() {        realSubject = new RealSubject();    }    @Override    public void request() {        System.out.println("do something before");        realSubject.request();        System.out.println("do something after");    }}

Test(用戶端測試):

/** * @author com.tiantian * @version 建立時間:2012-11-20 下午1:54:47 */public class Test {    public static void main(String[] args) {        ISubject proxySubject = new ProxySubject();        proxySubject.request();    }}

------------------------------------------------------------------------------------------------------------------------------

動態代理:

ISubject:

/** * @author com.tiantian * @version 建立時間:2012-11-20 下午2:51:31 */public interface ISubject {    public void request();}

RealSubject:

/** * @author com.tiantian * @version 建立時間:2012-11-20 下午2:52:00 */public class RealSubject implements ISubject{    @Override    public void request() {        System.out.println("realSubject requesting");    }}

SubjectInvocationHandler(調用處理類):

/** * @author com.tiantian * @version 建立時間:2012-11-20 下午2:54:38 * 調用處理類 */public class SubjectInvocationHandler implements InvocationHandler{    private Object obj;        public SubjectInvocationHandler(Object obj) {        this.obj = obj;    }        /**     * 組建代理程式類工廠     * @author com.tiantian     * @param realObj     * @return 返回產生的代理類     */    public static Object getProxyInstanceFactory(Object realObj){        Class<?> classType = realObj.getClass();        return Proxy.newProxyInstance(classType.getClassLoader(),                 classType.getInterfaces(), new SubjectInvocationHandler(realObj));    }        @Override    public Object invoke(Object proxy, Method method, Object[] args)            throws Throwable {        System.out.println("before");                method.invoke(obj, args);                System.out.println("after");                return null;    }}

Test:

/** * @author com.tiantian * @version 建立時間:2012-11-20 下午2:56:25 */public class Test {    public static void main(String[] args) {        RealSubject realSubject = new RealSubject();//        InvocationHandler handler = new SubjectInvocationHandler(realSubject);//        ISubject subject = (ISubject)Proxy.newProxyInstance(realSubject.getClass().getClassLoader(),//                realSubject.getClass().getInterfaces(), handler);        ISubject subject = (ISubject)SubjectInvocationHandler.getProxyInstanceFactory(realSubject);        subject.request();            }}

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.