Detailed series of tutorials on design patterns (iii) status modes for general online shopping Order Processes

Source: Internet
Author: User

This is a state chart drawn using EA. It can be seen from the figure that it shows in detail the form of a shopping order and the sending process. In the process of order formation, it will go through various states for processing, when to enter which State requires complicated logic to judge. to simplify the logic, we can apply the state pattern to distribute complicated logic judgments to each subclass. First, take a look at the status chart:

This figure describes various order statuses, such as adding orders, registering, approving, sending, printing invoices, and canceling orders.

The following figure shows how we use the design mode to convert it to the status mode:

Apart from an orderstate class, we abstract behavior related to all States. Different subclasses have different behavior implementations.

It can be understood that the state mode = aggregation + inheritance.

Various statuses can be aggregated, but they are not essential to each status. The salseorder class in the figure controls the instance objects of a status class, it has the function of setting and reading various instance objects. The logic judgment for determining the specific shape for instantiation has been distributed inside each instance. Therefore, the status mode makes the code flexible and easy to expand.

It can also be understood that the state mode is a special class. It has many attributes. It does not decide which attribute to use, but is determined by its own attributes. In our life, such things are numerous. For example: as a good leader, you must know how to give power to others. Do not decide everything in person and judge it in person. If so, you must be exhausted and know that decentralization is a good leader. This is not the use of decentralization. It is only responsible for Displaying results, reducing its burden, and learning to lead others.

Learning is an action that comes from life and is higher than life. Learning cannot be separated from life. Learning is based on life to understand and understand what we learn, it will deepen your understanding and understanding. You will certainly find some commonalities and some valuable things. In turn, it will guide your life and improve yourself by 1.1.

The following is the classic status mode:

After reading this figure, do you think of a similar pattern, that is, the rule pattern. In fact, the state pattern and the rule pattern are exactly the same, that is, the same principle. However, the difference between them lies in different focuses and different solutions. The strategic model lies in solving algorithms. For example, there are many sales promotion methods, such as discounts, discounts, and 100 off, some elastic problems. The State mode is to break down complex logic and distribute complex logic into sub-classes, which simplifies the program code.

It is not important to remember the diagram of each design pattern. The key lies in understanding and application. The design pattern is a long-term learning !!

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.