外观模式 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();
}

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

Rectangle.java

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

Square.java

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

Circle.java

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

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

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

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

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

Circle::draw()
Rectangle::draw()
Square::draw()

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

rust仓库

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

过去日记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值