Principles and important uses of state machines
As a code farmer, the abstract ability of the hacker is really not bad. Is it related to my family, so I always simplify, embody, and publish complicated and abstract problems, i'm afraid others do not understand. Today I will talk about another technology!
An application can only be chained up, and there is a soul to get it up, that is, how to implement specific business logic. The best way to achieve it is the state machine.
An application has a complete life cycle from the beginning to the end. It is in a stable and useful state for most of its life, he must go through various unstable states before entering the Q & A steady state. A state machine is a global variable that marks continuous migration from an unstable state and finally enters the problematic state. Of course, the stable state is mutual, and it will also migrate from the stable state to the unstable state, and finally migrate to the stable state. For example, the application is in the initial login trial status when it is started. (If a login failure occurs, the login fails. This status may not exist ), when the login is successful, it is in the Login Succeeded state (stable state), then you can perform various operations. If you are bored and want to change your account, quit. Then you will be in the Unlogged-on status (because you are usually unable to do core tasks in this status, so it can be called a stable State). When you log on again successfully, you will log on again. When you quit the application, you don't need to record it. This is the simplest login state machine. Similar order state machine for Order Service. Some applications need to enter the previous page when closing the application and then opening the application. This is most suitable for implementation with the state machine. In fact, many people are implementing state machines in a variety of complex ways, instead of using the simplest and most straightforward way.
The simplest and straightforward description of a state machine is to use a global variable of the enumeration type to record different States of an application. Based on this global variable, you can enter different pages or process different logics. An application can have different state machines (global variables of different enumeration types). Global variables of enumeration types can be recorded in files or databases (for example: object Storage core dat or file sandbox, the object persistence of MFC is stored in the database), the next login can read this enumeration type of global variables and jump to different pages and processes.
The Network-triggered automatic logon state machine Enumeration type of an app I created:
typedef NS_ENUM(NSInteger,LOGIN_STATE) { LOGIN_STATE_INIT = 0, LOGIN_STATE_LOGIN_SUCESS, LOGIN_STATE_LOGINING, LOGIN_STATE_UNAUTO_LOGIN, LOGIN_STATE_UNNET_LOGIN, LOGIN_STATE_EXIT_LOGIN};
The Network-triggered automatic login state machine logic diagram of an app I made:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.