State machine mode

Source: Internet
Author: User

State machine mode
Now you need to create an APP that is simple as a video player, mainly including playing, pausing, and stopping three features. Before learning the state machine mode, you may implement this: abstract An IPlayer interface to define the actions and possible status fields to be implemented by your player: Copy code 1 public interface IPlayer {2 public static final int STATE_PLAYING = 1; 3 public static final int STATE_PAUSED = 2; 4 public static final int STATE_STOPPED = 3; 5 6 public void palyVedio (); 7 8 public void pause (); 9 10 public void stop (); 11} copy the code to implement the IPlayer interface now: Copy code 1 public class VedioPlaye R implements IPlayer {2 public int mCurrentState; 3 4 @ Override 5 public void palyVedio () {6 switch (mCurrentState) {7 case STATE_PLAYING: 8 System. out. println ("curent state is palying, do nothing. "); 9 case STATE_PAUSED: 10 case STATE_STOPPED: 11 System. out. println ("paly vedio now. "); 12 break; 13 default: 14 // wocould it happen? Who care.15 break; 16} 17 mCurrentState = STATE_PLAYING; 18} 19 20 @ Override21 public void pause () {22 switch (mCurrentState) {23 case STATE_PLAYING: 24 System. out. println ("pause vedio now"); 25 break; 26 case STATE_PAUSED: 27 System. out. println ("curent state is paused, do noting. "); 28 case STATE_STOPPED: 29 System. out. println ("curent state is stopped, do noting. "); 30 break; 31 default: 32 // wocould it happ En? Who care.33 break; 34} 35 mCurrentState = STATE_PAUSED; 36} 37 38 @ Override39 public void stop () {40 switch (mCurrentState) {41 case STATE_PLAYING: 42 case STATE_PAUSED: 43 System. out. println ("stop vedio now. "); 44 case STATE_STOPPED: 45 System. out. println ("curent state is stopped, do noting. "); 46 break; 47 default: 48 // wocould it happen? Who care.49 break; 50} 51 mCurrentState = STATE_STOPPED; 52} 53 54 55} It's still wrong to copy the code. As we all know, the demand will always change. Now your boss needs to play an advertisement in video playback (titles or credits. Well, you may think it doesn't matter. You just need to add one more method on the interface, and add a state field. After modification: 1 public interface IPlayer {2 public static final int STATE_PLAYING = 1; 3 public static final int STATE_PAUSED = 2; 4 public static final int STATE_STOPPED = 3; 5 public static final int STATE_AD = 4; 6 7 public void palyVedio (); 8 public void pause (); 9 public void stop (); 10 public void showAD (); 11} copy the Code. In the end, you only need VedioPlayer to implement the added showAD method. Copy code 1 @ Override 2 public void showAD () {3 switch (mCurrentState) {4 case STATE_AD: 5 System. out. println ("curent state is AD, do noting"); 6 break; 7 case STATE_PLAYING: 8 System. out. println ("show advertisement now. "); 9 break; 10 case STATE_PAUSED: 11 System. out. println ("curent state is paused, do noting"); 12 case STATE_STOPPED: 13 System. out. println ("curent state is stopped, do noting. "); 14 break; 15 defa Ult: 16 // wocould it happen? Who care.17 break; 18} 19 mCurrentState = STATE_AD; 20} is the copy of the code really finished? Finally, we found out that swtich in the three methods of palyVedio, pause, and stop also needs to add a case to each of them for judgment. Nana !!! If there are several more States in the future, you have to modify them. As the status increases, the modified Code will multiply, Which is unimaginable. In this case, the state machine mode can help you a lot.

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.