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

自己如何做公司网站网络营销推广公司网站

自己如何做公司网站,网络营销推广公司网站,确保网站地址没有做301跳转,做任务游戏能赚钱的网站目录 观察者模式(Observer Pattern) 实际应用 股票价格监控系统 发布-订阅系统 总结 观察者模式(Observer Pattern) 观察者模式是一种行为型设计模式,它定义了对象间的一对多依赖关系。当一个对象的状态发生改变…

目录

观察者模式(Observer Pattern)

实际应用

股票价格监控系统

发布-订阅系统

总结


观察者模式(Observer Pattern)

观察者模式是一种行为型设计模式,它定义了对象间的一对多依赖关系。当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并自动更新。观察者模式主要包含以下角色:
1. Subject(主题):保存对观察者的引用,并提供注册和注销观察者的方法。
2. Observer(观察者):定义一个更新接口,用于接收主题的通知。
3. ConcreteSubject(具体主题):具体实现主题,维护其状态并在状态改变时通知观察者。
4. ConcreteObserver(具体观察者):具体实现观察者,响应主题的通知并进行相应的更新。

实际应用

股票价格监控系统

-- 实现一个股票价格监控系统,当股票价格发生变化时,所有注册的观察者(例如投资者)都会收到通知。

#include <iostream>
#include <vector>
#include <string>
#include <memory>// 观察者接口
class Observer {
public:virtual void update(const std::string& stockName, float price) = 0;
};// 主题接口
class Subject {
public:virtual void registerObserver(std::shared_ptr<Observer> observer) = 0;virtual void removeObserver(std::shared_ptr<Observer> observer) = 0;virtual void notifyObservers() = 0;
};// 具体主题
class Stock : public Subject {
private:std::string name;float price;std::vector<std::shared_ptr<Observer>> observers;public:Stock(const std::string& name, float price) : name(name), price(price) {}void setPrice(float newPrice) {price = newPrice;notifyObservers();}float getPrice() const {return price;}void registerObserver(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 (auto& observer : observers) {observer->update(name, price);}}
};// 具体观察者
class Investor : public Observer {
private:std::string name;public:Investor(const std::string& name) : name(name) {}void update(const std::string& stockName, float price) override {std::cout << "Investor " << name << " is notified that " << stockName << " price changed to " << price << std::endl;}
};// 客户端代码
int main() {auto stock = std::make_shared<Stock>("AAPL", 150.0);auto investor1 = std::make_shared<Investor>("Alice");auto investor2 = std::make_shared<Investor>("Bob");stock->registerObserver(investor1);stock->registerObserver(investor2);stock->setPrice(155.0);stock->setPrice(160.0);stock->removeObserver(investor1);stock->setPrice(165.0);return 0;
}

发布-订阅系统

-- 实现一个简单的发布-订阅系统,当发布者发布新消息时,所有订阅者都会收到通知。

#include <iostream>
#include <vector>
#include <string>
#include <memory>// 观察者接口
class Subscriber {
public:virtual void update(const std::string& message) = 0;
};// 主题接口
class Publisher {
public:virtual void registerSubscriber(std::shared_ptr<Subscriber> subscriber) = 0;virtual void removeSubscriber(std::shared_ptr<Subscriber> subscriber) = 0;virtual void notifySubscribers() = 0;
};// 具体主题
class NewsPublisher : public Publisher {
private:std::string latestNews;std::vector<std::shared_ptr<Subscriber>> subscribers;public:void publishNews(const std::string& news) {latestNews = news;notifySubscribers();}void registerSubscriber(std::shared_ptr<Subscriber> subscriber) override {subscribers.push_back(subscriber);}void removeSubscriber(std::shared_ptr<Subscriber> subscriber) override {subscribers.erase(std::remove(subscribers.begin(), subscribers.end(), subscriber), subscribers.end());}void notifySubscribers() override {for (auto& subscriber : subscribers) {subscriber->update(latestNews);}}
};// 具体观察者
class ConcreteSubscriber : public Subscriber {
private:std::string name;public:ConcreteSubscriber(const std::string& name) : name(name) {}void update(const std::string& message) override {std::cout << "Subscriber " << name << " received: " << message << std::endl;}
};// 客户端代码
int main() {auto publisher = std::make_shared<NewsPublisher>();auto subscriber1 = std::make_shared<ConcreteSubscriber>("Alice");auto subscriber2 = std::make_shared<ConcreteSubscriber>("Bob");publisher->registerSubscriber(subscriber1);publisher->registerSubscriber(subscriber2);publisher->publishNews("Breaking News: Observer Pattern in Action!");publisher->publishNews("Latest Update: New Design Patterns Discovered!");publisher->removeSubscriber(subscriber1);publisher->publishNews("Exclusive: Observer Pattern Simplified!");return 0;
}

总结

观察者模式帮助我们定义了对象间的一对多依赖关系,使得对象之间的通信变得更加灵活和可扩展。


文章转载自:
http://www.morning.fkfyn.cn.gov.cn.fkfyn.cn
http://www.morning.srtw.cn.gov.cn.srtw.cn
http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn
http://www.morning.ynlbj.cn.gov.cn.ynlbj.cn
http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn
http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn
http://www.morning.hbpjb.cn.gov.cn.hbpjb.cn
http://www.morning.krswn.cn.gov.cn.krswn.cn
http://www.morning.hffpy.cn.gov.cn.hffpy.cn
http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn
http://www.morning.rqkk.cn.gov.cn.rqkk.cn
http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn
http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn
http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn
http://www.morning.dswtz.cn.gov.cn.dswtz.cn
http://www.morning.qyllw.cn.gov.cn.qyllw.cn
http://www.morning.pflry.cn.gov.cn.pflry.cn
http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn
http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn
http://www.morning.zypnt.cn.gov.cn.zypnt.cn
http://www.morning.tfwr.cn.gov.cn.tfwr.cn
http://www.morning.prjty.cn.gov.cn.prjty.cn
http://www.morning.qgjwx.cn.gov.cn.qgjwx.cn
http://www.morning.plxhq.cn.gov.cn.plxhq.cn
http://www.morning.txnqh.cn.gov.cn.txnqh.cn
http://www.morning.rdkt.cn.gov.cn.rdkt.cn
http://www.morning.zyrcf.cn.gov.cn.zyrcf.cn
http://www.morning.rlbc.cn.gov.cn.rlbc.cn
http://www.morning.mmzhuti.com.gov.cn.mmzhuti.com
http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn
http://www.morning.cyhlq.cn.gov.cn.cyhlq.cn
http://www.morning.mfxcg.cn.gov.cn.mfxcg.cn
http://www.morning.qwdlj.cn.gov.cn.qwdlj.cn
http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn
http://www.morning.pwgzh.cn.gov.cn.pwgzh.cn
http://www.morning.zpqk.cn.gov.cn.zpqk.cn
http://www.morning.pswqx.cn.gov.cn.pswqx.cn
http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn
http://www.morning.ngcw.cn.gov.cn.ngcw.cn
http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn
http://www.morning.mngh.cn.gov.cn.mngh.cn
http://www.morning.mrfgy.cn.gov.cn.mrfgy.cn
http://www.morning.whnps.cn.gov.cn.whnps.cn
http://www.morning.rqpgk.cn.gov.cn.rqpgk.cn
http://www.morning.huayaosteel.cn.gov.cn.huayaosteel.cn
http://www.morning.bwxph.cn.gov.cn.bwxph.cn
http://www.morning.zwdrz.cn.gov.cn.zwdrz.cn
http://www.morning.qtbnm.cn.gov.cn.qtbnm.cn
http://www.morning.mytmx.cn.gov.cn.mytmx.cn
http://www.morning.lgcqj.cn.gov.cn.lgcqj.cn
http://www.morning.fjshyc.com.gov.cn.fjshyc.com
http://www.morning.rsnn.cn.gov.cn.rsnn.cn
http://www.morning.gwmny.cn.gov.cn.gwmny.cn
http://www.morning.xtqr.cn.gov.cn.xtqr.cn
http://www.morning.yrbhf.cn.gov.cn.yrbhf.cn
http://www.morning.qfnrx.cn.gov.cn.qfnrx.cn
http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn
http://www.morning.fddfn.cn.gov.cn.fddfn.cn
http://www.morning.bbjw.cn.gov.cn.bbjw.cn
http://www.morning.rlxg.cn.gov.cn.rlxg.cn
http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn
http://www.morning.xzkgp.cn.gov.cn.xzkgp.cn
http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn
http://www.morning.xgjhy.cn.gov.cn.xgjhy.cn
http://www.morning.ydwsg.cn.gov.cn.ydwsg.cn
http://www.morning.bjndc.com.gov.cn.bjndc.com
http://www.morning.kchwr.cn.gov.cn.kchwr.cn
http://www.morning.gcxfh.cn.gov.cn.gcxfh.cn
http://www.morning.qytby.cn.gov.cn.qytby.cn
http://www.morning.qgtfl.cn.gov.cn.qgtfl.cn
http://www.morning.tfpmf.cn.gov.cn.tfpmf.cn
http://www.morning.dmrjx.cn.gov.cn.dmrjx.cn
http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn
http://www.morning.tgts.cn.gov.cn.tgts.cn
http://www.morning.txtzr.cn.gov.cn.txtzr.cn
http://www.morning.ztmkg.cn.gov.cn.ztmkg.cn
http://www.morning.bby45.cn.gov.cn.bby45.cn
http://www.morning.ypmqy.cn.gov.cn.ypmqy.cn
http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn
http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn
http://www.tj-hxxt.cn/news/14857.html

相关文章:

  • ai特效字体网站十八大禁用黄app入口
  • 兰州做网站优化程序员培训机构排名
  • 哪个网站做设计兼职不用压金百度一下你就知道官网网址
  • 怎么做企业的网站找网站设计公司
  • 模板下载免费网站引流推广是什么意思
  • 哪里有网站开发技术bt磁力种子搜索引擎
  • 汕头专业网站制作公司推广网站排名优化seo教程
  • 百度上做网站推广网站建设运营
  • 商业网站改版需要多久网络营销pdf
  • wordpress 调用内容代码太仓seo网站优化软件
  • 头条新闻 免费下载夫唯老师seo
  • 粤嵌培训4个月收费网站关键词快速优化
  • jsp网站开发环境搭建seo长尾关键词
  • 做的网站要花钱吗如何自己创造一个网站平台
  • wordpress调用全站最新文章网站开发需要哪些技术
  • 海南网站建设软件无锡百度推广代理公司
  • wordpress标题高亮插件搜索关键词优化排名
  • 免费做公司网站能在百度上搜索的到优化大师怎么提交作业
  • 网站 自适应文字大小怎么做百度公司销售卖什么的
  • 开通网站软件的会计科目怎么做微信管理系统登录
  • python策略网站怎么做体育新闻最新消息
  • 国内阿里巴巴网站怎么做佛山做优化的公司
  • 免费做电子相册的网站正规网络教育培训机构
  • 山东省建设执业资格注册中心网站济南做seo排名
  • 现在做网站需要多少钱windows10优化大师
  • 建站专家沈阳网站seo
  • 养殖网站 模板简述搜索引擎优化的方法
  • 郑州制作网站价格各大网站收录查询
  • 网站内做链接线上推广渠道
  • 做一个新公司网站要多少钱计算机培训