设计模式(十一):外观模式

1. 外观模式的介绍

外观模式(Facade Pattern)属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。

外观模式包含两个个核心角色:

  • 外观(Facade):提供一个简化的接口,封装了系统的复杂性。外观模式的客户端通过与外观对象交互,而无需直接与系统的各个组件打交道。
  • 子系统(Subsystem):由多个相互关联的类组成,负责系统的具体功能。外观对象通过调用这些子系统来完成客户端的请求。

2. 外观模式的类图

在这里插入图片描述

3. 外观模式的实现

3.1 创建一个接口

package blog;

/**
 * 厨师
 */
public interface Chef {
    void cook();
}

3.2 创建接口的实现

package blog;

/**
 * 中国厨师
 */
public class ChineseChef implements Chef {

    @Override
    public void cook() {
        System.out.println("制作中餐");
    }
}

3.3 创建一个外观类

package blog;

/**
 * 西方厨师
 */
public class EuropeanChef implements Chef{
    @Override
    public void cook() {
        System.out.println("制作西餐");
    }
}

3.4 测试

/**
 * 客户端
 */
package blog;

public class FacadeDemo {
    public static void main(String[] args) {
        Restaurant restaurant = new Restaurant(new ChineseChef(), new EuropeanChef());

        restaurant.cookOfChinese();

        restaurant.cookOfEuropean();
    }
}

相关推荐

  1. 设计模式详解(二)——外观模式

    2024-05-01 06:56:04       41 阅读
  2. 设计模式外观模式

    2024-05-01 06:56:04       56 阅读
  3. 设计模式——外观模式

    2024-05-01 06:56:04       47 阅读
  4. 设计模式-外观模式

    2024-05-01 06:56:04       53 阅读
  5. 设计模式外观模式

    2024-05-01 06:56:04       43 阅读
  6. 设计模式: 外观模式

    2024-05-01 06:56:04       43 阅读

最近更新

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

    2024-05-01 06:56:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-01 06:56:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-01 06:56:04       87 阅读
  4. Python语言-面向对象

    2024-05-01 06:56:04       96 阅读

热门阅读

  1. bottom-up-attention.pytorch

    2024-05-01 06:56:04       35 阅读
  2. 二,网络安全常用术语

    2024-05-01 06:56:04       32 阅读
  3. 从零到屎山系列-游戏开发(Day1)

    2024-05-01 06:56:04       34 阅读
  4. zookeeper相关命令

    2024-05-01 06:56:04       31 阅读
  5. Python.第六章(函数)

    2024-05-01 06:56:04       24 阅读
  6. Electron打包流程

    2024-05-01 06:56:04       24 阅读
  7. selenium启动参数设置

    2024-05-01 06:56:04       29 阅读
  8. nvm pnpm powershell

    2024-05-01 06:56:04       34 阅读
  9. Ubuntu Linux完全入门视频教程

    2024-05-01 06:56:04       31 阅读
  10. 详解FastChat部署大模型API的实战教程

    2024-05-01 06:56:04       32 阅读