静态代理,动态代理---笔记

  • 静态代理

例如租房子。

public interface Leasing {
    public String doLeasing(String name,long money);
}

一般实现类

public class LeasingImpl implements Leasing {
    public String doLeasing(String name ,long money)
    {
        return name+"租到房,价格:"+money;
    }
}

中介的实现类

public class PreoxyLeasing implements Leasing {

    LeasingImpl mLeasImpl;
    public PreoxyLeasing( LeasingImpl mLeasImpl) {
        this.mLeasImpl =mLeasImpl;
    }

    @Override
    public String doLeasing(String name,long money) {
       getMoeyFromUser();
        System.out.println("用户多花了"+(money/2));
        getMoeyFromHouser();
        System.out.println("房东多花了"+(money/2));
        return name+"中介帮忙租到房子 价格:"+money;
    }
    public void getMoeyFromUser() {
        System.out.println("收取用户的一半租金");
    }
    public void getMoeyFromHouser() {
        System.out.println("收取房东的一半租金");
    }
}

租客自己租房。

 Leasing mLeas = new LeasingImpl();
        System.out.println(mLeas.doLeasing("自己",1000l));

租客通过中介来租房

  Leasing mLeas = new LeasingImpl();   
 mLeas = new PreoxyLeasing(new LeasingImpl());
 System.out.println(mLeas.doLeasing("中介",1000l));
  • 动态代理。

首先实现InvocationHandler接口,处理invoke方法,使用动态代理,调用每个方法都会执行invoke方法。

public class LeasingHandler implements InvocationHandler {
    Object mBase;

    public LeasingHandler(Object mBase) {
        this.mBase = mBase;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //拦截方法
        if("doLeasing".equals(method.getName()))
        {
            {
                System.out.println("收用户一半的房租");
                System.out.println("收房主一半的房租");
                Long money = (Long) args[1];
                money +=money/2;
                // 帮忙租房子
                String things = (String) method.invoke(mBase,args[0], money);
                return things;
            }
        }
        return null;
    }
}
Java 静态代理是一种常见的设计模式,其主要思想是通过一个代理对象来控制对真实对象的访问,从而增强真实对象的功能或者限制对真实对象的访问。 静态代理的实现需要创建一个代理类和一个真实对象类,代理类和真实对象类都实现同一个接口或者继承同一个父类,代理类中持有真实对象的引用,在代理类的方法中调用真实对象的方法并对其进行增强或者限制。 静态代理的优点是实现简单,可以在不修改真实对象的情况下对其进行增强或者限制,缺点是需要为每一个真实对象类创建一个代理类,工作量较大。 下面是一个简单的静态代理示例: ```java public interface Subject { void request(); } public class RealSubject implements Subject { @Override public void request() { System.out.println("Real Subject Request"); } } public class ProxySubject implements Subject { private RealSubject realSubject; public ProxySubject(RealSubject realSubject) { this.realSubject = realSubject; } @Override public void request() { System.out.println("Before Real Subject Request"); realSubject.request(); System.out.println("After Real Subject Request"); } } public class Main { public static void main(String[] args) { RealSubject realSubject = new RealSubject(); ProxySubject proxySubject = new ProxySubject(realSubject); proxySubject.request(); } } ``` 在上面的示例中,Subject 接口定义了一个 request 方法,RealSubject 类是真实对象类,实现了 Subject 接口的 request 方法,ProxySubject 类是代理类,也实现了 Subject 接口的 request 方法,在其方法中调用了 RealSubject 的 request 方法并对其进行了增强。 在 Main 类中,我们创建了一个 RealSubject 对象和一个 ProxySubject 对象,通过调用 proxySubject 的 request 方法,实际上是调用了它持有的 realSubject 的 request 方法,并对其进行了增强。 注意,在这个示例中,RealSubject 类和 ProxySubject 类都实现了 Subject 接口,这是静态代理的一种常见实现方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值