Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the design pattern of C # Observer

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the design pattern of C # Observer". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the design pattern of C # Observer".

Let's take a look at a new example, because there is a lot of related content, so the progress of this article will be slightly faster:

Suppose we have a high-end water heater, and we turn it on 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 / / boil Water public void BoilWater () {for (int I = 0; I 95) {MakeAlert (temperature); ShowMsg (temperature);} / / issue a voice alarm private void MakeAlert (int param) {Console.WriteLine ("Alarm: Didi, water has reached {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 C# 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)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report