代理模式 -- 静态代理

一、代理模式介绍
1、代理模式简介
代理模式(Proxy),为其他对象提供一种代理以控制这个对象的访问。–引用至《大话设计模式》

2、代理模式组成
①访问者
②代理者
③被代理者

3、现实生活中案例
这里写图片描述

二、Java代码模拟代理模式(静态代理)
1、房东接口

/**
 * 
 * @author 大家都说名字长不会被发现
 *  房东接口
 */
public interface Tenant {
    String rent(double money);
}

2、具体房东类

/**
 * 
 * @author 大家都说名字长不会被发现
 *
 *  陈姓房东类
 */
public class TenantByChen implements Tenant{

    public String rent(double money) {
        //房东获得扣除中介费后的房租
        System.out.println("出租房屋,"+"获得房租:"+money+"¥");

        //将钥匙给中介
        return "钥匙";
    }



}

3、中介类

/**
 * 
 * @author 大家都说名字长不会被发现
 *  中介类
 */
public class IntermediaryProxy implements Tenant{
    private double agencyFee = 800;

    private Tenant tenant;
    public String rent(double money) {

        //获取房东对象
        tenant = new TenantByChen();
        //扣除中介费
        money = money - 800;
        System.out.println("中介获得,"+agencyFee+"¥中介费");

        //中介调用房东出租方法
        String key = tenant.rent(money);


        //替换门锁
        key = "换锁后的新"+key;
        return key;
    }



}

4、租客

/**
 * 
 * @author 大家都说名字长不会被发现
 *  租客类
 */
public class Landlord {
    public static void main(String[] args){
        Landlord landlord = new Landlord();
        landlord.renting();
    }

    public void renting(){
        //找中介公司租房
        IntermediaryProxy proxy = new IntermediaryProxy();
        String rent = proxy.rent(1500.00);

        System.out.println("租客获得"+rent);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值