"Engine room cooperation" state mode for on-machine judgment

Source: Internet
Author: User

In the computer room charge system of self-feeling on the machine is the need to judge the most of a function, to determine: 1, the card number is present, 2, the password is correct, 3, whether the card can be used, 4, whether the card has been on the machine, 5, the card is not rich. Temporarily only I can think of so many judgments, if still according to the previous wording, how much to write If...else ... , all listed together also do not conform to the idea of object-oriented, so you can use the state mode to solve the problem.

State mode

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

It mainly solves the situation that when the conditional expression that controls an object state transition is too complex, the judgment logic of State is transferred to a series of classes representing different states, which can simplify the complex judgment logic.

Benefits:

Localize the behavior associated with a particular state and separate the behavior of the different states. The purpose is to eliminate the large conditional branching statements, and the state mode reduces the dependency between the States by distributing the various state transfer logic to the sub-classes of the status.

Status mode structure diagram:


The state mode of the machine room charge system:


As you can see from the class diagram, I have defined an abstract state class, five specific state classes, and one instance maintenance class.

on-Machine abstract class:

Public abstract class Cardonlinestate    {public        abstract void Online (Cardonlinecontext context,cardentity Cardentity, lineinfoentity lineinfoentity, basicdataentity basicdataentity);    }
on-machine conditions determine the specific state class:

public class Isexitcard:cardonlinestate {public override void Online (Cardonlinecontext context,cardentity car Dentity, lineinfoentity lineinfoentity, basicdataentity basicdataentity) {//Check card number if there is a method facto Ry.  Factorydb factory = new Factory.factorydb (); Example Factory ICard ICard = Factory.  Createicard ();  Create interface list<cardentity> List = Icard.isexitcardno (cardentity); Adjust the D-layer method via the interface if (list. Count = = 0) {throw new Exception ("The card number does not exist and cannot be on the machine!")            "); } else {context.  Cardonlinestate = new Checkpwd (); Set next state, Checkpwd}}} public class Checkpwd:cardonlinestate {public override void Online (Cardonlinecontext context, cardentity cardentity, lineinfoentity lineinfoentity, basicdataentity            basicdataentity) {//Check whether the card is on the machine method Factory.factorydb Factory = new Factory.factorydb (); ICard ICaRD = Factory.            Createicard ();            list<cardentity> list = Icard.checkstupwd (cardentity); if (list. Count = = 0) {throw new Exception ("The password is not correct and cannot be on the machine!")            "); } else {context.  Cardonlinestate = new Iscarduse (); Set next state, Iscarduse}}} public class Iscarduse:cardonlinestate {public override Voi D Online (Cardonlinecontext context, cardentity cardentity, lineinfoentity lineinfoentity, basicdataentity            basicdataentity) {//check if the card is used factory.factorydb Factory = new Factory.factorydb (); ICard ICard = Factory.            Createicard ();            list<cardentity> list = Icard.iscarduse (cardentity); if (list. Count = = 0) {throw new Exception ("This card has been sold and cannot be on machine!")            "); } else {context.  Cardonlinestate = new IsOnline ();  Set next state, i.e. Isonline}      }} public class Isonline:cardonlinestate {public override void Online (Cardonlinecontext context,             Cardentity cardentity, lineinfoentity lineinfoentity, basicdataentity basicdataentity) {//Determine if the card is already on the machine method            Factory.factorydb Factory = new Factory.factorydb (); Ilineinfo Ilineinfo = Factory.            Createilineinfo ();            list<lineinfoentity> list = Ilineinfo.isstuonline (lineinfoentity); if (list. Count! = 0) {throw new Exception ("This card is on the machine, the machine fails!")            "); } else {context.  Cardonlinestate = new Isleastcash ();        Set next state, Isleastcash}}} public class Isleastcash:cardonlinestate { public override void Online (Cardonlinecontext context, cardentity cardentity, Lineinfoentity lineinfoentity, Basicdataentity basicdataentity) {//Determine if the card is rich in Machine method factory.factorydb Factory = new FactOry.            Factorydb (); ICard ICard = Factory.            Createicard ();            list<cardentity> list = icard.inquirebalance (cardentity);            Factory.factorydb factory1 = new Factory.factorydb (); Ibasicdata ibasicdata = Factory1.            Createibasicdata ();            List<basicdataentity> List1 = Ibasicdata.inquirebasicdata (basicdataentity); if (List[0]. Cash < list1[0]. Leastcash) {throw new Exception ("This card has insufficient balance, failed on the machine!")            "); }        }    }
to maintain an instance of the State class:

public class Cardonlinecontext    {        //Define maintenance class initial state        private cardonlinestate cardonlinestate;        Public Cardonlinecontext (cardonlinestate cardonlinestate)        {            this.cardonlinestate = cardonlinestate;        }        Read-write State property for reading the current state and setting the new state public        cardonlinestate cardonlinestate        {            get {return cardonlinestate;}            set {cardonlinestate = value;}        }        Handle the request and set the next state public        void SetState (cardentity cardentity, lineinfoentity lineinfoentity, basicdataentity basicdataentity)        {            cardonlinestate. Online (this,cardentity,lineinfoentity, basicdataentity);        }    }
Call:

        public void Linelogin (cardentity cardentity, lineinfoentity lineinfoentity, basicdataentity basicdataentity)        {            #region State mode Implementation on                //Settings Maintenance instance Class initial state, i.e. Isexitcard                cardonlinecontext c1 = new Cardonlinecontext (New Isexitcard ( )); Determine if the card number is present                //The following is an ongoing request                C1. SetState (cardentity, lineinfoentity, basicdataentity);                C1. SetState (cardentity, lineinfoentity, basicdataentity);                C1. SetState (cardentity, lineinfoentity, basicdataentity);                C1. SetState (cardentity, lineinfoentity, basicdataentity);                C1. SetState (cardentity, lineinfoentity, basicdataentity);            #endregion                    }
on the use of State mode is also stumbled over, once was changed beyond recognition, but again and again debugging to the design of the understanding of the pattern is more profound, and ultimately successfully run out, so, to be bold to do, do not do and how will know that success is not far from the corner?


"Engine room cooperation" state mode for on-machine judgment

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.