• 外观模式 rust和java的实现


    外观模式

    外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。它向现有的系统添加一个接口,来隐藏系统的复杂性。
    举个例子 :就像电脑的usb接口,自己内部实现了复杂的usb协议,自己却只提供了接口,让我们能够即插即用向我们屏蔽了,底层协议的细节。

    介绍

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

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

    应用实例
    去医院看病,可能要去挂号、门诊、划价、取药,让患者或患者家属觉得很复杂,如果有提供接待人员,只让接待人员来处理,就很方便。

    优点: 1、减少系统相互依赖。 2、提高灵活性。 3、提高了安全性。

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

    实现

    我们将创建一个 Shape 接口和实现了 Shape 接口的实体类。下一步是定义一个外观类 ShapeMaker。 我们采用把所有的实现类封装在shapemaker,由shapemaker提供统一的接口,使我们能够方便调用。

    ShapeMaker 类使用实体类来代表用户对这些类的调用。FacadePatternDemo 类使用 ShapeMaker 类来显示结果。

    外观模式的 UML 图
    在这里插入图片描述

    java

    步骤 1
    创建一个接口。
    Shape.java

    public interface Shape {
       void draw();
    }
    
    • 1
    • 2
    • 3

    步骤 2
    创建实现接口的实体类。

    Rectangle.java

    public class Rectangle implements Shape {
     
       @Override
       public void draw() {
          System.out.println("Rectangle::draw()");
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Square.java

    public class Square implements Shape {
     
       @Override
       public void draw() {
          System.out.println("Square::draw()");
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Circle.java

    public class Circle implements Shape {
     
       @Override
       public void draw() {
          System.out.println("Circle::draw()");
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    步骤 3
    创建一个外观类,这个外观类中,封装装了上述实现类的方法,这样我们就可以通过外观类中提供的方法,间接调用底层继承shape抽象类的实体类实现的方法。

    ShapeMaker.java

    public class ShapeMaker {
       private Shape circle;
       private Shape rectangle;
       private 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();
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    步骤 4
    使用该外观类画出各种类型的形状,由下面的代码我们可以看到,我们可以调用shapemaker的方法间接调用底层实现类的方法。

    FacadePatternDemo.java
    public class FacadePatternDemo {
       public static void main(String[] args) {
          ShapeMaker shapeMaker = new ShapeMaker();
     
          shapeMaker.drawCircle();
          shapeMaker.drawRectangle();
          shapeMaker.drawSquare();      
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    步骤 5
    执行程序,输出结果:

    Circle::draw()
    Rectangle::draw()
    Square::draw()
    
    • 1
    • 2
    • 3

    rust

    rsut实现的大致思路和java相同,就不再赘述过程。

    // 创建形状接口
    trait Shape {
        fn draw(&self);
    }
    struct  Rectangle {}
    struct Circle{}
    struct Square{}
    impl Shape for Rectangle {
        fn draw(&self) {
            println!("Shape: Rectangle");
        }
    }
    impl Shape for Circle {
        fn draw(&self) {
            println!("Shape: Circle");
        }
    }
    impl Shape for Square {
        fn draw(&self) {
            println!("Shape: Square");
        }
    }
    // 创建外观
    struct ShapeMaker{
        rectangle:Rectangle,
        circle:Circle,
        square:Square
    }
    impl ShapeMaker {
        fn draw_rectangle(&self) {
            self.rectangle.draw();
        }
        fn draw_circle(&self) {
            self.circle.draw();
        }
        fn draw_square(&self) {
            self.square.draw();
        }
    }
    fn main() {
        //创建接口实体
        let shape_maker=ShapeMaker{rectangle:Rectangle {  },circle:Circle {  },square:Square {  }};
        // 体现接口抽象实现的各种方法
        shape_maker.draw_circle();
        shape_maker.draw_rectangle();
        shape_maker.draw_square();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    rust仓库

    https://github.com/onenewcode/design.git
    本教程项目在bin文件夹下的facade.rs文件中

  • 相关阅读:
    对HFish蜜罐捕捉到的样本分析
    win7 X64 安装tensorflow 并使用 spyder 教程
    Confluent之Kafka Connector初体验
    amlogic-android9.0-hdmi调试
    nacos源码分析-服务注册(客户端)
    C++ PCL点云曲率分割颜色标识
    通过python 获取当前局域网内存在的IP和MAC
    LabVIEW工业虚拟仪器的标准化实施
    Linux C语言开发-D10控制语句if
    G1D14fraud&git&pipenv&df操作&APT论文&RCE37-40&服务器搭建
  • 原文地址:https://blog.csdn.net/studycodeday/article/details/134443802