设计模式——外观(门面)模式09

外观模式:能为系统框架或其他复杂业务流程封装提供一个简单的接口。
例如抽奖过程中
设计模式,一定要敲代码理解

在这里插入图片描述

调用1(抽奖系统)

/**
 * @author ggbond
 * @date 2024年04月08日 10:34
 */
public class Lottery {
    public String  getId(){
        System.out.println("获取商品id");
        return  "00001";
    }
    public boolean  stockControl(String id){
        System.out.println("库存减一");
        return true;
    }
}

调用2(快递系统)

/**
 * @author ggbond
 * @date 2024年04月08日 10:38
 */
public class Delivery {
    public String  createSaleId(String id){
        System.out.println("商品打包,生产快递订单号");
        return  "00001";
    }
    public boolean  send(String id){
        System.out.println("发货");
        return true;
    }
}

门面

/**
 * @author ggbond
 * @date 2024年04月08日 10:43
 */
public class Facade {
    private Delivery delivery;
    private  Lottery lottery;

    public Facade( Lottery lottery,Delivery delivery) {
        this.delivery = delivery;
        this.lottery = lottery;
    }

    public void  toLottery(){
       String id= lottery.getId();//抽奖
        boolean f= lottery.stockControl(id); //库存管理
        if(true==f) {
            delivery.createSaleId(id);//生成订单信息
            delivery.send(id); //发货
        }

    }
}

客户端调用

/**
 * @author ggbond
 * @date 2024年04月08日 10:40
 */
public class Main {
    public static void main(String[] args) {
        //客户端进行抽奖(普通写法)
        Lottery lottery=new Lottery();
        Delivery delivery=new Delivery();
       String id=lottery.getId();
        boolean stockControl = lottery.stockControl(id);
        if (stockControl) {
            delivery.createSaleId(id);//生成订单信息
            delivery.send(id); //发货
        }

        //门面模式 抽奖封装,客户端执行封装好的一个服务方法。
        Facade facade=new Facade(lottery,delivery);
        facade.toLottery();
    }
}

结果

获取商品id
库存减一
商品打包,生产快递订单号
发货

总结

门面模式目的在于,封装个子系统代码流程,使其独立于复杂子系统。

相关推荐

  1. 设计模式 门面模式

    2024-04-09 08:34:03       42 阅读
  2. 设计模式-06 设计模式-Facade Pattern门面模式

    2024-04-09 08:34:03       39 阅读
  3. 门面设计模式

    2024-04-09 08:34:03       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-09 08:34:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-09 08:34:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-09 08:34:03       82 阅读
  4. Python语言-面向对象

    2024-04-09 08:34:03       91 阅读

热门阅读

  1. OneFlow深度学习框架介绍

    2024-04-09 08:34:03       38 阅读
  2. Pandas进行数据分析

    2024-04-09 08:34:03       33 阅读
  3. MySQL_5.7.17的安装与配置

    2024-04-09 08:34:03       35 阅读
  4. MySQL-9. 事务

    2024-04-09 08:34:03       38 阅读
  5. 【备忘录】MySQL 8.3 中删除的功能

    2024-04-09 08:34:03       35 阅读
  6. 4.8QT

    4.8QT

    2024-04-09 08:34:03      35 阅读
  7. 冒泡排序算法实现步骤

    2024-04-09 08:34:03       33 阅读