当前位置: 首页 > news >正文

【设计模式】中介者模式

概念

行为模式


类图

Mediator


代码

#include <iostream>using namespace std;class Component;
class Mediator {
public:virtual void Notify(Component* sender, const string& event) = 0;
};class Component {
public:explicit Component(Mediator* mediator) {dialog = mediator;}void Click() {dialog->Notify(this, "click");}void KeyPress() {dialog->Notify(this, "keypress");}protected:Mediator* dialog;
};class Button : public Component {};class Textbox : public Component {};class Checkbox : public Component {
public:explicit Checkbox(Mediator* mediator) : Component(mediator) {};void Check() {dialog->Notify(this, "check");}
};class AuthenticationDialog : public Mediator {
public:explicit AuthenticationDialog(const string& dialog) {title = dialog;}void Notify(Component* sender, const string& event) override {cout << "Auth " << event << endl;}private:string title;// Checkbox* loginOrRegisterCheckbox;// Textbox* loginUserName, loginPassword;// Textbox* registrationUsername, registrationPassword, registrationEmail;// Button* okButton, cancelButton;
};int main(int argc, char *argv[]) {auto auth = new AuthenticationDialog("OnBoarding");auto checkbox = new Checkbox(auth);auth->Notify(checkbox, "notify");checkbox->Check();delete checkbox;delete auth;return 0;
}
http://www.dtcms.com/a/394122.html

相关文章:

  • nginx添加modsecurity插件
  • 代码上传Github:SSH法
  • 【iOS】AFNetworking初步了解及使用
  • JVM实战-G1参数调优
  • 超简单的视频分割脚本
  • 基于51单片机电子钟闹钟12/24小时制LCD显示( proteus仿真+程序+设计报告+讲解视频)
  • 在 Windows 系统上安装官方 Codex CLI 教程
  • Redis 配置与优化全攻略
  • 二分查找左右边界写法
  • Oracle体系结构-Large Pool详解
  • Elasticsearch面试精讲 Day 19:磁盘IO与存储优化
  • 【AI智能体】Dify 搭建数据分析应用实战操作详解
  • Nginx localtion / 、/a、/a/ 的区别
  • 【C++】string的使用与模拟实现
  • 新手向 算法 希尔排序-yang
  • 如何用RAG增强的动态能力与大模型结合打造企业AI产品?
  • 黑马头条_SpringCloud项目阶段五:openFeign服务接入以及接入腾讯云内容安全服务实现文章提交违规信息自动审核
  • Spring、SpringBoot框架核心流程详解
  • 195. Java 异常 - finally 块:Java 中的“兜底侠”
  • C语言底层学习(2.指针与数组的关系与应用)(超详细)
  • 008 Rust注释
  • ubuntu防火墙开放端口
  • ​MySQL 8.0.29 RPM 安装教程(CentOS 7 / RHEL 7 详细步骤)​附安装包
  • AIPPT:AI一键生成高质量PPT
  • [已更新]2025华为杯E题数学建模研赛E题研究生数学建模思路代码文章成品:高速列车轴承智能故障诊断问题
  • 从零到一:Vue3 + Spring Boot + MySQL 全栈项目部署到阿里云服务器完整教程
  • 微服务基础2-网关路由
  • ubuntu创建新用户
  • 黑豹X2(Panther-x2)armbian 驱动NPU/VPU的驱动下载安装
  • 50.Mysql主从复制与读写分离