C # Water Heater

Source: Internet
Author: User

 Requirement Analysis:Now there is a burner, assuming that the water temperature rises to 100 degrees for boiling water, please use the design program to simulate the whole process of boiling water, open the water heater, when the water temperature reaches 95 degrees, the alarm starts to send an alarm, when the water temperature reaches 100 degrees, disconnect the water heater power.

 

We use conventional thinking to analyze this requirement. Isn't it a switch control or an alarm? Let's take a look at the following spam code.

 

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using system. threading; namespace heater {class program {static int tempture = 0; // <summary> // switch /// </Summary> class switcher {public void switchon () {console. writeline ("water heater turned on");} public void switchoff () {console. writeline ("water heater disabled") ;}/// <summary> // water heater /// </Summary> class heater {public void boidwater () {int I = 0; for (I = 0; I <= 100; I ++) {tempture = I; thread. sleep (1000/(I + 1); If (tempture <95 & tempture % 10 = 0) {console. writeline ("boiling water... ");} else {makealerm (tempture); showtempture (tempture) ;}} public static void makealerm (int tem) {If (TEM> 95) {console. writeline ("alarm:" The water is burning out ..... "") ;}} Private Static void showtempture (INT tempture) {If (tempture> 95) {If (tempture = 100) {console. writeline ("\ n water really opened \ n");} else {console. writeline ("water temperature: {0} \ n", tempture) ;}}} static void main (string [] ARGs) {heater = new heater (); switcher switcher = new switcher (); switcher. switchon (); heater. boidwater (); switcher. switchoff (); console. readline ();}}}
View code

 

If you don't believe it, you can see that it should meet your needs.

In this way, I have no brains at all. To tell the truth, even garbage is not as valuable as it is, and it is completely completed by a code farmer. Now let's change our thinking. Is there an Observer Pattern in the design pattern? Can we try it with the observer pattern? Since the observer mode must have the same object as the observer, I feel that the observer here is the water heater, which monitors the water temperature in real time, and the water temperature is the trigger of other events, let's take a look at the code written this time:

Observer. CS

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using system. threading; namespace oberver {// switch public class switch {public void switchon (INT tempture) {If (tempture <= 0) {console. writeline ("water heater turned on") ;}} public void switchoff (INT tempture) {If (tempture> = 100) {console. writeline ("water heater disabled") ;}}// water heater public class heater {privat E int tempture; Public Delegate void boildhander (int param); // declare the delegate public event boildhander boilevent; // declare the event // open public void boilwater () {If (boilevent! = NULL) {boilevent (tempture) ;}for (INT I = 0; I <= 100; I ++) {tempture = I; thread. sleep (1000/(I + 1); If (I % 10 = 0 & I <100) {console. writeline ("boiling water... ");} // console. writeline ("in boiling water, temperature: {0} '", tempture); If (tempture> 95) {If (boilevent! = NULL) // If an event is registered {boilevent (tempture ); // call all registration time Methods }}}// alert public class alarm {public void makealert (INT tempture) {If (tempture> = 96) {console. writeline ("tick, water has {0} degrees \ n", tempture) ;}}// display public class display {public static void showmsg (int tem) {If (TEM = 100) {console. writeline ("boiling water, current temperature: {0} degree \ n", TEM) ;}}} class progrm {static void main (string [] ARGs) {Switch switcher = new switch ();
Heater heater = new heater (); Alarm alarm = new alarm (); heater. boilevent + = switcher. switchon; // register the event heater. boilevent + = alarm. makealert; // register the event // heater. boilevent + = (new alarm ()). makealert; // Method for registering anonymous objects heater. boilevent + = display. showmsg; // registration method heater. boilevent + = switcher. switchoff; heater. boilwater (); // starts the boiling water console. read ();}}}

Running result:

The same function, different code, does the latter have a sense of height.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.