Go和Java实现外观模式

Go和Java实现外观模式

下面我们通过一个构造各种形状的案例来说明外观模式的使用。

1、外观模式

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

模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。

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

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

    更加容易使用。

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

  • 何时使用:1、客户端不需要知道系统内部的复杂联系,整个系统只需提供一个"接待员"即可。 2、定义系统

    的入口。

  • 如何解决:客户端不与系统耦合,外观类与系统耦合。

  • 关键代码:在客户端和复杂系统之间再加一层,这一层将调用顺序、依赖关系等处理好。

  • 应用实例:1、去医院看病,可能要去挂号、门诊、划价、取药,让患者或患者家属觉得很复杂,如果有提供

    接待人员,只让接待人员来处理,就很方便。 2、JAVA 的三层开发模式。

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

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

  • 使用场景:1、为复杂的模块或子系统提供外界访问的模块。 2、子系统相对独立。 3、预防低水平人员带来

    的风险。

  • 注意事项:在层次化结构中,可以使用外观模式定义系统中每一层的入口。

  • 适用性:

    当你要为一个复杂子系统提供一个简单接口时,子系统往往因为不断演化而变得越来越复杂。大多数模式使用

    时都会产生更多更小的类,这使得子系统更具可重用性,也更容易对子系统进行定制,但这也给那些不需要定

    制子系统的用户带来一些使用上的困难。Facade 可以提供一个简单的缺省视图,这一视图对大多数用户来说

    已经足够,而那些需要更多的可定制性的用户可以越过 facade 层。

    客户程序与抽象类的实现部分之间存在着很大的依赖性,引入 facade 将这个子系统与客户以及其他的子系统

    分离,可以提高子系统的独立性和可移植性。

    当你需要构建一个层次结构的子系统时,使用 facade 模式定义子系统中每层的入口点,如果子系统之间是相

    互依赖的,你可以让它们仅通过 facade 进行通讯,从而简化了它们之间的依赖关系。

2、Go实现外观模式

go 复制代码
package facade

//  ========== Shape ==========
type Shape interface {
	draw()
}
go 复制代码
package facade

import "fmt"

// ========== Circle ==========
type Circle struct {
}

func (circle *Circle) draw() {
	fmt.Println("Circle::draw()")
}
go 复制代码
package facade

import "fmt"

// ========== Rectangle ==========
type Rectangle struct {
}

func (rectangle *Rectangle) draw() {
	fmt.Println("Rectangle::draw()")
}
go 复制代码
package facade

import "fmt"

// ========== Square ==========
type Square struct {
}

func (Square *Square) draw() {
	fmt.Println("Square::draw()")
}
go 复制代码
package facade

// ========== ShapeFacade ==========
type ShapeFacade struct {
	circle    Circle
	rectangle Rectangle
	square    Square
}

func NewShapeFacade() ShapeFacade {
	return ShapeFacade{
		circle:    Circle{},
		rectangle: Rectangle{},
		square:    Square{},
	}
}

func (shapeFacade *ShapeFacade) DrawCircle() {
	shapeFacade.circle.draw()
}

func (shapeFacade *ShapeFacade) DrawRectangle() {
	shapeFacade.rectangle.draw()
}

func (shapeFacade *ShapeFacade) DrawSquare() {
	shapeFacade.square.draw()
}
shell 复制代码
# 输出
Circle::draw()
Rectangle::draw()
Square::draw()

3、Java实现外观模式

java 复制代码
package facade;

//  ========== Shape ==========
public interface Shape {
    void draw();
}
java 复制代码
package facade;

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

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

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

// ========== ShapeFacade ==========
public class ShapeFacade {

    private Shape circle;
    private Shape rectangle;
    private Shape square;

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

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

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

    public void drawSquare(){
        square.draw();
    }
}
java 复制代码
package facade;

public class Test {
    public static void main(String[] args) {
        ShapeFacade shapeFacade = new ShapeFacade();
        shapeFacade.drawCircle();
        shapeFacade.drawRectangle();
        shapeFacade.drawSquare();
    }
}
shell 复制代码
# 输出
Circle::draw()
Rectangle::draw()
Square::draw()
相关推荐
我爱挣钱我也要早睡!8 分钟前
Java 复习笔记
java·开发语言·笔记
AD钙奶-lalala2 小时前
Mac OS上搭建 http server
java
是誰萆微了承諾6 小时前
【golang学习笔记 gin 】1.2 redis 的使用
笔记·学习·golang
皮皮林5516 小时前
SpringBoot 全局/局部双模式 Gzip 压缩实战:14MB GeoJSON 秒变 3MB
java·spring boot
weixin_456904276 小时前
Spring Boot 用户管理系统
java·spring boot·后端
趁你还年轻_6 小时前
异步编程CompletionService
java
DKPT6 小时前
Java内存区域与内存溢出
java·开发语言·jvm·笔记·学习
sibylyue6 小时前
Guava中常用的工具类
java·guava
奔跑吧邓邓子6 小时前
【Java实战㉞】从0到1:Spring Boot Web开发与接口设计实战
java·spring boot·实战·web开发·接口设计
专注API从业者7 小时前
Python/Java 代码示例:手把手教程调用 1688 API 获取商品详情实时数据
java·linux·数据库·python