Android and design mode-State mode, androidstate

Source: Internet
Author: User

Android and design mode-State mode, androidstate
He first described the State mode in the book "JAVA and mode" by Dr. Yan Hong:
  Status mode, also known as Pattern of Objects for States, is the behavior mode of an object.
The State mode allows an object to change its behavior when its internal state changes. This object looks like a class that has changed it.


Status mode structure
Express in one sentence,The state pattern encapsulates the behavior of the studied object in different State objects. Each State object belongs to a subclass of an abstract state class. The intention of the state mode is to change the behavior of an object when its internal state changes.. The schematic diagram of the Status mode is as follows:




The following roles are involved in the status mode:

● Context: in this example, DataConnecton is used to define the interface that the client is interested in and keep an instance of a specific status class. The instance of this specific status class shows the existing status of the environment object.
● Abstract State role: defines an interface IState to encapsulate the behavior of a specific State of an environment (Context) object.
● ConcreteState role: In this example, DcXXXState is used. Each specific state class implements the action corresponding to a state of the environment (Context.
● Client role: In this example, the DcController is used to manage and maintain the Context. (Text comes from the Network)

This instance is the data connection part of Android4.4. DataConnecton inherits from the StateMachine State machine class, And the StateMachine State machine class implements the State design mode application, the State design mode is used for managing Wi-Fi, Bluetooth, and data connections.

As shown in the figure, seven state subclasses are defined, which are internal classes of DataConnecton. data connections are converted in six states, and DefaultState is the parent state class of the other six subclasses, the so-called parent state class is that when the child state class cannot be processed (processed in the processMessage method), it will be handed over to the parent state class for processing. Status transition is set through the transitionTo () method of DataConnecton.


Source code (there are too many codes, and only the code of Context management State is pasted ):

Private DataConnection (PhoneBase phone, String name, int id, DcTrackerBase dct, DcTesterFailBringUpAll failBringUpAll, DcController dcc) {// [mr2] removed // setDbg (false); addState (mDefaultState ); addState (mInactiveState, mDefaultState); // mDefaultState as the parent State addState (mActivatingState, mDefaultState); addState (callback, mDefaultState); addState (mActiveState, mDefaultState); addState, mDefaultState); addState (mDisconnectingErrorCreatingConnection, mDefaultState); setInitialState (mInactiveState); // set the initial state}


Not yet resumed. please correct me if there is anything wrong.

Related Article

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.