JavaScript Write State mode

Source: Internet
Author: User

Writes the toggle of the state mode, as well as the branch loop. But how to implement sub-state nesting it?

/** * by Jackchen 2016-3-26 11.51.20 * * State mode: * One state to another transition. In fact, the program can be transformed into a complex process of extraction out. * For example, the transformation of state in Level2 is complex and can be completely and externally processed. * By specifying the condition of the toggle state in the state object. Reads the current run condition decision. * While the core class can only manage what they need to do, no external switch *////////////////////////////State class//design your own state name, switch state by name to find var Stateitem = function ( Name) {this._name = name;} Stateitem.prototype = {};        StateItem.prototype.construct = stateitem;//updates its coordinates simultaneously, synchronizes to the external element object.defineproperties (Stateitem.prototype, {Name: {        Get:function () {return this._name;        }, Set:function (v) {this._name = v; }});//The Run function, equivalent to the interface, used to run the current state of the code. Get the name of the next state StateItem.prototype.run = function () {return;};/ State Management class var statemanager = function () {This._group = {};this._entrance = "";}; Statemanager.prototype = {}; StateManager.prototype.construct =statemanager;//establishes the state table with the name index StateManager.prototype.add = function (Stateitem) {if (! This._group[stateitem.name]) {This._group[stateitem.name] = Stateitem;return true;} else {Return false;};};/ /Set Unique Entry StateManager.prototype.setEntrance = function (stateitemname) {this._entrance = stateitemname;};/ /overall run, find the current entry function, get the next state name after running, and then find the function of that name///Once the next state is unknown, then end run StateManager.prototype.run = function (begeinstate) {var Nextstate;nextstate = This._group[this._entrance];while (nextstate) {var nextname = Nextstate.run (); Console.log ( Nextstate.name); nextstate = this._group[nextname];}};/ Test//external changes to the process var Needterminalriskmanager = True;var Needonline = False;var Manager = new Statemanager ();//start var begin = new Stateitem ("Begin"), Begin.run = function () {return "appselection";}; Manager.add (begin); Manager.setentrance ("Begin");//Application Select var appselection = new Stateitem ("Appselection"); Appselection.run = function () {return "Appinit";}; Manager.add (appselection);//Apply initialize var appinit = new Stateitem ("Appinit"); appinit.run = function () {return "Readappdata" ;}; Manager.add (appinit);///Read application data//This determines the mode of switching for the next state. The same can be turned into a loop that calls itself to form a cyclic var readappdata = new Stateitem ("ReadappData "); Readappdata.run = function () {if (Needterminalriskmanager) {return "Triskmanager";} Else{return "dataauthentic";};}; Manager.add (readappdata);//terminal risk management var Triskmanager = new Stateitem ("Triskmanager"); Triskmanager.run = function () {return "taanalysis";}; Manager.add (Triskmanager);//static data authentication var dataauthentic = new Stateitem ("Dataauthentic");D Ataauthentic.run = function () { return "Procrestric";}; Manager.add (dataauthentic);//processing limits var procrestric = new Stateitem ("Procrestric"); Procrestric.run = function () {return "cardholderverify";}; Manager.add (procrestric);//card holder certified var cardholderverify = new Stateitem ("cardholderverify"); Cardholderverify.run = function () {return "taanalysis";}; Manager.add (cardholderverify);//terminal behavior analysis var taanalysis = new Stateitem ("taanalysis"); Taanalysis.run = function () {return "CAC";}; Manager.add (taanalysis);//card behavior analysis var CAC = new Stateitem ("CAC"); Cac.run = function () {return "Onlinedecison";}; Manager.add (CAC);//Online decision var Onlinedecison = new Stateitem ("Onlinedecison"); ONlinedecison.run = function () {if (needonline) {return "Onlineproc";} else {return "completion";};}; Manager.add (Onlinedecison);//trade End var completion = new Stateitem ("completion"); Completion.run = function () {return "End";}; Manager.add (completion);//Issuer online authentication var onlineproc = new Stateitem ("Onlineproc"); onlineproc.run = function () {return] Scriptproc ";}; Manager.add (Onlineproc);//Issuance line script var scriptproc = new Stateitem ("Scriptproc"); Scriptproc.run = function () {return "completion";}; Manager.add (Scriptproc);//end var end = new Stateitem ("End"); end.run = function () {return;}; Manager.add (end); Manager.setentrance ("Begin"); Manager.run ();

  

JavaScript Write State 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.