0
点赞
收藏
分享

微信扫一扫

Redis 常用的基本命令

目标践行者 19小时前 阅读 0

一、外观模式

外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性

这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用

主要解决

降低访问复杂系统的内部子系统时的复杂度,简化客户端之间的接口

何时使用

1、客户端不需要知道系统内部的复杂联系,整个系统只需提供一个"接待员"即可

2、定义系统的入口

优点:

  • 减少系统相互依赖
  • 提高灵活性
  • 提高了安全性

缺点:

  • 不符合开闭原则,如果要改东西很麻烦,继承重写都不合适

1. 各个角色介绍

1.1 外观(Facade)

  • 提供一个简化的接口,封装了系统的复杂性。外观模式的客户端通过与外观对象交互,而无需直接与系统的各个组件打交道

1.2 子系统(Subsystem)

  • 由多个相互关联的类组成,负责系统的具体功能。外观对象通过调用这些子系统来完成客户端的请求

1.3 客户端(Client)

  • 使用外观对象来与系统交互,而不需要了解系统内部的具体实现

2. UML图

​ 我们将创建一个 Shape 接口和实现了 Shape 接口的实体类。下一步是定义一个外观类 ShapeMakerShapeMaker 类使用实体类来代表用户对这些类的调用。Main 类使用 ShapeMaker 类来显示结果

在这里插入图片描述

3. 具体例子和代码

  • Shape:形状接口

    • Rectangle:实现形状接口的长方形类
    • Circle:实现形状接口的圆形类
    • Square:实现形状接口的正方形
  • ShapeMaker:外观类

3.1 抽象组件及其实现类

  • Shape
package com.vinjcent.prototype.decorator;

/**
 * @author vinjcent
 * @description 形状接口
 * @since 2024/3/15 16:23
 */
public interface Shape {

    /**
     * 形状绘制动作
     */
    void draw();

}

  • Rectangle
package com.vinjcent.prototype.facade;

/**
 * @author vinjcent
 * @description 长方形
 * @since 2024/3/20 16:32
 */
public class Rectangle implements Shape {

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

}

  • Circle
package com.vinjcent.prototype.facade;

/**
 * @author vinjcent
 * @description 圆形
 * @since 2024/3/20 16:36
 */
public class Circle implements Shape {

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

}

  • Square
package com.vinjcent.prototype.facade;

/**
 * @author vinjcent
 * @description 正方形
 * @since 2024/3/20 16:34
 */
public class Square implements Shape {

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

}

3.2 外观类

  • ShapeMaker
package com.vinjcent.prototype.facade;

import io.swagger.annotations.ApiModelProperty;

/**
 * @author vinjcent
 * @description 外观类
 * @since 2024/3/20 16:35
 */
public class ShapeMaker {

    @ApiModelProperty("圆形")
    private final Shape circle;

    @ApiModelProperty("长方形")
    private final Shape rectangle;

    @ApiModelProperty("正方形")
    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();
    }

}

3.3 测试主函数

package com.vinjcent.prototype.facade;

/**
 * @author vinjcent
 * @description 外观模式
 * @since 2024/3/20 16:38
 */
public class Main {

    public static void main(String[] args) {

        // 创建一个"外观类"
        ShapeMaker shapeMaker = new ShapeMaker();

        // 绘制"圆形"
        shapeMaker.drawCircle();

        // 绘制"长方形"
        shapeMaker.drawRectangle();

        // 绘制"正方形"
        shapeMaker.drawSquare();
    }

}

  • 测试结果

在这里插入图片描述

4. 使用场景

  • 为复杂的模块或子系统提供外界访问的模块
  • 子系统相对独立
  • 预防低水平人员带来的风险

在这里插入图片描述

举报

相关推荐

0 条评论