设计模式-代理模式Proxy

定义:为其他对象提供一种代理以控制对这个对象的访问。

实现组成:

  • 抽象角色:通过接口或抽象类声明真实角色实现的业务方法。

  • 代理角色:实现抽象角色,是真实角色的代理,通过真实角色的业务逻辑方法来实现抽象方法,并可以附加自己的操作。

  • 真实角色:实现抽象角色,定义真实角色所要实现的业务逻辑,供代理角色调用

两种实现方式:静态代理、动态代理

静态代理模拟的代码:

1、抽象角色(代理实现的方法)

public interface ILog {

    public String printLog();
}

2、真实角色(被代理的类)

public class Dog implements ILog {

    public Dog(){

    }

    public  String printLog(){
        System.out.println("this is a Dog.");
        return "end";
    }
}

3、代理角色(代理)

public class StaticProxy implements ILog {

    ILog target;

    public StaticProxy(ILog target) {
        this.target = target;
    }

    @Override
    public String printLog() {
        System.out.println("before print");
        String result = target.print();
        System.out.println("after print");

        return "print " + result;
    }
}

4、测试类 

public class Test {
    public static void main(String[] args) {
        IDemo demo = new StaticProxy(new Demo());
        System.out.println(demo.print());
    }
}

动态代理模拟的代码:

 1、抽象角色(代理实现的内容)

public  interface Movable{
    void move();
}

2、真实角色(被代理的类)

public class Car implements Movable {
   
    public void move() {
        System.out.println("Tank moving claclacla..."+this.getClass().getName());
        try {
            Thread.sleep(new Random().nextInt(10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public class Plane implements Movable {
   
    public void move() {
        System.out.println("Tank moving claclacla..."+this.getClass().getName());
        try {
            Thread.sleep(new Random().nextInt(10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

3、创建实现代理的类(通过反射实现)


class TimeProxy implements InvocationHandler {
    Movable m;

    public TimeProxy(Movable m) {
        this.m = m;
    }

    public void before() {
        System.out.println("method start.."+m.getClass().getName());
    }

    public void after() {
        System.out.println("method stop.."+m.getClass().getName());
    }


    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object o = method.invoke(m, args);
        after();
        return o;
    }

}

4、测试类


public class Test {
	public static void main(String[] args) {
		 
		Movable m = (Movable) Proxy.newProxyInstance(
				Car.class.getClassLoader(), new Class[] { Movable.class },  
				new TimeProxy(new Car()));
		m.move();
		
	
		Movable m2 = (Movable) Proxy.newProxyInstance(
				Car.class.getClassLoader(), new Class[] { Movable.class },  
				new TimeProxy(new Plane()));
		m2.move();
		
	}
}

总结:

优点:

        1、职责清晰。2、高扩展性。3、智能化

缺点:

        1)、由于在客户端和真实主题之间增加了代理对象,因此有些类型的代理模式可能会造成请

             求的处理速度变慢。

        2)、实现代理模式需要额外的工作,有些代理模式的实现非常复杂。

项目中的使用场景:AOP面向切面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值