设计模式之 Facade Mode 表面模式: Swift 实现

发布于:2023-01-07 ⋅ 阅读:(589) ⋅ 点赞:(0)

Facade Mode 表面模式

The sub-system has many objects and we want to decoupling them with the client’s calling, so we set a class Facade and use its instance to interact with the sub-system. The Facade just like an inter-media.

一个子系统有许多对象,而我们想要将这些对象与客户端调用解耦合,所以我们设置一个 “外观” 类,使用外观类的实例来与子系统交互。这个外观类实例就像一个处于中间的媒介。
在这里插入图片描述

// Sub-System objects
class VideoFile {
    
}
class BitwiseNot {
    
}

class CvtColor {
    
}

class Transformer {
    
}

// Facade
class VideoConverter {
    func convert() -> File {
        var videoFile = VideoFile()
        var bitwiseNot = BitwiseNot()
        var cvtColor = CvtColor()
        var transformer = Transformer()
        var file = File()
        return file
    }
}
class File {
    
}

let converter = VideoConverter()
var file = converter.convert()

基于现有的基础库,比如我们用到的 OpenCV、OpenGL ,可以理解为它们是一些函数库,我们使用一个中间的工具去实现某一任务,这一工具的主要负责的是对基础库的各种组合调用。这样的一种调用关系,称为表面模式。

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到