State mode teaches you how to steamed Carp

Source: Internet
Author: User
Tags event timer time 0

I have been buying nine sisters for almost a year. From android2.3.5 to flyme1.1.3 with deep optimization, It is a storm and rain that comes along with me. I also pay attention to the design of meizu mobile phones. Meizu is a learning team, which is very considerate to users. The new feature of meizu alarm clock-Event timer. Previously, I saw that it was very practical. Recently, When I reviewed the design model, I suddenly found that meizu's new function is a real instance of the Status mode.


In order to let the colleagues who know and do not know the status mode understand what I will talk about next, I will briefly introduce this status mode:

State

The State mode allows an object to change its behavior when its internal state changes, so that the object seems to have modified its class.

The State mode separates multiple states of the system and distributes them to sub-classes of the State, eliminating the large branch statements and reducing coupling, at the same time, it is easy to add and convert new States.

This is equivalent to a schedule or school timetable. The time changes, and our itinerary or course changes accordingly.

Today, with the help of the new features of M9, we are going to experiment with the state pattern to make a steamed carp, so that you can learn more about the design pattern. Not to mention anything else. First:

There are five statuses: preparetime, boillingstate, steamshstate, turnofffishstate, and saucingstate, which correspond to the five steps of the operation.

/// <Summary> /// create a steamed fish /// </Summary> public class steamfish {private State current; // store the current state public steamfish () {current = new preparestate () ;}// total time private int timecount; // when recording the current operation, private int time = 0; Public int timecount {get {return timecount ;} set {// time = timecount; timecount = value ;}}// task completion ID private bool finish = false; Public bool taskfinished {get {return finish ;} set {finish = value ;}// Switch Status public void setstate (State S) {current = s;} // perform the public void steamfishes () steaming operation () {// continue the current operation. steam (this, time );}}

/// <Summary> /// abstract status /// </Summary> public abstract class State {// steamed fish public abstract void steam (steamfish SF, int time );}

/// <Summary> /// preparation status /// </Summary> public class preparestate: State {Private Static int statictime; // The time used to accumulate the current status (in seconds) public override void steam (steamfish SF, int time) {statictime + = (SF. timecount-time); // time used to calculate the cumulative state (seconds) if (SF. timecount <= 60) {If (statictime = (SF. timecount-Time) {console. writeline ("\ r [steamed fish recipe tutorial] \ n ①. put a little ginger, salt, and sesame oil into the fish belly. "+" \ T "+" required time 1 minute 0 seconds, used to {0} minute {1} seconds. \ N ", statictime/60, statictime % 60);} else {console. writeline ("\ t dear user, please wait. This status has been maintained for {0} minutes {1} seconds. \ N ", statictime/60, statictime % 60) ;}} else {statictime = 0; // The next step is to boil the water to SF. setstate (New boillingstate (); SF. steamfishes ();}}}

/// <Summary> /// boiling state /// </Summary> public class boillingstate: State {Private Static int statictime; // The time used to accumulate the current State (in seconds) public override void steam (steamfish SF, int time) {statictime + = (SF. timecount-time); // time used to calculate the cumulative state (seconds) if (SF. timecount <= 150) {If (statictime = (SF. timecount-Time) {console. writeline ("②. put a proper amount of water into the pot and boil it out. \ T required time 1 minute 30 seconds, used to {0} minute {1} seconds. \ N ", statictime/60, statictime % 60);} else {console. writeline ("\ t dear user, please wait. This status has been maintained for {0} minutes {1} seconds. \ N ", statictime/60, statictime % 60) ;}} else {statictime = 0; // The next step is to cook fish SF. setstate (New steamfishstate (); SF. steamfishes ();}}}

/// <Summary> /// steamed fish status /// </Summary> class steamfishstate: State {Private Static int statictime; // The time used to accumulate the current State (in seconds) public override void steam (steamfish SF, int time) {statictime + = (SF. timecount-time); // time used to calculate the cumulative state (seconds) if (SF. timecount <= 570) {If (statictime = (SF. timecount-Time) {console. writeline ("③. place the fish in the pot and close the lid. "+" \ T "+" required time 7 minutes 0 seconds, used to {0} minute {1} seconds. \ N ", statictime/60, statictime % 60);} else {console. writeline ("\ t dear user, please wait. This status has been maintained for {0} minutes {1} seconds. \ N ", statictime/60, statictime % 60) ;}} else {statictime = 0; // The next step is to close the SF. setstate (New turnoffstate (); SF. steamfishes ();}}}

/// <Summary> /// fire-off status /// </Summary> class turnoffstate: State {Private Static int statictime; // The time used to accumulate the State (in seconds) public override void steam (steamfish SF, int time) {statictime + = (SF. timecount-time); // time used to calculate the cumulative state (seconds) if (SF. timecount <= 870) {If (statictime = (SF. timecount-Time) {console. writeline ("4. close the fire. Don't open it! "+" \ T "+" required time 5 minutes 0 seconds, used to {0} minute {1} seconds. \ N ", statictime/60, statictime % 60);} else {console. writeline ("\ t dear user, please wait. This status has been maintained for {0} minutes {1} seconds. \ N ", statictime/60, statictime % 60) ;}} else {statictime = 0; // The next step is to boil the water to SF. setstate (New saucingstate (); SF. steamfishes ();}}}

/// <Summary> /// seasoning status /// </Summary> class saucingstate: State {Private Static int statictime; // The time used to accumulate the current State (in seconds) public override void steam (steamfish SF, int time) {statictime + = (SF. timecount-time); // time used to calculate the cumulative state (seconds) if (SF. timecount <= 880) {If (statictime = (SF. timecount-Time) {console. writeline ("⑤. open the cover, and the spices will be covered with fish! "+" \ T "+" required time 0 minutes 10 seconds, used to {0} minute {1} seconds. \ N ", statictime/60, statictime % 60);} else {console. writeline ("\ t dear user, please wait. This status has been maintained for {0} minutes {1} seconds. \ N ", statictime/60, statictime % 60) ;}} else {SF. taskfinished = true; console. writeline! ");}}}


Client code:

Static void main (string [] ARGs) {// [steamed fish tutorial] steamfish Sf = new steamfish (); SF. timecount = 60; SF. steamfishes (); SF. timecount = 100; SF. steamfishes (); SF. timecount = 150; SF. steamfishes (); SF. timecount = 570; SF. steamfishes (); SF. timecount = 600; SF. steamfishes (); SF. timecount = 870; SF. steamfishes (); SF. timecount = 880; SF. steamfishes (); SF. timecount = 1000; SF. steamfishes (); console. read ();}

Effect:


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.