"Computer room cooperation" state mode and the machine

Source: Internet
Author: User

in the computer room charge system, there are several business logic is more complex, such as on the machine, the next machine. Remember when I do the first version of the VB charging system, but also special to take out the machine to draw a complete flow chart, otherwise, the final result must be ignorant, do not want to continue to write code. It is necessary for us to comb the business logic of the machine in a complete way before we learn the design pattern. I. On-Machine business logic1. Determine if the card number exists2. Determine if the card number is used3. Determine if the card number balance is sufficient4. Determine if the card number is on the machine5. Perform on-board, add on-machine recordsuch a listing, it is clear that the implementation of the operation of the business needs to go through four times before the judgment. Before we were all through one of the if...else ... Statement to solve the problem. There is no doubt that this is not allowed in object-oriented programming. In order to solve this problem, which requires a large number of if statements to be reused, GOF provides us with a solution, which is "state mode". before starting to combine it with the computer room, first of all, or first review what is "state mode". two. State mode theorystatus Mode (state), when an object's internal state changes to allow it to change its behavior, the object looks like it has changed its class. state mode mainly solves the situation when the conditional expression that controls an object transformation is too complex. It is possible to simplify the complex judgment logic by transferring the judgment logic of the state to a series of classes representing different states. light from the above professional language to see, it is not clear when to use the state mode. But when you see the benefits of using state mode that I'll summarize below, there must be no problem. of the state modeBenefits:  the behavior related to a particular state is localized and the behavior of the different states is separated. To put it bluntly, the goal is to  eliminate large conditional branching statements , and large branch judgments make them difficult to modify and extend.  The  state mode reduces mutual dependency by transferring various states to the logical distribution between the sub-classes of state.   below is its original class diagram:      Three. Using the status mode of the upper machine    through the review of state mode, it is clear that first of all, we need to first define an abstract state class; , we must determine the specific state classes, and these subclasses, in fact, we need to do a single judgment; Finally, we need an instance that maintains the individual state classes that define the current state.   below is the upper class diagram of the plus state mode:   It is also clear that an abstract class, five concrete classes, An instance maintenance class.   five specific classes, that is the matter of the top confidential, and in the instance maintenance class, I defined an initial state, that is, to determine whether the card number exists. If satisfied, the state is changed under the specific State class, and the next state judgment is continued.   here is some code:    abstract class on machine:  
public class Cardonlinestatebll '    <summary> '    card number on machine state abstract class--Hu Zhiting--August 7, 2015 08:54:26    ' </ Summary> "    <param name=" Online "> On machine </param>"    <param name= "Enlinerecord" > On-Machine record entity </param> "    <param name=" Enstdcard "> Card Student View entity </param>    ' <remarks></remarks > Public    Overridable Sub Online (ByVal online as CARDONLINEBLL, ByVal Enlinerecord as Entity.linerecordentity, Byva L Enstdcard as Entity.stucardviewentity)    End subend Class
The specific status class for which the card number exists:

Public Class isexistcardbll:inherits CARDONLINESTATEBLL Private myfacotry as New AbstractFactory.AbstractFactory.Data SQL Private ICard as Idal. ICard ' <summary> ' on machine status determine if card number exists--Hu Zhiting--August 7, 2015 09:12:22 ' </summary> ' <param name= ' O Nline "> Machine </param>" <param name= "Enlinerecord" > Up/Down Entities </param> "<param name=" Enstdcard "&G T; Student Card View entity </param> ' <remarks></remarks> public Overrides Sub Online (online as CARDONLINEBLL, EnL Inerecord as Entity.linerecordentity, Enstdcard as Entity.stucardviewentity) mybase.online (Online, EnLineRecord, en        Stdcard) Dim Factory as AbstractFactory.AbstractFactory.DataSql = New AbstractFactory.AbstractFactory.DataSql Dim ICard as Idal. Istucardview ICard = Factory. Createstudentcardview () Dim cardlist as New List (of entity.stucardviewentity) cardlist = Icard.getstucardinf Obycardid (Enstdcard.cardno) If cardlist. Count = 0 Then            Throw New Exception ("The card number does not exist, cannot be on the machine") Else online. SetState (New iscancelcardbll) ' calls the next state to determine if the card number uses online. Online (Enlinerecord, Enstdcard) ' calls the on-machine method end If End SubEnd Class

To maintain an instance of the State class:
Public Class cardonlinebll ' <summary> ' defines a state variable--Hu Zhiting--August 7, 2015 08:59:14 ' </summary> ' & Lt;remarks></remarks> ' Private state as Cardonlinestatebll ' <summary> ' ' constructor to initialize the initial status of the machine        To determine if the card number exists--Hu Zhiting--August 7, 2015 09:00:07 ' </summary> ' <remarks></remarks> public Sub New () State = New ISEXISTCARDBLL () End Sub ' <summary> ' Set status--Hu Zhiting--August 7, 2015 09:00:41 ' &LT;/SUMMARY&G    T "' <param name=" onlinestate "> Change the On-machine status via incoming status </param> ' <returns></returns> ' <remarks& Gt;</remarks> public Function SetState (ByVal onlinestate as CARDONLINESTATEBLL) as CARDONLINESTATEBLL Stat E = Onlinestate ' current status assignment to variable state return onlinestate ' return current state End Function ' <summary> ' on machine--Hu Zhiting --August 7, 2015 09:01:41 "</summary>" <param name= "Enlinerecord" > Up and Down table entities </param> ' <param Name= "Enstdcard" &GT; Student Card View entity </param> ' <returns></returns> ' <remarks></remarks> public Function O Nline (ByVal Enlinerecord as entity.linerecordentity, ByVal Enstdcard as entity.stucardviewentity) as Boolean Dim on Lines as CARDONLINEBLL = New cardonlinebll ' instantiation on machine class Dim flag as Boolean = False ' defines an initial value of false Try S Tate.            Online (Onlines, Enlinerecord, Enstdcard) ' Call on Machine state class method flag = True ' on machine successful Catch ex as Exception Throw New Exception (ex. Message) ' Catch the machine exception and throw an exception end Try return flag ' Returns the result end FunctionEnd Class<span style= "Font-family:kaiti_g b2312;font-size:18px; " ></span>
In fact, for the design mode of learning, I have always felt very confused, do not know how to apply in the computer room. But the computer room cooperation is a very good opportunity, and everyone together to discuss how we can add them in the computer room, incidentally, the design mode to learn again, and so the practical learning effect and the previous theoretical learning effect is completely different levels. So, still need to try, not from the heart to think difficult and no longer try, do not wait for others to tell you, that may be unlimited waiting ...



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Computer room cooperation" state mode and the machine

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.