Analysis and Practice of Android source code design patterns (8)

Source: Internet
Author: User

Analysis and Practice of Android source code design patterns (8)
Chapter 8. State Mode 1. Definition

The behavior in the state mode is determined by the State, and different states have different behaviors. When the internal state of an object changes, it is allowed to change its behavior. This object seems to have changed its class.

2. Use Cases

1. The behavior of an object depends on its state, and it must change its behavior according to its state at runtime.
2. The Code contains a large number of condition statements related to the object state. For example, an operation contains a large number of multi-branch statements, and these branches depend on the state of the object.

3. Simple implementation

Effect: first, the status of the TV is divided into the boot and shutdown status. You can use the remote control to switch channels and adjust the volume, but these operations will fail when the TV is turned off.

The first is the general implementation method:

Public class TVController {// boot status private final static int POWER_ON = 1; // shutdown status private final static int POWER_OFF = 2; // default status private int mState = POWER_OFF; public void powerOn () {if (mState = POWER_OFF) {System. out. println ("TV started");} mState = POWER_ON;} public void powerOff () {if (mState = POWER_ON) {System. out. println ("TV shut down");} mState = POWER_OFF;} public void nextChannel () {if (mState = POWER_ON) {System. out. println ("next channel");} else {System. out. println ("No boot") ;}} public void prevChannel () {if (mState = POWER_ON) {System. out. println ("last channel");} else {System. out. println ("No boot") ;}} public void turnUp () {if (mState = POWER_ON) {System. out. println ("turn up the volume");} else {System. out. println ("No boot") ;}} public void turnDown () {if (mState = POWER_ON) {System. out. println ("");} else {System. out. println ("No boot ");}}}

We can see that each execution is performed by judging the current status, and some code is duplicated. Assuming that the status and function increase, it will become increasingly difficult to maintain. You can use the status mode as follows:

TV operations

/*** TV Status interface, which defines the TV operation function ***/public interface TVState {public void nextChannel (); public void prevChannel (); public void turnUp (); public void turnDown ();}

Shutdown status

/***** Shutdown status, no operation result ***/public class PowerOffState implements TVState {@ Override public void nextChannel () {}@ Override public void prevChannel () {}@ Override public void turnUp () {}@ Override public void turnDown (){}}

Boot status

/***** Boot status, operation effective ***/public class PowerOnState implements TVState {@ Override public void nextChannel () {System. out. println ("next channel") ;}@ Override public void prevChannel () {System. out. println ("previous channel") ;}@ Override public void turnUp () {System. out. println ("increase volume") ;}@ Override public void turnDown () {System. out. println (" ");}}

Power Operation Interface

/*** Power operation interface ***/public interface PowerController {public void powerOn (); public void powerOff ();}

TV remote control

/*** TV remote control ***/public class TVController implements PowerController {TVState mTVState; public void setTVState (TVState mTVState) {this. mTVState = mTVState;} @ Override public void powerOn () {setTVState (new PowerOnState (); System. out. println ("started") ;}@ Override public void powerOff () {setTVState (new PowerOffState (); System. out. println ("Shut Down");} public void nextChannel () {mTVState. nextChannel ();} public void prevChannel () {mTVState. prevChannel ();} public void turnUp () {mTVState. turnUp ();} public void turnDown () {mTVState. turnDown ();}}

Call:

Public class Client {public static void main (String [] args) {TVController tvController = new TVController (); // sets the boot status of tvController. powerOn (); // tvController of the next channel. nextChannel (); // increase the volume of tvController. turnUp (); // shutdown tvController. powerOff (); // turn down the volume, which does not take effect for tvController. turnDown ();}}

Result

Powered on, the next channel is used to increase the volume and shut down.

It can be seen that the state mode encapsulates these behaviors into the state class and forwards these functions to the State object during operations. Different states have different implementations, the if-else statement is already exceeded, which is the essence of the state mode.

4. Differences from policy Modes

The status mode and Policy mode have almost the same structure, just like a twin brother. However, they have different places of view and essence. The action in the state mode is parallel and irreplaceable. The action in the Policy mode is independent of each other and can be replaced with each other. The status mode is usually a change in the self-control status. The policy mode specifies the policy used by the outside.

5. Use of Android in practice

1. log on to the system and determine the event handling method based on whether the user logs on.

2. Wi-Fi management. In different statuses, Wi-Fi scan requests are processed differently.

6. Summary 1. Advantages

Put all actions related to a specific State into a State object, which provides a better way to organize Code related to a specific State, it converts tedious status judgments into state-class families with clear structures to ensure scalability and maintainability while avoiding code expansion.

2. Disadvantages

The use of state mode will inevitably increase the number of System Classes and objects.

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.