学习总结录 学习总结录
首页
归档
分类
标签
  • Java基础
  • Java集合
  • MySQL
  • Redis
  • JVM
  • 多线程
  • 计算机网络
  • 操作系统
  • Spring
  • Kafka
  • Elasticsearch
  • Python
  • 面试专题
  • 案例实践
  • 工具使用
  • 项目搭建
  • 服务治理
  • ORM框架
  • 分布式组件
  • MiniSpring
  • 设计模式
  • 算法思想
  • 编码规范
友链
关于
GitHub (opens new window)
首页
归档
分类
标签
  • Java基础
  • Java集合
  • MySQL
  • Redis
  • JVM
  • 多线程
  • 计算机网络
  • 操作系统
  • Spring
  • Kafka
  • Elasticsearch
  • Python
  • 面试专题
  • 案例实践
  • 工具使用
  • 项目搭建
  • 服务治理
  • ORM框架
  • 分布式组件
  • MiniSpring
  • 设计模式
  • 算法思想
  • 编码规范
友链
关于
GitHub (opens new window)
  • 设计模式

    • 设计模式-工厂模式
    • 设计模式-抽象工厂模式
    • 设计模式-单例模式
    • 设计模式-建造者模式
    • 设计模式-原型模式
    • 设计模式-适配器模式
    • 设计模式-桥接模式
    • 设计模式-组合模式
    • 设计模式-外观模式
      • 设计模式-外观模式
        • 概述
        • 定义
      • 外观模式结构
      • 外观模式应用案例
      • 外观模式优缺点
      • 外观模式使用环境
      • 参考
    • 设计模式-装饰模式
    • 设计模式-享元模式
    • 设计模式-代理模式
    • 设计模式-职责链模式
    • 设计模式-命令模式
    • 设计模式-迭代器模式
    • 设计模式-中介者模式
    • 设计模式-观察者模式
    • 设计模式-状态模式
    • 设计模式-策略模式
  • 算法思想

  • 编码规范

  • 技术思想
  • 设计模式
旭日
2023-03-27
目录

设计模式-外观模式

# 设计模式-外观模式

# 概述

当我们想要吃一个汉堡的时候,我们有两种方式,一是自己做,二是去店里买。

如果我们自己做,我们就需要去和食材这一列复杂的东西交互。而去店里买,我们只需要和服务员进行交互。

在软件开发中有时候为了完成一项较为复杂的功能,一个客户类需要和多个业务类交互,由于涉及的类比较多,导致使用代码较为复杂,此时特别需要一个类似服务员的角色,有它来负责和多个业务类进行交互,而客户类只需要与该类交互。

# 定义

为子系统中的一组接口提供一个统一的入口。外观模式定义了一个高层接口,这个接口使得子系统更加容易使用。

# 外观模式结构

外观模式包含以下两个角色:

外观角色(Facade) :在客户端可以调用它的方法,在外观角色中可以知道相关的子系统的功能和责任。

子系统角色(SubSystem):在软件系统中可以有一个或者多个子系统角色,每一个子系统可以不是一个单独的类,而是一个类的集合,实现子系统的功能。

image-20220506123122220

# 外观模式应用案例

image-20220506124533801

1、创建一个图形接口

public interface Shape {
    void draw();
}

2、创建三个基本图形

public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Shape: Circle");
    }
}

public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Shape: Rectangle");
    }
}

public class Square implements Shape{
    @Override
    public void draw() {
        System.out.println("Square::draw()");
    }
}

3、创建这些图形的外观类

public class ShapeMaker {
    private final Shape circle;
    private final Shape rectangle;
    private final Shape square;

    public ShapeMaker() {
        circle = new Circle();
        rectangle = new Rectangle();
        square = new Square();
    }

    public void drawCircle() {
        circle.draw();
    }

    public void drawRectangle() {
        rectangle.draw();
    }

    public void drawSquare() {
        square.draw();
    }
}

4、案例实现

public class FacadePatternDemo {
    public static void main(String[] args) {
        ShapeMaker shapeMaker = new ShapeMaker();

        shapeMaker.drawCircle();
        shapeMaker.drawRectangle();
        shapeMaker.drawSquare();
    }
}

// 输出
Shape: Circle
Shape: Rectangle
Square::draw()

# 外观模式优缺点

优点:

  1. 它对客户端屏蔽了子系统组件,减少了客户端所需处理的对象数目,并使子系统使用更加容易。
  2. 实现了子系统与客户端之间的松耦合关系,使得子系统的变化不会影响到调用它的客户端。
  3. 一个子系统的修改对其他子系统没有任何影响,而且子系统内部变化也不会影响到外观对象。

缺点:

  1. 不能很好地限制客户端直接调用子系统类,如果对客户端访问子系统类做太多的限制减少了可变性和灵活性。
  2. 如果设计不当,增加新的子系统可能需要额外修改外观类的源代码。

# 外观模式使用环境

  1. 为复杂的模块或子系统提供外界访问的模块。

  2. 子系统相对独立。

  3. 预防低水平人员带来的风险。

# 参考

设计模式入门实践 (opens new window)

Java设计模式

#设计模式
上次更新: 2024/06/29, 15:13:44
设计模式-组合模式
设计模式-装饰模式

← 设计模式-组合模式 设计模式-装饰模式→

最近更新
01
基础概念
10-31
02
Pytorch
10-30
03
Numpy
10-30
更多文章>
Theme by Vdoing | Copyright © 2021-2024 旭日 | 蜀ICP备2021000788号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式