Java设计模式

Java实现23种设计模式(十一):代理模式(重点掌握)

2020-06-10  本文已影响0人  依然慢节奏

二十三种设计模式分类

设计模式三大分类.jpg

一、概述

在代理模式(Proxy Pattern)中,一个类代表另一个类的功能。这种类型的设计模式属于结构型模式。
代理模式的定义:由于某些原因需要给某对象提供一个代理以控制对该对象的访问。这时,访问对象不适合或者不能直接引用目标对象,代理对象作为访问对象和目标对象之间的中介。

优点

缺点

场景

1、远程代理。
2、虚拟代理。
3、Copy-on-Write代理。
4、保护(Protect or Access)代理。
5、Cache代理。


二、实现

1. 结构图

代理模式的主要角色如下。

代理模式

PS:UML结构图可以参考,例子实现并不根据UML图来完成,灵活实现即可;

2. 实现

2.1 静态代理

package cn.missbe.model.proxy;

/**
 * Copyright (c) 2020.
 * Email: love1208tt@foxmail.com
 * @author lyg  2020/4/28 下午2:18
 * description:
 * 可移动的(形容词)接口
 **/

public interface Movable {
    public void go();
}
package cn.missbe.model.proxy;

import cn.missbe.model.factory.Moveable;

/**
 * Copyright (c) 2020.
 * Email: love1208tt@foxmail.com
 * @author lyg  2020/4/28 下午2:18
 * description:
 * 被代理对象Car
 **/

public class Car implements Moveable {
    @Override
    public void go() {
        System.out.println("car is moving wu wu wu....");
    }
}
package cn.missbe.model.proxy;

import cn.missbe.model.factory.Moveable;

/**
 * Copyright (c) 2020.
 * Email: love1208tt@foxmail.com
 * @author lyg  2020/4/28 下午2:20
 * description:
 **/

public class CarLogProxy implements Moveable {
    /**被代理的对象*/
    Moveable moveable;

    public CarLogProxy(Moveable moveable) {
        this.moveable = moveable;
    }

    @Override
    public void go() {
        ///before process
        System.out.println("static proxy log start.....");
        this.moveable.go();
        ///after process
        System.out.println("static proxy log end.....");
    }
}

package cn.missbe.model.proxy;

import cn.missbe.model.factory.Moveable;

import java.util.Random;

/**
 * Copyright (c) 2020.
 * Email: love1208tt@foxmail.com
 *
 * @author lyg  2020/4/28 下午2:21
 * description:
 **/

public class CarTimeProxy implements Moveable {
    /**
     * 被代理的对象
     */
    Moveable moveable;

    public CarTimeProxy(Moveable moveable) {
        this.moveable = moveable;
    }

    @Override
    public void go() {
        ///before process
        long start = System.currentTimeMillis();
        this.moveable.go();
        try {
            Thread.sleep(new Random().nextInt(10_000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ///after process
        long end = System.currentTimeMillis();
        System.out.println("static proxy car run time:" + (end - start));
    }
}
package cn.missbe.model.proxy;

import cn.missbe.model.factory.Moveable;

/**
 * Copyright (c) 2020.
 * Email: love1208tt@foxmail.com
 *
 * @author lyg  2020/4/28 下午2:18
 * description:
 * 静态代理
 **/

public class StaticProxyMain {

    public static void main(String[] args) {
        System.out.println("==========No Proxy=============");
        Car car = new Car();
        car.go();
        System.out.println("==========Proxy=============");
        ///静态代理的组合,先记录日志再计算运行时间
        Moveable moveable = new CarLogProxy(new CarTimeProxy(new Car()));
        moveable.go();
    }

}

2.2 动态代理

package cn.missbe.model.proxy.dynamic;

import cn.missbe.model.factory.Moveable;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Random;

/**
 * Copyright (c) 2020.
 * Email: love1208tt@foxmail.com
 * @author lyg  2020/4/28 下午2:36
 * description:
 **/

public class CarTimeInvocationHandler implements InvocationHandler {
    /**被代理对象*/
    private Moveable moveable;

    public CarTimeInvocationHandler(Moveable moveable) {
        this.moveable = moveable;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        ///before process
        long start = System.currentTimeMillis();
        Object o = method.invoke(moveable, args);
        try {
            Thread.sleep(new Random().nextInt(10_000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        ///after process
        long end = System.currentTimeMillis();
        System.out.println("static proxy car run time:" + (end - start));
        return o;
    }
}
package cn.missbe.model.proxy.dynamic;

import cn.missbe.model.factory.Moveable;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Copyright (c) 2020.
 * Email: love1208tt@foxmail.com
 * @author lyg  2020/4/28 下午2:33
 * description:
 **/

public class CarLogInvocationHandler implements InvocationHandler {
    /**被代理对象*/
    private Moveable moveable;

    public CarLogInvocationHandler(Moveable moveable) {
        this.moveable = moveable;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        ///before process
        System.out.println("static proxy log start.....");
        Object o = method.invoke(moveable, args);
        ///after process
        System.out.println("static proxy log end.....");
        return o;
    }
}
package cn.missbe.model.proxy.dynamic;

import cn.missbe.model.factory.Moveable;
import cn.missbe.model.proxy.Car;

import java.lang.reflect.Proxy;

/**
 * Copyright (c) 2020.
 * Email: love1208tt@foxmail.com
 *
 * @author lyg  2020/4/28 下午2:31
 * description:
 **/

public class DynamicMain {
    public static void main(String[] args) {
        Car car = new Car();
        ///保存生成的代理类
        //        System.getProperties().put("jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true");
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");

        Moveable carLog = (Moveable) Proxy.newProxyInstance(car.getClass().getClassLoader(), new Class[]{Moveable.class}, new CarLogInvocationHandler(car));
        System.out.println("================LOG==================");
        carLog.go();

        Moveable carTime = (Moveable) Proxy.newProxyInstance(car.getClass().getClassLoader(), new Class[]{Moveable.class}, new CarTimeInvocationHandler(car));
        System.out.println("=================Time=================");
        carTime.go();
    }
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.sun.proxy;

import cn.missbe.model.factory.Moveable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

public final class $Proxy0 extends Proxy implements Moveable {
    private static Method m1;
    private static Method m2;
    private static Method m3;
    private static Method m0;

    public $Proxy0(InvocationHandler var1) throws  {
        super(var1);
    }

    public final boolean equals(Object var1) throws  {
        try {
            return (Boolean)super.h.invoke(this, m1, new Object[]{var1});
        } catch (RuntimeException | Error var3) {
            throw var3;
        } catch (Throwable var4) {
            throw new UndeclaredThrowableException(var4);
        }
    }

    public final String toString() throws  {
        try {
            return (String)super.h.invoke(this, m2, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final void go() throws  {
        try {
            super.h.invoke(this, m3, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    public final int hashCode() throws  {
        try {
            return (Integer)super.h.invoke(this, m0, (Object[])null);
        } catch (RuntimeException | Error var2) {
            throw var2;
        } catch (Throwable var3) {
            throw new UndeclaredThrowableException(var3);
        }
    }

    static {
        try {
            m1 = Class.forName("java.lang.Object").getMethod("equals", Class.forName("java.lang.Object"));
            m2 = Class.forName("java.lang.Object").getMethod("toString");
            m3 = Class.forName("cn.missbe.model.factory.Moveable").getMethod("go");
            m0 = Class.forName("java.lang.Object").getMethod("hashCode");
        } catch (NoSuchMethodException var2) {
            throw new NoSuchMethodError(var2.getMessage());
        } catch (ClassNotFoundException var3) {
            throw new NoClassDefFoundError(var3.getMessage());
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读