代理模式

现实生活中,如美的空调,每个地区都会有各种代理商,就是美的不与消费者直接接触。
在编程中即为降低对象之间的耦合,真正做了什么,使用者并不需要知道被使用者修改了什么东西。

package com.lt.designpatterns;

/**
 * 手写一个代理模式
 */
public class ProxyClient {
    public static void main(String[] args) {
        DellProxyInterface dellProxyInterface = new ProxyPerson();
        System.out.println("正在为您进行代理购物!!");
        dellProxyInterface.buyMe();
    }
}

/**
 * 代理接口
 */
interface DellProxyInterface {
    void buyMe();
}

/**
 * 戴尔电脑
 */
class DellComputer implements DellProxyInterface {

    @Override
    public void buyMe() {
        System.out.println("快买我,我快等不及了!");
    }
}

/**
 * 代理人
 */
class ProxyPerson implements DellProxyInterface {
    private DellComputer dellComputer;

    @Override
    public void buyMe() {
        buyBefore();
        if (dellComputer == null) {
            dellComputer = new DellComputer();
        }
        dellComputer.buyMe();

    }

    private void buyBefore() {
        System.out.println("别急,我是老鸨,先来点手续费!");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值