Design Mode state model (II)

In the last article, we have seen the demand for change, we need to transform the existing force candy machine code to meet the needs of this new mention. However, it did not bother us, at least we give the ideas and in the end the class diagram, you do not know how to think of it.

We do not come empty, directly to the question, open our learning journey.

We achieved the status of class

Now is the time to achieve a state: we know what we have to act, we just make it into the code. We intend to fully comply with the written state machine code, but this time it was scattered in different classes. For example, we NoQuarterState Example.

public class NoQuarterState implements State {
    GumballMachine gumballMachine;
 
    public NoQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }
 
    public void insertQuarter() {
        System.out.println("You inserted a quarter");
        gumballMachine.setState(gumballMachine.getHasQuarterState());
    }
 
    public void ejectQuarter() {
        System.out.println("You haven't inserted a quarter");
    }
 
    public void turnCrank() {
        System.out.println("You turned, but there's no quarter");
     }
 
    public void dispense() {
        System.out.println("You need to pay first");
    } 
    
}

Prior to the completion of these state class, we need to reinvent candy machine, so you know it all works. We had to use an integer representing the state of the object's status to:

    State soldOutState;
    State noQuarterState;
    State hasQuarterState;
    State soldState;
 
    State state;
    int count = 0;

In this way, we have a class full of candy machines

public class GumballMachine {
 
    State soldOutState;
    State noQuarterState;
    State hasQuarterState;
    State soldState;
 
    State state;
    int count = 0;
 
    public GumballMachine(int numberGumballs) {
        soldOutState = new SoldOutState(this);
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);

        this.count = numberGumballs;
        if (numberGumballs > 0) {
            state = noQuarterState;
        } else {
            state = soldOutState;
        }
    }
 
    public void insertQuarter() {
        state.insertQuarter();
    }
 
    public void ejectQuarter() {
        state.ejectQuarter();
    }
 
    public void turnCrank() {
        state.turnCrank();
        state.dispense();
    }
 
    void releaseBall() {
        System.out.println("A gumball comes rolling out the slot...");
        if (count != 0) {
            count = count - 1;
        }
    }
 
    // 后面部分省略
}

Well, this way, we can continue to achieve more of a class state. For example, we can achieve HasQuarterState and SoldState class

public class HasQuarterState implements State {
    GumballMachine gumballMachine;
 
    public HasQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }
  
    public void insertQuarter() {
        System.out.println("You can't insert another quarter");
    }
 
    public void ejectQuarter() {
        System.out.println("Quarter returned");
        gumballMachine.setState(gumballMachine.getNoQuarterState());
    }
 
    public void turnCrank() {
        System.out.println("You turned...");
        gumballMachine.setState(gumballMachine.getSoldState());
    }

    public void dispense() {
        System.out.println("No gumball dispensed");
    }
    
}
public class SoldState implements State {
 
    GumballMachine gumballMachine;
 
    public SoldState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }
       
    public void insertQuarter() {
        System.out.println("Please wait, we're already giving you a gumball");
    }
 
    public void ejectQuarter() {
        System.out.println("Sorry, you already turned the crank");
    }
 
    public void turnCrank() {
        System.out.println("Turning twice doesn't get you another gumball!");
    }
 
    public void dispense() {
        gumballMachine.releaseBall();
        if (gumballMachine.getCount() > 0) {
            gumballMachine.setState(gumballMachine.getNoQuarterState());
        } else {
            System.out.println("Oops, out of gumballs!");
            gumballMachine.setState(gumballMachine.getSoldOutState());
        }
    }
    
}

Check, what we have done so far

You have a candy machine to achieve now, he and the previous version very different in structure, but the function is the same. We found that you have achieved the following:

  • The behavior of each state is localized to its own class

  • The statement prone to problems if removed to facilitate future maintenance

  • So that each state "closed for modification" so that candy machine "open for extension," because the class can add new state

  • Create a new code base and class structure, which is more universal mapping Figure candy company, but also easier to read and understand

Defined state mode

Yes, just now, we have achieved the status mode. Now let's look at what is the status mode.

State mode allows the object to change its behavior when the internal state changes, the object appears to modify its class.

Let us take a look at the class diagram state pattern:

The class diagram class diagram and the strategy pattern is the same. But while the class diagram is the same, but the difference between the two modes is that their "intention" different. The state model group behavior encapsulated in state object, context behavior at any time may be entrusted to one of those states object. For policy model, customers usually specify a policy initiative to be a combination of the object context is which one.

Etc., etc., not to say that there are ten times before pumping in a game? How not to write it? Ha ha ha ha, and not be anxious, you are more than content to digest yet? If you digest it, please be patient, if not, then you first learn them now. I will be the next to get this lottery.

Bye!

"Siege lion run it," thank you for your attention, and now back to reply "Design Mode" gifts you Xiao Bian carefully selected design pattern books. Xiao Bian recently resuscitation, set up a technical exchange group, reply "plus group" to unlock.

Guess you like

Origin www.cnblogs.com/dimple91/p/11423339.html