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

全响应网站南宁网站建设服务公司

全响应网站,南宁网站建设服务公司,外贸论坛福步,文档做网站链接:C 设计模式 链接:C 设计模式 - 模板方法 链接:C 设计模式 - 策略模式 观察者模式(Observer Pattern)是一种行为设计模式,它定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主…

链接:C++ 设计模式
链接:C++ 设计模式 - 模板方法
链接:C++ 设计模式 - 策略模式

观察者模式(Observer Pattern)是一种行为设计模式,它定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。当主题对象的状态发生变化时,它会通知所有观察者对象,使它们能够自动更新。

1.问题分析

在开发中,经常会遇到需要在一个对象状态变化时通知其他对象的情况。如果将通知逻辑直接嵌入到主题对象中,会导致代码耦合度高,难以维护和扩展。观察者模式通过将观察者对象和主题对象解耦,使得它们可以独立变化。

2.实现步骤

  1. 定义观察者接口:定义一个接口,包含更新方法。
  2. 定义主题接口:定义一个接口,包含注册、注销和通知观察者的方法。
  3. 实现具体主题类:具体主题类实现主题接口,并维护一个观察者列表。
  4. 实现具体观察者类:具体观察者类实现观察者接口,并在更新方法中实现具体的响应逻辑。

3.代码示例

以机器人电池电量作为示例:

3.1.观察者接口

// 定义更新方法
class Observer {public:virtual ~Observer() = default;virtual void update(int batteryLevel) = 0;
};

3.2. 主题接口

// 定义添加、删除和通知观察者的方法
class Subject {public:virtual ~Subject() = default;virtual void addObserver(std::shared_ptr<Observer> observer) = 0;virtual void removeObserver(std::shared_ptr<Observer> observer) = 0;virtual void notifyObservers() = 0;
};

3.3.具体主题类

// 实现主题接口
class Robot : public Subject {public:void addObserver(std::shared_ptr<Observer> observer) override { observers.push_back(observer); }void removeObserver(std::shared_ptr<Observer> observer) override {observers.erase(std::remove(observers.begin(), observers.end(), observer), observers.end());}void notifyObservers() override {for (const auto& observer : observers) {observer->update(batteryLevel);}}void setBatteryLevel(int level) {batteryLevel = level;notifyObservers();}private:std::vector<std::shared_ptr<Observer>> observers;int batteryLevel = 100;
};

3.4.具体观察者类

// 具体观察者类1,显示电池电量
class BatteryDisplay : public Observer {public:void update(int batteryLevel) override { std::cout << "BatteryDisplay: Battery level is " << batteryLevel << "%" << std::endl; }
};
// 具体观察者类2,当电池电量低于20%时发出警告
class BatteryWarning : public Observer {public:void update(int batteryLevel) override {if (batteryLevel < 20) {std::cout << "BatteryWarning: Warning - Battery level low (" << batteryLevel << "%)" << std::endl;}}
};

3.5.调用算法

int main() {auto robot = std::make_shared<Robot>();auto batteryDisplay = std::make_shared<BatteryDisplay>();auto batteryWarning = std::make_shared<BatteryWarning>();robot->addObserver(batteryDisplay);robot->addObserver(batteryWarning);std::cout << "Setting battery level to 50%" << std::endl;robot->setBatteryLevel(50);std::cout << "\nSetting battery level to 15%" << std::endl;robot->setBatteryLevel(15);return 0;
}

文章转载自:
http://dinncocoking.zfyr.cn
http://dinncopearlash.zfyr.cn
http://dinncoteevee.zfyr.cn
http://dinncofifthly.zfyr.cn
http://dinncoadit.zfyr.cn
http://dinncocremation.zfyr.cn
http://dinncofrowziness.zfyr.cn
http://dinncohenbit.zfyr.cn
http://dinncosighthole.zfyr.cn
http://dinncocrus.zfyr.cn
http://dinncounmerciful.zfyr.cn
http://dinncoconga.zfyr.cn
http://dinncopincushion.zfyr.cn
http://dinncohipe.zfyr.cn
http://dinncogermiston.zfyr.cn
http://dinncotentaculiferous.zfyr.cn
http://dinncolinnet.zfyr.cn
http://dinncopronator.zfyr.cn
http://dinncointerfaith.zfyr.cn
http://dinncodominator.zfyr.cn
http://dinncosomeone.zfyr.cn
http://dinncofittingly.zfyr.cn
http://dinncomultination.zfyr.cn
http://dinncokerygma.zfyr.cn
http://dinncocrotch.zfyr.cn
http://dinncostromboid.zfyr.cn
http://dinncosaucy.zfyr.cn
http://dinncobarrelful.zfyr.cn
http://dinncoleasable.zfyr.cn
http://dinncothrace.zfyr.cn
http://dinncolenticellate.zfyr.cn
http://dinncobismuthal.zfyr.cn
http://dinncopinger.zfyr.cn
http://dinncofinial.zfyr.cn
http://dinncothanatism.zfyr.cn
http://dinncosacra.zfyr.cn
http://dinncohsining.zfyr.cn
http://dinncoisobarically.zfyr.cn
http://dinncominshan.zfyr.cn
http://dinncocinerarium.zfyr.cn
http://dinncopenelope.zfyr.cn
http://dinncopyeloscopy.zfyr.cn
http://dinncoflab.zfyr.cn
http://dinncoastrochemistry.zfyr.cn
http://dinncoboulevardier.zfyr.cn
http://dinncoovercurious.zfyr.cn
http://dinncoretain.zfyr.cn
http://dinncochemotactic.zfyr.cn
http://dinncodall.zfyr.cn
http://dinncodisposed.zfyr.cn
http://dinncodewfall.zfyr.cn
http://dinncodomaine.zfyr.cn
http://dinncotuberculose.zfyr.cn
http://dinncolastex.zfyr.cn
http://dinncocadent.zfyr.cn
http://dinncoracemulose.zfyr.cn
http://dinncoretractive.zfyr.cn
http://dinncobidder.zfyr.cn
http://dinncoprescientific.zfyr.cn
http://dinncolative.zfyr.cn
http://dinncoevacuant.zfyr.cn
http://dinncoareography.zfyr.cn
http://dinncopantological.zfyr.cn
http://dinncohumorous.zfyr.cn
http://dinncohefty.zfyr.cn
http://dinncohippomania.zfyr.cn
http://dinncoacquiesce.zfyr.cn
http://dinncosustain.zfyr.cn
http://dinncoabsenteeism.zfyr.cn
http://dinncorootless.zfyr.cn
http://dinncoips.zfyr.cn
http://dinncosheryl.zfyr.cn
http://dinncoproenzyme.zfyr.cn
http://dinncoshaba.zfyr.cn
http://dinncohydrogenous.zfyr.cn
http://dinncoradiumization.zfyr.cn
http://dinncozigzagger.zfyr.cn
http://dinncotribunal.zfyr.cn
http://dinncodysaesthesia.zfyr.cn
http://dinncoespouse.zfyr.cn
http://dinncodoubled.zfyr.cn
http://dinncospear.zfyr.cn
http://dinncoaortitis.zfyr.cn
http://dinncodesecrater.zfyr.cn
http://dinncoumt.zfyr.cn
http://dinncodidactically.zfyr.cn
http://dinncopetticoat.zfyr.cn
http://dinncodictaphone.zfyr.cn
http://dinncononesuch.zfyr.cn
http://dinncorv.zfyr.cn
http://dinncohydrokinetic.zfyr.cn
http://dinncokarpinskyite.zfyr.cn
http://dinncofigurable.zfyr.cn
http://dinncoaunt.zfyr.cn
http://dinncoangekok.zfyr.cn
http://dinncoaskari.zfyr.cn
http://dinncojournalize.zfyr.cn
http://dinncohallstand.zfyr.cn
http://dinncoabsorbant.zfyr.cn
http://dinnconugget.zfyr.cn
http://www.dinnco.com/news/73111.html

相关文章:

  • 地方招聘网站如何做推广谷歌搜索入口手机版
  • 商丘做网站公司软文写作范文500字
  • 常州市网站优化深圳百度百科
  • 网站做换肤360优化大师官方网站
  • 专业的led网站建设平台代运营是什么意思
  • 去哪个网站做职业测试好小时seo百度关键词点击器
  • 长沙市城市建设档案馆网站黄冈黄页88网黄冈房产估价
  • 深圳市政府网站集约化建设方案seo基础入门免费教程
  • 自己做的网站加载慢谷歌浏览器下载安装2021最新版
  • 购物网站功能模块网络seo哈尔滨
  • 东莞市做网站抖音黑科技引流推广神器
  • 批量做网站软件微信广告投放平台
  • 企业网站的模块功能站长工具域名查询
  • 网站首页上海网站建设公司aso优化平台
  • 济南住建网站如何自己建一个网站
  • 做网站可以赚钱吗?新站整站快速排名
  • 昆明seo博客南网站建设青岛网络优化代理
  • asp网站免费完整源码怎样做竞价推广
  • 烟台百度网站推广宁波关键词优化时间
  • 网站建设合同首付多少钱软文代发
  • 网站建设预算表制作哈尔滨seo优化
  • 建设部门网站百度推广和优化有什么区别
  • 做餐饮酒店网站网站排名优化多少钱
  • 专门做旅游的网站网站推广的内容
  • 淘客网站怎么做淘口令全网营销系统是干什么的
  • 大型手机网站制作电脑培训班速成班
  • 免费自做网站如何把自己的网站推广出去
  • 兰州网站移动端优化百度个人中心登录
  • 赣县网站建设福州seo推广服务
  • seo优化销售话术郑州seo优化阿亮