Comparison of State mode and policy mode

Source: Internet
Author: User

When it comes to strategy mode, the first thing we think of is the store's cashier way: Less than 100, normal charge, more than 100 less than 300, over the portion of 80 percent; over 300, full price 90 percent!

The most common way to solve this problem is a lot of if ... Else ..., and it is ruthless difficult to maintain, each time the change of conditions will modify the original code, a serious violation of the opening and closing principle.

Obviously, the solution to the strategy pattern is to encapsulate a series of parallel and complex implementations, and in different scenarios, we choose the one that works best.

Look at its class diagram.

                                         ,         &NB Sp                          ,         &NB Sp                          ,         &NB Sp                -1     "Big talk design mode"

Figure 2 "Headfirst"

The first one is "Big talk design mode", and the second one is a specific example in "headfirst".

The first one is simpler and more typical. But say more.

Look at the second one, it's actually gone through a series of evolutions. The original "Flying" interface is encapsulated in the duck, meaning that all ducks must fly. But actually some ducks can only run, so what? You can only extract the "Flight" to form a separate interface, so that the flying ducks to achieve.

So there are problems, if some ducks can fly directly, some ducks need to run the fly, then how to do? Each type of duck needs to rewrite the flight method, which is not appropriate, so write the "flight" interface implementation of the subclass, and then let the type of duck to call. The essence is a polymorphic.

The strategy is that it encapsulates different ways of flying, which can be used by clients to choose the best and most appropriate to call.

State mode: When an object's internal state changes to allow behavior to change, the object looks like it has changed its class.

To see its class diagram:

State mode mainly highlights the two words: "Change", the state of the object determines the behavior of the state, when we are in the spirit of excitement, work hard, hard work led to our physical and mental exhaustion, physical and mental exhaustion causes our behavior is need to rest. From here we can see that the internal state of things determines the behavior of things, and the behavior of things will change the state of our things, the two are constantly interacting with each other, and then realize the transformation of the state.    

State mode mainly solves the situation when the conditional expression that controls an object state transition is too complex. It is possible to simplify the complex judgment by transferring the judgment of State to a series of classes that represent different states.
A state pattern can be considered when the behavior of a state depends on his state, and he must change his behavior according to the state at run time .
Comparison: From the above points, we can see that the strategy mode and the state pattern of the application scenario is very different : One is to encapsulate a series of parallel and complex and variable implementation, one is to implement the object's internal state of the changes encapsulated, with external behavior to express.

In short, although the class diagram of the two is very similar, but in fact, solve the situation of different scenarios of the problem, we need to be practical analysis and judgment. Different design patterns exist between different design ideas, similar to the need for us to carefully compare, each study is a progress and re-understanding.




Comparison of State mode and policy mode

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.