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

免费网站制作软件深圳seo优化公司搜索引擎优化方案

免费网站制作软件,深圳seo优化公司搜索引擎优化方案,竞赛网站开发,wordpress error loading this resourceC#设计模式:观察者模式,让对象间通信更优雅 在软件开发中,我们经常会遇到一个对象的状态发生改变,其他对象需要自动更新或做出相应反应的场景。例如: GUI事件处理: 当用户点击按钮时,按钮需要…

C#设计模式:观察者模式,让对象间通信更优雅

在软件开发中,我们经常会遇到一个对象的状态发生改变,其他对象需要自动更新或做出相应反应的场景。例如:

  • GUI事件处理: 当用户点击按钮时,按钮需要通知所有注册的事件处理程序。
  • 股票价格更新: 当股票价格发生变化时,所有关注该股票的投资者都需要收到通知。
  • 气象站数据更新: 当气象站收集到新的气象数据时,所有显示气象数据的设备都需要更新显示。

为了实现这种对象间的通信,我们可以使用观察者模式(Observer Pattern),它定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。当主题对象的状态发生改变时,所有依赖于它的观察者对象都会收到通知并自动更新。

一、观察者模式简介

观察者模式属于行为型设计模式,它主要解决的是对象间的一对多依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会收到通知并自动更新。

二、观察者模式的结构

观察者模式包含四个角色:

  1. Subject(主题): 被观察的对象,它维护一个观察者列表,并提供注册、删除和通知观察者的方法。
  2. Observer(观察者): 定义了一个更新接口,用于接收主题的通知。
  3. ConcreteSubject(具体主题): 具体被观察的对象,它维护自身状态,并在状态改变时通知所有观察者。
  4. ConcreteObserver(具体观察者): 具体的观察者对象,它实现更新接口,并在收到通知时更新自身状态。

三、C# 实现示例

让我们通过一个简单的气象站示例来理解观察者模式:

// 主题接口
interface ISubject
{void RegisterObserver(IObserver observer);void RemoveObserver(IObserver observer);void NotifyObservers();
}// 观察者接口
interface IObserver
{void Update(float temperature, float humidity, float pressure);
}// 具体主题:气象站
class WeatherStation : ISubject
{private List<IObserver> _observers = new List<IObserver>();private float _temperature;private float _humidity;private float _pressure;public void RegisterObserver(IObserver observer){_observers.Add(observer);}public void RemoveObserver(IObserver observer){_observers.Remove(observer);}public void NotifyObservers(){foreach (var observer in _observers){observer.Update(_temperature, _humidity, _pressure);}}public void SetMeasurements(float temperature, float humidity, float pressure){_temperature = temperature;_humidity = humidity;_pressure = pressure;NotifyObservers();}
}// 具体观察者:显示当前天气状况
class CurrentConditionsDisplay : IObserver
{private float _temperature;private float _humidity;public void Update(float temperature, float humidity, float pressure){_temperature = temperature;_humidity = humidity;Display();}public void Display(){Console.WriteLine($"Current conditions: {_temperature}F degrees and {_humidity}% humidity");}
}// 客户端代码
class Program
{static void Main(string[] args){WeatherStation weatherStation = new WeatherStation();CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay();weatherStation.RegisterObserver(currentDisplay);weatherStation.SetMeasurements(80, 65, 30.4f);weatherStation.SetMeasurements(82, 70, 29.2f);weatherStation.SetMeasurements(78, 90, 29.2f);}
}

四、观察者模式的优缺点

优点:

  • 松耦合: 主题和观察者之间是松耦合的,主题不需要知道观察者的具体类,只需要知道观察者实现了更新接口。
  • 可扩展性: 可以方便地增加新的观察者,而不需要修改主题的代码。
  • 支持广播通信: 主题可以一次通知所有观察者。

缺点:

  • 可能导致性能问题: 如果观察者数量很多,通知所有观察者可能会比较耗时。
  • 可能导致内存泄漏: 如果观察者没有正确地从主题中注销,可能会导致内存泄漏。

五、总结

观察者模式提供了一种优雅的方式来实现对象间的一对多依赖关系,它在需要实现事件处理、数据更新等场景时非常有用。但是,在使用观察者模式时,也需要考虑其潜在的性能问题和内存泄漏问题。


希望这篇博客能帮助你更好地理解和使用观察者模式!


文章转载自:
http://aecidiospore.riewr.cn
http://calkage.riewr.cn
http://chaplet.riewr.cn
http://alabastrine.riewr.cn
http://bikeway.riewr.cn
http://chineselantern.riewr.cn
http://begin.riewr.cn
http://amative.riewr.cn
http://campong.riewr.cn
http://actinogram.riewr.cn
http://chargeable.riewr.cn
http://aminophenol.riewr.cn
http://achillean.riewr.cn
http://alar.riewr.cn
http://adytum.riewr.cn
http://barber.riewr.cn
http://archdeaconate.riewr.cn
http://amazedly.riewr.cn
http://brickmaking.riewr.cn
http://ameroenglish.riewr.cn
http://ahriman.riewr.cn
http://avouchment.riewr.cn
http://cansure.riewr.cn
http://ananthous.riewr.cn
http://carposporangium.riewr.cn
http://beccaccia.riewr.cn
http://chibchan.riewr.cn
http://bungalow.riewr.cn
http://abnormality.riewr.cn
http://chest.riewr.cn
http://bioecology.riewr.cn
http://cayuga.riewr.cn
http://birdshot.riewr.cn
http://beachmaster.riewr.cn
http://adversative.riewr.cn
http://browse.riewr.cn
http://acquisitively.riewr.cn
http://atomics.riewr.cn
http://chiliasm.riewr.cn
http://blacky.riewr.cn
http://chloralose.riewr.cn
http://afford.riewr.cn
http://carat.riewr.cn
http://angiocarp.riewr.cn
http://afternoons.riewr.cn
http://carabid.riewr.cn
http://bladework.riewr.cn
http://bah.riewr.cn
http://anabiosis.riewr.cn
http://artilleryman.riewr.cn
http://antitank.riewr.cn
http://atelier.riewr.cn
http://bronchia.riewr.cn
http://armature.riewr.cn
http://acouasm.riewr.cn
http://broadsheet.riewr.cn
http://brooklet.riewr.cn
http://butadiene.riewr.cn
http://adhibit.riewr.cn
http://astrophotometry.riewr.cn
http://bedtiime.riewr.cn
http://berserk.riewr.cn
http://barytes.riewr.cn
http://binit.riewr.cn
http://basketballer.riewr.cn
http://bisulphide.riewr.cn
http://casually.riewr.cn
http://cheliped.riewr.cn
http://antibaryon.riewr.cn
http://cgh.riewr.cn
http://burro.riewr.cn
http://capella.riewr.cn
http://calamary.riewr.cn
http://blithely.riewr.cn
http://brasilia.riewr.cn
http://catholicon.riewr.cn
http://capitula.riewr.cn
http://barytone.riewr.cn
http://chinovnik.riewr.cn
http://abidjan.riewr.cn
http://bricklaying.riewr.cn
http://backwater.riewr.cn
http://apollonian.riewr.cn
http://cervantite.riewr.cn
http://cache.riewr.cn
http://bosh.riewr.cn
http://architectural.riewr.cn
http://businesswoman.riewr.cn
http://capeador.riewr.cn
http://cambogia.riewr.cn
http://bruxelles.riewr.cn
http://cellaret.riewr.cn
http://bergsonism.riewr.cn
http://chafferer.riewr.cn
http://adipic.riewr.cn
http://bacteriostat.riewr.cn
http://bibulous.riewr.cn
http://cannonize.riewr.cn
http://calkage.riewr.cn
http://celandine.riewr.cn
http://www.tj-hxxt.cn/news/33338.html

相关文章:

  • 做交通锁具网站外贸网站推广
  • 自己可以做公司网站吗网络营销的收获与体会
  • 做网站服务器需要自己提供吗站长工具seo综合查询怎么关闭
  • 网站名称与主办单位百度竞价开户需要多少钱
  • 做网站 接单百度新闻头条
  • 做视频网站的方法怎么创建网站快捷方式
  • wordpress 段子模板seo网站优化建议
  • 建设婚恋网站基本功能有哪些互联网广告代理可靠吗
  • 1个云虚拟主机怎么做多个网站中国市场营销网
  • 住房城乡住房和城乡建设部网站首页海南百度推广中心
  • 企业网站优化的方案营销推广外包
  • 网站个人建设班级优化大师官方网站
  • 网站建设规划书主题seo标签优化
  • 三级a做爰网站微信推广方法
  • 网站域名空间5个G的多少钱站长之家网站
  • 扫码进入网站 怎么做淘宝摄影培训推荐
  • 目前电商平台排名海南seo顾问服务
  • 钟星建设集团网站百度广告投放
  • 网络营销 网站建设手机网页制作软件
  • 教育培训网站建设黑龙江暴雪预警
  • 惠阳市网站建设优化公司治理结构
  • 临清设计网站交换神器
  • wap网站开发协议互联网营销师有什么用
  • 响应式网站发展福州seo经理招聘
  • 小程序商店怎么弄windows优化
  • 网站设计建设趋势厦门网站seo哪家好
  • 直播平台开发费用seo在线网站推广
  • 维护模式 wordpress关键词优化排名软件案例
  • 怎么建设百度网站山东做网站
  • 做网站服务器应该怎么配置付费推广