IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> 设计模式之 Mediator 中介模式:Swift 实现 -> 正文阅读

[移动开发]设计模式之 Mediator 中介模式:Swift 实现

Mediator Mode 中介者模式

提起中介,我们常想到,中介是使用自己手上的人脉资源,构建起被介绍人之间一对一的协作的;同时他手上的许多人脉之间通常是不能直接沟通的,想要沟通,是找中介来帮忙,把自己需求告诉中介,让中介找合适的人来做事情。同样的,设计模式中的中介者模式有类似的性质。

对于中介者模式,有以下的描述:

The mediator constrains the interaction between the component, the components can only notify the mediator and the mediator can manage the action of all the components it contains.

中介者限制了组件之间的交互,组件只能通知中介一些指令,然后中介者管理它内部拥有的所有组件的数据操作与行为。

中介者持有多个发送者的引用(预先注册好),如下图的 a,b,c,发送者(在需要的时候)只调用相应的通知方法来通知中介者,中介者协调各方关系从而执行动作。
在这里插入图片描述

// Component basic class
class Component {
    var m: Mediator
    init(mediator: Mediator) {
        self.m = mediator
        m.notify(component: self)
    }
}

// Concrete component
class Button: Component {
    func click() {
        m.notify(component: self)
    }
}
// Concrete component
class CheckBox: Component {
    var check = false
    func isCheck() -> Bool {
        return check
    }
}
// Concrete component
class PasswordText: Component {
    var text: String = ""
    func getText() -> String{
        return text
    }
    
}
// Concrete component
class UserNameText: Component {
    var text: String = ""
    func getText() -> String{
        return text
    }
    
}

protocol Mediator {
    func notify(component: Component)
}

// Concrete mediator
class LoginMediator: Mediator {
    var btnLogin: Button?
    var tickSavePassword: CheckBox?
    var password: PasswordText?
    var userName: UserNameText?
    func notify(component: Component) {
        if component is Button {
            var btn = component as! Button
            if self.btnLogin == nil {
                // self btn initialize
                self.btnLogin = btn
                return 
            }
            
            if (canLogin(userName: userName?.getText(), password: password?.getText())) {
                print("Login success")
                if let wantSave = tickSavePassword?.isCheck() {
                    // save the userName and password
                }
            } else {
                print("Login failed")
            }
        } else if component is CheckBox {
            var box = component as! CheckBox
            self.tickSavePassword = box
        } else if component is UserNameText {
            var userName = component as! UserNameText
            self.userName = userName
        } else if component is PasswordText {
            var password = component as! PasswordText 
            self.password = password
        }
        
    }
}

// The function check whether the username and password match
func canLogin(userName: String?, password: String?) -> Bool {
    if userName == nil || password == nil {
        return false
    }
    if let name = userName, let word = password{
        if name == "hello" && word == "Mike" {
            return true
        } else {
            return false
        }
    } 
    return false
}

let mediator = LoginMediator()
let userName = UserNameText(mediator: mediator)
let password = PasswordText(mediator: mediator)
let tickSavePassword = CheckBox(mediator: mediator)
let btnLogin = Button(mediator: mediator)

userName.text = "hello"
password.text = "Mike"
tickSavePassword.check = true

btnLogin.click()

以上代码中,多个组件都可能向中介者发送通知,中介者会判断通知的来源,并且做出相应的动作。登录按钮所需要的数据分散在各个组件中,为了集中这些消息,又要不让组件之间互相沟通(这会使得组件之间产生耦合),所以使用中介者,统一收集信息,然后登陆按钮才会发挥作用。

命令模式是一对一的命令,中介者模式是中介者对多个发送者负责并协调各方关系

中介者的作用:收集各个组件的信息,同时不让组件之间耦合。耦合的点,集中在中介者上。

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-10-31 12:09:06  更:2022-10-31 12:12:56 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 23:49:27-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码