Design Mode note-state

Source: Internet
Author: User
For example State Mode. This mode and Strategy Patterns have certain similarity:

InStateMode, the derived class holds a reference pointing back to the context class. The main function of the derived class is to use this reference to select and call methods in the context class. InStrategyThis restriction and intent does not exist in the mode,StrategyThe derived class does not support references to the context class, and does not need to call the method of the context class.

StateThe mode completely separates the logic and action of the state machine. The action is inContextClass, while the logic is distributed inStateClass. This makes it easy for the two to change independently without affecting each other. Another advantage of this method is that it is very efficient, basically nestedSwitch/caseThe efficiency is the same. Therefore, the table-driven method is flexible and nested.Switch/caseMethod efficiency.

The cost of using this mode is reflected in two aspects: first,StateWriting a derived class is boring.20Several state machines are troublesome. Second, the logic is scattered and the entire state logic cannot be seen in one place. ThereforeCodeDifficult to maintain.

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.