head first--------------state pattern
head first----------浅谈状态模式
状态模式:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类
实现代码如下:
package com.clark.statepattern.state;
/**
* 将所有的方法封装到一个状态类中,
* insertQuarter(),ejectQuarter(),turnCrank(),dispense()
* 没一个状态都定义成一个类,实现接口State
* @author Administrator
*
*/
public interface State {
//投币
public void insertQuarter();
//退币
public void ejectQuarter();
//转动糖果机
public void turnCrank();
//出糖果
public void dispense();
}
/**
* 将所有的方法封装到一个状态类中,
* insertQuarter(),ejectQuarter(),turnCrank(),dispense()
* 没一个状态都定义成一个类,实现接口State
* @author Administrator
*
*/
public interface State {
//投币
public void insertQuarter();
//退币
public void ejectQuarter();
//转动糖果机
public void turnCrank();
//出糖果
public void dispense();
}
package com.clark.statepattern.state;
/**
* 糖果机器生产
* @author Administrator
*
*/
public class GumballMachine {
//定义四个状态对象
public State soldOutState;
public State noQuarterState;
public State hasQuarterState;
public State soldState;
public State winnerState;
//初始化状态为soldOutState
State state=soldOutState;
//初始化糖果个数
public int count=0;
public GumballMachine(int count) {
this.count = count;
soldOutState = new SoldOutState(this);
soldState = new SoldState(this);
noQuarterState = new NoQuarterState(this);
hasQuarterState = new HasQuarterState(this);
winnerState = new WinnerState(this);
if(count>0){
state=noQuarterState;
}
}
public void insertQuarter(){
state.insertQuarter();
}
public void ejectQuarter(){
state.ejectQuarter();
}
public void turnCrank(){
state.turnCrank();
state.dispense();
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public void releaseBall(){
System.out.println("A gumball comes rolling............");
if(count!=0){
count--;
}
}
public State getSoldOutState() {
return soldOutState;
}
public State getNoQuarterState() {
return noQuarterState;
}
public State getHasQuarterState() {
return hasQuarterState;
}
public State getSoldState() {
return soldState;
}
public int getCount() {
return count;
}
public State getWinnerState() {
return winnerState;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return count+" gumballs";
}
}
/**
* 糖果机器生产
* @author Administrator
*
*/
public class GumballMachine {
//定义四个状态对象
public State soldOutState;
public State noQuarterState;
public State hasQuarterState;
public State soldState;
public State winnerState;
//初始化状态为soldOutState
State state=soldOutState;
//初始化糖果个数
public int count=0;
public GumballMachine(int count) {
this.count = count;
soldOutState = new SoldOutState(this);
soldState = new SoldState(this);
noQuarterState = new NoQuarterState(this);
hasQuarterState = new HasQuarterState(this);
winnerState = new WinnerState(this);
if(count>0){
state=noQuarterState;
}
}
public void insertQuarter(){
state.insertQuarter();
}
public void ejectQuarter(){
state.ejectQuarter();
}
public void turnCrank(){
state.turnCrank();
state.dispense();
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public void releaseBall(){
System.out.println("A gumball comes rolling............");
if(count!=0){
count--;
}
}
public State getSoldOutState() {
return soldOutState;
}
public State getNoQuarterState() {
return noQuarterState;
}
public State getHasQuarterState() {
return hasQuarterState;
}
public State getSoldState() {
return soldState;
}
public int getCount() {
return count;
}
public State getWinnerState() {
return winnerState;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return count+" gumballs";
}
}
package com.clark.statepattern.state;
import java.util.Random;
public class HasQuarterState implements State {
GumballMachine gumballMachine;
//增加一个随机数,产生10%赢的机会
Random randomWinner=new Random(System.currentTimeMillis());
public HasQuarterState(GumballMachine gumballMachine) {
this.gumballMachine=gumballMachine;
}
@Override
public void insertQuarter() {
System.out.println("you couldn't insert twice quarter");
}
@Override
public void ejectQuarter() {
System.out.println("Quarter returned");
gumballMachine.setState(gumballMachine.getNoQuarterState());
}
@Override
public void turnCrank() {
System.out.println("you turned......");
int winner = randomWinner.nextInt(10);
if((winner==0)&&(gumballMachine.getCount()>1)){
gumballMachine.setState(gumballMachine.getWinnerState());
}else{
gumballMachine.setState(gumballMachine.getSoldState());
}
}
@Override
public void dispense() {
System.out.println("No gumball dispense");
}
}
import java.util.Random;
public class HasQuarterState implements State {
GumballMachine gumballMachine;
//增加一个随机数,产生10%赢的机会
Random randomWinner=new Random(System.currentTimeMillis());
public HasQuarterState(GumballMachine gumballMachine) {
this.gumballMachine=gumballMachine;
}
@Override
public void insertQuarter() {
System.out.println("you couldn't insert twice quarter");
}
@Override
public void ejectQuarter() {
System.out.println("Quarter returned");
gumballMachine.setState(gumballMachine.getNoQuarterState());
}
@Override
public void turnCrank() {
System.out.println("you turned......");
int winner = randomWinner.nextInt(10);
if((winner==0)&&(gumballMachine.getCount()>1)){
gumballMachine.setState(gumballMachine.getWinnerState());
}else{
gumballMachine.setState(gumballMachine.getSoldState());
}
}
@Override
public void dispense() {
System.out.println("No gumball dispense");
}
}
package com.clark.statepattern.state;
public class NoQuarterState implements State {
public GumballMachine gumballMachine;
public NoQuarterState(GumballMachine gumballMachine) {
this.gumballMachine=gumballMachine;
}
@Override
public void insertQuarter() {
System.out.println("You insert a quarter");
gumballMachine.setState(gumballMachine.getHasQuarterState());
}
@Override
public void ejectQuarter() {
System.out.println("You haven't insert a quarter");
}
@Override
public void turnCrank() {
System.out.println("you turned!but there is no quarter");
}
@Override
public void dispense() {
System.out.println("you need to pay first");
}
}
public class NoQuarterState implements State {
public GumballMachine gumballMachine;
public NoQuarterState(GumballMachine gumballMachine) {
this.gumballMachine=gumballMachine;
}
@Override
public void insertQuarter() {
System.out.println("You insert a quarter");
gumballMachine.setState(gumballMachine.getHasQuarterState());
}
@Override
public void ejectQuarter() {
System.out.println("You haven't insert a quarter");
}
@Override
public void turnCrank() {
System.out.println("you turned!but there is no quarter");
}
@Override
public void dispense() {
System.out.println("you need to pay first");
}
}
package com.clark.statepattern.state;
public class SoldOutState implements State {
GumballMachine gumballMachine;
public SoldOutState(GumballMachine gumballMachine) {
this.gumballMachine=gumballMachine;
}
@Override
public void insertQuarter() {
System.out.println("there isn't a quarter to sale");
}
@Override
public void ejectQuarter() {
System.out.println("you couldn't eject any other quarter");
}
@Override
public void turnCrank() {
System.out.println("you couldn't turn crank");
}
@Override
public void dispense() {
System.out.println("there no gumball to dispense");
}
}
public class SoldOutState implements State {
GumballMachine gumballMachine;
public SoldOutState(GumballMachine gumballMachine) {
this.gumballMachine=gumballMachine;
}
@Override
public void insertQuarter() {
System.out.println("there isn't a quarter to sale");
}
@Override
public void ejectQuarter() {
System.out.println("you couldn't eject any other quarter");
}
@Override
public void turnCrank() {
System.out.println("you couldn't turn crank");
}
@Override
public void dispense() {
System.out.println("there no gumball to dispense");
}
}
package com.clark.statepattern.state;
public class SoldState implements State {
GumballMachine gumballMachine;
public SoldState(GumballMachine gumballMachine) {
this.gumballMachine=gumballMachine;
}
@Override
public void insertQuarter() {
System.out.println("please wait,we have already giving you a gumball");
}
@Override
public void ejectQuarter() {
System.out.println("Sorry!you have turn crank");
}
@Override
public void turnCrank() {
System.out.println("turning twice doesn't give you another gumball");
}
@Override
public void dispense() {
gumballMachine.releaseBall();
if(gumballMachine.getCount()>0){
gumballMachine.setState(gumballMachine.getNoQuarterState());
}else{
System.out.println("out of gumballs!");
gumballMachine.setState(gumballMachine.getSoldOutState());
}
}
}
public class SoldState implements State {
GumballMachine gumballMachine;
public SoldState(GumballMachine gumballMachine) {
this.gumballMachine=gumballMachine;
}
@Override
public void insertQuarter() {
System.out.println("please wait,we have already giving you a gumball");
}
@Override
public void ejectQuarter() {
System.out.println("Sorry!you have turn crank");
}
@Override
public void turnCrank() {
System.out.println("turning twice doesn't give you another gumball");
}
@Override
public void dispense() {
gumballMachine.releaseBall();
if(gumballMachine.getCount()>0){
gumballMachine.setState(gumballMachine.getNoQuarterState());
}else{
System.out.println("out of gumballs!");
gumballMachine.setState(gumballMachine.getSoldOutState());
}
}
}
package com.clark.statepattern.state;
public class WinnerState implements State {
GumballMachine gumballMachine;
public WinnerState(GumballMachine gumballMachine) {
this.gumballMachine=gumballMachine;
}
@Override
public void insertQuarter() {
System.out.println("please wait,we have already giving you a gumball");
}
@Override
public void ejectQuarter() {
System.out.println("Sorry!you have turn crank");
}
@Override
public void turnCrank() {
System.out.println("turning twice doesn't give you another gumball");
}
//给两次释放糖果的机会,让后进入NoQuarter State和SoldOutState
@Override
public void dispense() {
System.out.println("You are a winner!you get two gumballs for you quarter");
gumballMachine.releaseBall();
if(gumballMachine.getCount()==0){
gumballMachine.setState(gumballMachine.getSoldOutState());
}else{
gumballMachine.releaseBall();
if(gumballMachine.getCount()>0){
gumballMachine.setState(gumballMachine.getNoQuarterState());
}else{
System.out.println("out of gumballs!!!");
gumballMachine.setState(gumballMachine.getSoldOutState());
}
}
}
}
public class WinnerState implements State {
GumballMachine gumballMachine;
public WinnerState(GumballMachine gumballMachine) {
this.gumballMachine=gumballMachine;
}
@Override
public void insertQuarter() {
System.out.println("please wait,we have already giving you a gumball");
}
@Override
public void ejectQuarter() {
System.out.println("Sorry!you have turn crank");
}
@Override
public void turnCrank() {
System.out.println("turning twice doesn't give you another gumball");
}
//给两次释放糖果的机会,让后进入NoQuarter State和SoldOutState
@Override
public void dispense() {
System.out.println("You are a winner!you get two gumballs for you quarter");
gumballMachine.releaseBall();
if(gumballMachine.getCount()==0){
gumballMachine.setState(gumballMachine.getSoldOutState());
}else{
gumballMachine.releaseBall();
if(gumballMachine.getCount()>0){
gumballMachine.setState(gumballMachine.getNoQuarterState());
}else{
System.out.println("out of gumballs!!!");
gumballMachine.setState(gumballMachine.getSoldOutState());
}
}
}
}
package com.clark.statepattern.state;
//测试类
public class GumballMachineTest {
public static void main(String[] args) {
GumballMachine gumballMachine= new GumballMachine(5);
System.out.println(gumballMachine);
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println(gumballMachine);
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println(gumballMachine);
}
}
//测试类
public class GumballMachineTest {
public static void main(String[] args) {
GumballMachine gumballMachine= new GumballMachine(5);
System.out.println(gumballMachine);
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println(gumballMachine);
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
gumballMachine.insertQuarter();
gumballMachine.turnCrank();
System.out.println(gumballMachine);
}
}
//======以下为测试结果,其中结果存在随即性,其主要是由于WinnerState引起的,如果存在十次抽中一次中奖的游戏,则其送一个免费的糖果==================
5 gumballs
You insert a quarter
you turned......
A gumball comes rolling............
4 gumballs
You insert a quarter
you turned......
A gumball comes rolling............
You insert a quarter
you turned......
You are a winner!you get two gumballs for you quarter
A gumball comes rolling............
A gumball comes rolling............
1 gumballs
You insert a quarter
you turned......
A gumball comes rolling............
4 gumballs
You insert a quarter
you turned......
A gumball comes rolling............
You insert a quarter
you turned......
You are a winner!you get two gumballs for you quarter
A gumball comes rolling............
A gumball comes rolling............
1 gumballs