State management of Java design patterns

Source: Internet
Author: User

definition : Allows an object to change its behavior when its internal state changes. The object appears to have modified its class.

Nature : Separates and chooses behavior according to state.

State : The property value of the object instance.

behavior : The function of an object is mostly mapped to a method.

the function of State mode : The behavior of separating state, by maintaining the change of state, to invoke different functions corresponding to different states.

States and behaviors are associated and can be described as: state-determined behavior .

(1) Defining State enumerations

public enum Magicstate {lock,download, downloading, PROCESS, processing, sound};

  

(2) Setting status

public void SetState (Magicstate state) {this.state = state};

  

(3) Adding a method to a state

private void Switchmagicstate (Magicstate ms) {LOGGING.D (TAG, "switchmagicstate|ms =" + ms); mmagictask.state = Ms;switch ( MS) {case LOCK:                        //.......lock related methods break;case download:break;case downloading:break;case process:break;case Processing:break;case Sound:break;default:break;}}

  

(4) Toggle Status

State management of Java design patterns

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.