In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the Observer design pattern example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Description of Observer design pattern
Suppose we have a high-end water heater, we turn on it, when the water temperature exceeds 95 degrees: 1, the loudspeaker will start to make a voice to tell you the temperature of the water; 2, the LCD will also change the display of the water temperature to indicate that the water is about to boil.
Now we need to write a program to simulate the process of boiling water, and we will define a class to represent the water heater, which we call Heater, which has a field that represents the water temperature, called temperature;, and of course, the essential feed water heating method BoilWater (), a voice alarm method MakeAlert (), a water temperature display method, ShowMsg ().
Namespace Delegate {class Heater {private int temperature; / / Water temperature / / public void BoilWater () {for (int I = 0; I)
< = 100; i++) { temperature = i; if (temperature >95) {MakeAlert (temperature); ShowMsg (temperature);} / issue a voice alarm private void MakeAlert (int param) {Console.WriteLine ("Alarm: Didi, the water is already {0} degrees:", param) } / / displays the water temperature private void ShowMsg (int param) {Console.WriteLine ("Display: the water is boiling soon, the current temperature is {0} degrees." , param);}} class Program {static void Main () {Heater ht = new Heater (); ht.BoilWater ();}
Brief introduction of Observer Design pattern
The above example can obviously accomplish the work we described earlier, but it is not good enough. Now suppose the water heater consists of three parts: the water heater, the alarm, and the display, which come from different manufacturers and are assembled. In that case, the water heater should only be responsible for boiling water, it can not sound the alarm or display the water temperature; when the water boils, the alarm will sound the alarm, the display will show the water temperature and so on.
At this point, the above example should look like this:
/ / Water heater public class Heater {private int temperature; / / private void BoilWater () {for (int I = 0; I)
< = 100; i++) { temperature = i; } } } // 警报器 public class Alarm{ private void MakeAlert(int param) { Console.WriteLine("Alarm:嘀嘀嘀,水已经 {0} 度了:" , param); } } // 显示器 public class Display{ private void ShowMsg(int param) { Console.WriteLine("Display:水已烧开,当前温度:{0}度。" , param); } } 这里就出现了一个问题:如何在水烧开的时候通知报警器和显示器?在继续进行之前,我们先了解一下Observer设计模式,Observer设计模式中主要包括如下两类对象: Subject:监视对象,它往往包含着其他对象所感兴趣的内容。在本范例中,热水器就是一个监视对象,它包含的其他对象所感兴趣的内容,就是temprature字段,当这个字段的值快到100时,会不断把数据发给监视它的对象。 Observer:监视者,它监视Subject,当Subject中的某件事发生的时候,会告知Observer,而Observer则会采取相应的行动。在本范例中,Observer有警报器和显示器,它们采取的行动分别是发出警报和显示水温。 在本例中,事情发生的顺序应该是这样的: 警报器和显示器告诉热水器,它对它的温度比较感兴趣(注册)。 热水器知道后保留对警报器和显示器的引用。 热水器进行烧水这一动作,当水温超过95度时,通过对警报器和显示器的引用,自动调用警报器的MakeAlert()方法、显示器的ShowMsg()方法。 类似这样的例子是很多的,GOF对它进行了抽象,称为Observer设计模式:Observer设计模式是为了定义对象间的一种一对多的依赖关系,以便于当一个对象的状态改变时,其他依赖于它的对象会被自动告知并更新。Observer模式是一种松耦合的设计模式。 实现范例的Observer设计模式 我们之前已经对委托和事件介绍很多了,现在写代码应该很容易了,现在在这里直接给出代码,并在注释中加以说明。 using System; using System.Collections.Generic; using System.Text; namespace Delegate { // 热水器 public class Heater { private int temperature; public delegate void BoilHandler(int param); //声明委托 public event BoilHandler BoilEvent; //声明事件 // 烧水 public void BoilWater() { for (int i = 0; i < = 100; i++) { temperature = i; if (temperature >95) {if (BoilEvent! = null) {/ / if there is an object registered BoilEvent (temperature) / / call the methods of all registered objects} / / alarm public class Alarm {public void MakeAlert (int param) {Console.WriteLine ("Alarm: Didi, water is already {0} degrees:", param) }} / / display public class Display {public static void ShowMsg (int param) {/ / static method Console.WriteLine ("Display: the water is boiling, current temperature: {0} degrees." , param);}} class Program {static void Main () {Heater heater = new Heater (); Alarm alarm = new Alarm (); heater.BoilEvent + = alarm.MakeAlert; / / Registration method heater.BoilEvent + = (new Alarm ()) .MakeAlert; / / register method heater.BoilEvent + = Display.ShowMsg for anonymous objects / / register the static method heater.BoilWater (); / / boil water and automatically call the method} of the registered object
The output is:
Alarm: Didi, the water is 96 degrees: Alarm: Didi, the water is already 96 degrees: Display: the water is boiling, the current temperature is 96 degrees. / / omit. Thank you for reading this article carefully. I hope the article "sample Analysis of Observer Design patterns" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.