设计模式-代理模式

本文详细介绍了Java编程中的代理模式,包括简单代理、强制代理和动态代理。通过租房、开会等场景展示了代理如何增强功能和解耦。在简单代理中,中介除了租房还负责收费;强制代理中,董事长通过秘书参加会议;动态代理则利用Java的Proxy类实现代理,动态创建具有额外行为的对象。这些例子揭示了代理模式在实际问题解决中的灵活性和实用性。
摘要由CSDN通过智能技术生成

代理模式

在这里插入图片描述
在这里插入图片描述

简单代理

//租房接口
public interface RentHourseInterface {

    public void rent();
}

public class RentHouse implements RentHourseInterface {

    private String name;

    public RentHouse(String name) {
        this.name = name;
    }

    @Override
    public void rent() {
        System.out.println(this.name+"租房");
    }
}

//中介除了有代理租房还有收钱功能
public interface Proxy {

    public void getMoney();
}


public class RentHouseProxy implements RentHourseInterface,Proxy{

    private RentHourseInterface rentHourseInterface=null;

    public RentHouseProxy(String name) {
        this.rentHourseInterface = new RentHouse(name);
    }

    @Override
    public void rent() {
        rentHourseInterface.rent();
    }

    @Override
    public void getMoney() {
        System.out.println("中介收费");
    }
}

//场景类
   RentHouseProxy zhang = new RentHouseProxy("zhang");
        zhang.rent();
        //代理类实现多个接口,实现功能的增强
        zhang.getMoney();

强制代理

/**
 * 参与开会
 */
public interface SmallMeeting {

    public void participate();
}

/**
 * 董事长开会,但是没有时间,需要找到自己的秘书去替自己开会
 */
public class ChairMan implements SmallMeeting{

    private SmallMeeting proxy=null;
    private String name;

    public ChairMan(String name) {
        this.name = name;
    }

    public SmallMeeting getProxy(){
        this.proxy=new SecretaryProxy(this);
        return this.proxy;
    }

    @Override
    public void participate() {
        if(this.proxy!=null){
            System.out.println("董事长"+this.name+"授权秘书替自己去参加会议");
        }else{
            System.out.println("没空");
        }

    }
}


/**
 * 秘书代理参加会议
 */
public class SecretaryProxy implements  SmallMeeting{

    private SmallMeeting chairman;

    public SecretaryProxy(SmallMeeting chairman) {
        this.chairman = chairman;
    }

    @Override
    public void participate() {
        chairman.participate();
    }
}



   ChairMan chairMan = new ChairMan("zs");

        //找到代理秘书
        SmallMeeting proxy = chairMan.getProxy();

        proxy.participate();

        chairMan.participate();

        ChairMan chairMan1 = new ChairMan("ls");
        chairMan1.participate();


动态代理

public interface Buy {

    public void buy();
}

public class BuyCloth implements Buy {

    public void buy(){
        System.out.println("买衣服");
    }
}


public class MyInvocationHandler implements InvocationHandler {

    private final Object target;

    public MyInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("----------------------------");
        return method.invoke(target,args);
    }
}


public class Factory {
    public static void main(String[] args) {
       Buy  o = (Buy) get(new BuyCloth());
       o.buy();
    }



    public static Object get(Object target){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new MyInvocationHandler(target));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值