外观模式 rust和java的实现

文章目录

外观模式

外观模式(Facade Pattern)隐藏系统的复杂性,它为子系统中的一组接口提供一个统一的高层接口,使得这些接口更加容易使用。外观模式通过封装子系统内部的复杂性,提供一个简单的接口,使得外部调用者无需了解子系统内部的处理细节,就可以完成复杂的操作。

举个例子 :就像电脑的usb接口,自己内部实现了复杂的usb协议,自己却只提供了接口,让我们能够即插即用向我们屏蔽了,底层协议的细节。

介绍

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

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

应用实例

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

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

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

实现

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

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

外观模式的 UML 图

java

步骤 1

创建一个接口。

Shape.java

java 复制代码
public interface Shape {
   void draw();
}

步骤 2

创建实现接口的实体类。

Rectangle.java

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

Square.java

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

Circle.java

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

步骤 3

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

ShapeMaker.java

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();
   }
}

步骤 4

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

java 复制代码
FacadePatternDemo.java
public class FacadePatternDemo {
   public static void main(String[] args) {
      ShapeMaker shapeMaker = new ShapeMaker();
 
      shapeMaker.drawCircle();
      shapeMaker.drawRectangle();
      shapeMaker.drawSquare();      
   }
}

步骤 5

执行程序,输出结果:

shell 复制代码
Circle::draw()
Rectangle::draw()
Square::draw()

rust

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

rs 复制代码
// 创建形状接口
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();
}

rust仓库

https://github.com/onenewcode/design.git

本教程项目在bin文件夹下的facade.rs文件中

相关推荐
zhangxingchao10 小时前
AI 大模型核心六:量化、Workflow 与 Agent、多轮 RAG
前端·人工智能·后端
_日拱一卒10 小时前
LeetCode:207课程表
java·数据结构·算法·leetcode·职场和发展
飞翔中文网10 小时前
Java学习笔记之抽象类与接口(设计思想)
java·笔记·学习
雪的季节11 小时前
企业级 Qt 全功能项目
开发语言·数据库·qt
qcx2311 小时前
【系统学AI】09 Multi-Agent架构(2026版):从学术理论到工业级实践
java·人工智能·架构·multi-agent·claude agent
IT_陈寒11 小时前
Vite打包时遇到的坑,原来问题出在这里
前端·人工智能·后端
智者知已应修善业11 小时前
【proteus设计文氏正弦波信号发生器】2023-5-9
驱动开发·经验分享·笔记·硬件架构·proteus·硬件工程
半旧夜夏11 小时前
【保姆级】微服务组件环境搭建(Docker Compose版)
java·linux·spring cloud·微服务·云原生·容器
代龙涛11 小时前
WordPress page.php 页面模板与自定义模板使用方法
android·开发语言·php
bigfootyazi11 小时前
python爬虫-基本库-urllib库(常用速查)
开发语言·爬虫·python