我用C语言玩对象,观察者模式应用2-热水的用途

2023-10-13 03:10

本文主要是介绍我用C语言玩对象,观察者模式应用2-热水的用途,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概述

观察者模式让多个观察者对象同时监听某一个主题对象。这个主题对象在状态变化时,会通知所有的观察者对象,使他们能够自动更新自己。

之前的文章已经详细阐述了这种设计模式的核心和注意事项,并完成了基类设计,请参见《C语言 - 观察者模式(基类部分)》,本文将结合实际案例论证这种设计模式,加深读者对观察者模式的理解和应用。

示例

★背景说明:水温在50℃~ 70℃时, 会发出警告:可以用来洗澡了!水温在100℃时也会发出警告:可以用来饮用了!在这里洗澡模式和饮用模式扮演了监听的角色, 而热水器则是被监听的对象。一旦热水器中的水温度发生变化, 监听者就能及时知道并做出相应的判断和动作。这就是程序设计中监听模式的生动展现。

★被观察者对象(热水器):

属性:温度(当前热水器水温)

行为:获取水温、设置水温。

继承:继承被观察者基类

★观察者对象(洗澡模式、喝水模式):

属性:无

行为:对应行为。

★包含头文件water_heater.h和源文件water_heater.c(均已验证通过)。

 water_heater.h

/*** @Filename : water_heater.h* @Revision : $Revision: 1.0 $* @Author : Feng* @Description : 观察者模式应用(C语言模拟C++)* @Explain : 热水器(被观察者)   water_heater洗澡模式(观察者)   washing_mode    50 < 热水器温度 < 70喝水模式(观察者)   drinking_mode   热水器温度 >= 100
**/#ifndef __WATERHEATER_H__
#define __WATERHEATER_H__#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "observer.h"/* 被观察者(热水器)类定义 */
struct water_heater {struct oble oble;   /* 继承被观察者基类 */int temprature;     /* 温度 */  int (*get)(struct water_heater *p_wheater);               /* 获取温度 */void (*set)(struct water_heater *p_wheater, int temp);    /* 设置温度 */
};/*** @创建热水器对象* @temp:温度 * @成功返回类对象,失败返回NULL 
**/
struct water_heater *new_water_heater(int temp);/* 观察者(洗澡模式)类定义 */
struct washing_mode {struct ober ober;
};/* 观察者(喝水模式)类定义 */
struct drinking_mode {struct ober ober;
};/*** @创建洗澡模式对象* @成功返回类对象,失败返回NULL 
**/
struct washing_mode *new_washing_mode(void);/*** @创建喝水模式对象* @成功返回类对象,失败返回NULL 
**/
struct drinking_mode *new_drinking_mode(void);#endif

 water_heater.c

/*** @Filename : water_heater.c* @Revision : $Revision: 1.0 $* @Author : Feng* @Description : 观察者模式应用(C语言模拟C++)* @Explain : 热水器(被观察者)   water_heater洗澡模式(观察者)   washing_mode    50 < 热水器温度 < 70喝水模式(观察者)   drinking_mode   热水器温度 >= 100
**/
#include "water_heater.h"/*** @获取热水器温度* @p_wheater:热水器类* @返回热水器温度 
**/
static int _get_temp(struct water_heater *p_wheater)
{return (p_wheater->temprature);
}/*** @设置热水器温度,并通知观察者* @p_wheater:热水器类      temp:温度 
**/
static void _set_temp(struct water_heater *p_wheater, int temp)
{p_wheater->temprature = temp;printf("current temprature is %d : ", p_wheater->temprature);p_wheater->oble.notify((struct oble *)p_wheater);printf("\n");
} /*** @创建热水器对象* @temp:温度 * @成功返回类对象,失败返回NULL 
**/
struct water_heater *new_water_heater(int temp)
{struct oble *p_ober;struct water_heater *p_wheater;p_wheater = (struct water_heater *)malloc(sizeof(struct water_heater));if (p_wheater == NULL)return NULL;memset((char *)p_wheater, 0, sizeof(struct water_heater));if ((p_ober = new_oble()) == NULL) {free(p_wheater);return NULL;}memcpy(&(p_wheater->oble), p_ober, sizeof(struct oble));free(p_ober);p_wheater->temprature = temp;p_wheater->get = _get_temp;p_wheater->set = _set_temp;return p_wheater;
}/*** @根据水温提示用户是否洗澡* @p_oble:被观察者类(热水器)
**/
static void _update_washing(struct ober *p_ober, struct oble *p_oble)
{struct water_heater *p_wheater = (struct water_heater *)p_oble;if ((p_wheater->get(p_wheater) > 50) && (p_wheater->get(p_wheater) < 70))printf("please washing...");
}/*** @根据水温提示用户是否喝水* @p_oble:被观察者类(热水器) 
**/
static void _update_drinking(struct ober *p_ober, struct oble *p_oble)
{struct water_heater *p_wheater = (struct water_heater *)p_oble;if (p_wheater->get(p_wheater) >= 100)printf("please drinking...");
}/*** @创建洗澡模式对象* @成功返回类对象,失败返回NULL 
**/
struct washing_mode *new_washing_mode(void)
{struct ober *p_ober;struct washing_mode *p_cwashingMode;p_cwashingMode = (struct washing_mode *)malloc(sizeof(struct washing_mode));if (p_cwashingMode == NULL)return NULL;memset((char *)p_cwashingMode, 0, sizeof(struct washing_mode));if ((p_ober = (struct ober *)malloc(sizeof(struct ober))) == NULL) {free(p_cwashingMode);return NULL;}p_ober->update = _update_washing;memcpy(&(p_cwashingMode->ober), p_ober, sizeof(struct ober));free(p_ober);return p_cwashingMode;
}/*** @创建喝水模式对象* @成功返回类对象,失败返回NULL 
**/
struct drinking_mode *new_drinking_mode(void)
{struct ober *p_ober;struct drinking_mode *p_cdrinkingMode;p_cdrinkingMode = (struct drinking_mode *)malloc(sizeof(struct drinking_mode));if (p_cdrinkingMode == NULL)return NULL;memset((char *)p_cdrinkingMode, 0, sizeof(struct drinking_mode));if ((p_ober = (struct ober *)malloc(sizeof(struct ober))) == NULL) {free(p_cdrinkingMode);return NULL;}p_ober->update = _update_drinking;memcpy(&(p_cdrinkingMode->ober), p_ober, sizeof(struct ober));free(p_ober);return p_cdrinkingMode;
}/*** @主函数,演示代码
**/
int main(void)
{   struct washing_mode *p_wash = new_washing_mode();struct drinking_mode *p_drink = new_drinking_mode();struct water_heater *p_water = new_water_heater(25);if ((p_water == NULL) || (p_wash == NULL) || (p_drink == NULL)) {printf("create observable class failed...\n");return -1;}p_water->oble.add(&(p_water->oble), &(p_wash->ober));p_water->oble.add(&(p_water->oble), &(p_drink->ober));/* 50-70 洗澡, >=100喝水 */p_water->set(p_water, 40);p_water->set(p_water, 60);p_water->set(p_water, 65);p_water->set(p_water, 120);  printf("--------------------------\n");  /* 删除喝水,水温等于120°不响应 */p_water->oble.rm(&(p_water->oble), &(p_drink->ober));p_water->set(p_water, 65);p_water->set(p_water, 120);printf("--------------------------\n"); /* 增加喝水,水温等于120°响应 */ p_water->oble.add(&(p_water->oble), &(p_drink->ober));p_water->set(p_water, 65);p_water->set(p_water, 120);return 0;
}

结论

输入示例代码运行,结果如下:

feng:observer$ gcc -o water water_heater.c observer.c class_dll.c dll.c
feng:observer$ ./water
current temprature is 40 : 
current temprature is 60 : please washing...
current temprature is 65 : please washing...
current temprature is 120 : please drinking...
--------------------------
current temprature is 65 : please washing...
current temprature is 120 : 
--------------------------
current temprature is 65 : please washing...
current temprature is 120 : please drinking...
feng:observer$ 

分析:示例定义了热水器对象作为被观察者,同时定义了饮用模式和洗澡模式对象作为观察者,开始的时候,饮用模式和洗澡模式均监听热水器,所以当水温到达模式设定范围内时,自动触发相应的行为。一段时间后饮用模式不再监听热水器,所以无法触发饮水行为,再后来,饮用模式重新监听热水器,当水温到达设定范围时,又会自动触发饮用模式。

往期 · 推荐

浅谈linux - 字符设备框架

帮你自动化办公的python-自动提取pdf指定页(项目概述)

也没想象中那么神秘的数据结构-一种通用化的双向链表设计(底层源码)

也没想象中那么神秘的数据结构-一环扣一环的“链表”(双向链表)

我用C语言玩对象,偷偷关注着你的观察者模式(基类设计)

关注

更多精彩内容,请关注微信公众号:不只会拍照的程序猿,本人致力分享linux、设计模式、C语言、嵌入式、编程相关知识,也会抽空分享些摄影相关内容,同样也分享大量摄影、编程相关视频和源码,另外你若想要本文章源码请关注公众号:不只会拍照的程序猿,后台回复:设计模式源码,也可点击此处下载

这篇关于我用C语言玩对象,观察者模式应用2-热水的用途的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/200376

相关文章

sqlserver、mysql、oracle、pgsql、sqlite五大关系数据库的对象名称和转义字符

《sqlserver、mysql、oracle、pgsql、sqlite五大关系数据库的对象名称和转义字符》:本文主要介绍sqlserver、mysql、oracle、pgsql、sqlite五大... 目录一、转义符1.1 oracle1.2 sqlserver1.3 PostgreSQL1.4 SQLi

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

JavaScript对象转数组的三种方法实现

《JavaScript对象转数组的三种方法实现》本文介绍了在JavaScript中将对象转换为数组的三种实用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友... 目录方法1:使用Object.keys()和Array.map()方法2:使用Object.entr

利用Python操作Word文档页码的实际应用

《利用Python操作Word文档页码的实际应用》在撰写长篇文档时,经常需要将文档分成多个节,每个节都需要单独的页码,下面:本文主要介绍利用Python操作Word文档页码的相关资料,文中通过代码... 目录需求:文档详情:要求:该程序的功能是:总结需求:一次性处理24个文档的页码。文档详情:1、每个

Go语言中json操作的实现

《Go语言中json操作的实现》本文主要介绍了Go语言中的json操作的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 一、jsOChina编程N 与 Go 类型对应关系️ 二、基本操作:编码与解码 三、结构体标签(Struc

使用MapStruct实现Java对象映射的示例代码

《使用MapStruct实现Java对象映射的示例代码》本文主要介绍了使用MapStruct实现Java对象映射的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、什么是 MapStruct?二、实战演练:三步集成 MapStruct第一步:添加 Mave

Java中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例解析

《Java中的分布式系统开发基于Zookeeper与Dubbo的应用案例解析》本文将通过实际案例,带你走进基于Zookeeper与Dubbo的分布式系统开发,本文通过实例代码给大家介绍的非常详... 目录Java 中的分布式系统开发基于 Zookeeper 与 Dubbo 的应用案例一、分布式系统中的挑战二

Java 缓存框架 Caffeine 应用场景解析

《Java缓存框架Caffeine应用场景解析》文章介绍Caffeine作为高性能Java本地缓存框架,基于W-TinyLFU算法,支持异步加载、灵活过期策略、内存安全机制及统计监控,重点解析其... 目录一、Caffeine 简介1. 框架概述1.1 Caffeine的核心优势二、Caffeine 基础2

使用Node.js和PostgreSQL构建数据库应用

《使用Node.js和PostgreSQL构建数据库应用》PostgreSQL是一个功能强大的开源关系型数据库,而Node.js是构建高效网络应用的理想平台,结合这两个技术,我们可以创建出色的数据驱动... 目录初始化项目与安装依赖建立数据库连接执行CRUD操作查询数据插入数据更新数据删除数据完整示例与最佳

python语言中的常用容器(集合)示例详解

《python语言中的常用容器(集合)示例详解》Python集合是一种无序且不重复的数据容器,它可以存储任意类型的对象,包括数字、字符串、元组等,下面:本文主要介绍python语言中常用容器(集合... 目录1.核心内置容器1. 列表2. 元组3. 集合4. 冻结集合5. 字典2.collections模块