head first Design Pattern State
生活随笔
收集整理的這篇文章主要介紹了
head first Design Pattern State
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
狀態(tài)模式:允許對象在內(nèi)部狀態(tài)改變時改變它的行為,對象看起來好像修改了它的類。將每種狀態(tài)封裝成獨立的類,并將動作委托給代表當前狀態(tài)的對象,當糖果機處于不同的狀態(tài)時,你投入25¥會得到不同的行為。
策略模式是除了繼承之外一種彈性替代方案,通過組合不同的對象來改變行為。
狀態(tài)模式是不用在context中放置許多條件判斷的替代方案,通常將行為包裝進狀態(tài)中,通過在context中簡單的改變狀態(tài)對象來改變context的行為。
狀態(tài)的改變可以放在context中也可以放在狀態(tài)類中控制,而且狀態(tài)類可以被多個Context實例共享,前提是狀態(tài)對象不能持有他們自己的內(nèi)部狀態(tài),否則不能共享。想要共享狀態(tài),需要把每個狀態(tài)都指定到靜態(tài)的實例變量中。狀態(tài)模式會導致類的數(shù)目大增。
public class GumballMachine {
//all the states of a machine
State soldOutState;
State noQuarterState;
State hasQuarterState;
State soldState;
State winnerState;
//there is no candy in the machine at the beginning
State state = soldOutState;
int count = 0; // count for the number of candy
public GumballMachine(int numberGumballs) {
soldOutState = new SoldOutState(this);
noQuarterState = new NoQuarterState(this);
hasQuarterState = new HasQuarterState(this);
soldState = new SoldState(this);
winnerState = new WinnerState(this);
this.count = numberGumballs;
if(numberGumballs > 0){//如果有糖果
state = noQuarterState;
}
}
public void insertQuarter(){
state.insertQuarter(); //委托給具體的狀態(tài)實例來執(zhí)行
}
public void ejectQuarter(){
state.ejectQuarter();
}
public void turnCrank(){
state.turnCrank();
state.dispense();???
}
public State getHasQuarterState() {
// TODO Auto-generated method stub
return hasQuarterState;
}
public void setState(State state) {
// TODO Auto-generated method stub
this.state = state;
}
void releaseBall(){
System.out.println("A gumball comes rolling out of slot...");
if (count != 0) {
count = count-1;
}
}
public State getNoQuarterState() {
// TODO Auto-generated method stub
return noQuarterState;
}
public State getSoldOutState() {
return soldOutState;
}
public State getSoldState() {
return soldState;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public State getWinnerState() {
// TODO Auto-generated method stub
return winnerState;
}
}
//所有狀態(tài)的接口,里面包括狀態(tài)轉(zhuǎn)移的方法
public interface State {
public void insertQuarter();
public void ejectQuarter();
public void turnCrank();
public void dispense();???
}
public class SoldState implements State {
GumballMachine gumballMachine;
public SoldState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
@Override
public void dispense() {
// TODO Auto-generated method stub
gumballMachine.releaseBall();
if (gumballMachine.getCount()>0) {
gumballMachine.setState(gumballMachine.getNoQuarterState());
} else {
System.out.println("Oops, out of gumballs!");
gumballMachine.setState(gumballMachine.getSoldOutState());
}
}
@Override
public void ejectQuarter() {
// TODO Auto-generated method stub
System.out.println("sorry, you already turned the crank");
}
@Override
public void insertQuarter() {
// TODO Auto-generated method stub
System.out.println("Please wait, we're already giving you a gumball");
}
@Override
public void turnCrank() {
// TODO Auto-generated method stub
System.out.println("Turning the crank twice doesn't get you another gumball!");
}
}
//implement the state which there is no 25$ in the machine
public class NoQuarterState implements State{
GumballMachine gumballMachine;
public NoQuarterState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
@Override
public void dispense() {
// if there is no money
System.out.println("You need to pay first!");
}
@Override
public void ejectQuarter() {
// 沒有給錢就不能退錢
System.out.println("You haven't insert a quarter");
}
@Override
public void insertQuarter() {
// 接收25$然后改變狀態(tài)為HasQuarterState
System.out.println("You insert a quarter");
gumballMachine.setState(gumballMachine.getHasQuarterState());
}
@Override
public void turnCrank() {
// 沒給錢就沒有糖果
System.out.println("You turned, but there's no quarter");
}
}
策略模式是除了繼承之外一種彈性替代方案,通過組合不同的對象來改變行為。
狀態(tài)模式是不用在context中放置許多條件判斷的替代方案,通常將行為包裝進狀態(tài)中,通過在context中簡單的改變狀態(tài)對象來改變context的行為。
狀態(tài)的改變可以放在context中也可以放在狀態(tài)類中控制,而且狀態(tài)類可以被多個Context實例共享,前提是狀態(tài)對象不能持有他們自己的內(nèi)部狀態(tài),否則不能共享。想要共享狀態(tài),需要把每個狀態(tài)都指定到靜態(tài)的實例變量中。狀態(tài)模式會導致類的數(shù)目大增。
public class GumballMachine {
//all the states of a machine
State soldOutState;
State noQuarterState;
State hasQuarterState;
State soldState;
State winnerState;
//there is no candy in the machine at the beginning
State state = soldOutState;
int count = 0; // count for the number of candy
public GumballMachine(int numberGumballs) {
soldOutState = new SoldOutState(this);
noQuarterState = new NoQuarterState(this);
hasQuarterState = new HasQuarterState(this);
soldState = new SoldState(this);
winnerState = new WinnerState(this);
this.count = numberGumballs;
if(numberGumballs > 0){//如果有糖果
state = noQuarterState;
}
}
public void insertQuarter(){
state.insertQuarter(); //委托給具體的狀態(tài)實例來執(zhí)行
}
public void ejectQuarter(){
state.ejectQuarter();
}
public void turnCrank(){
state.turnCrank();
state.dispense();???
}
public State getHasQuarterState() {
// TODO Auto-generated method stub
return hasQuarterState;
}
public void setState(State state) {
// TODO Auto-generated method stub
this.state = state;
}
void releaseBall(){
System.out.println("A gumball comes rolling out of slot...");
if (count != 0) {
count = count-1;
}
}
public State getNoQuarterState() {
// TODO Auto-generated method stub
return noQuarterState;
}
public State getSoldOutState() {
return soldOutState;
}
public State getSoldState() {
return soldState;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public State getWinnerState() {
// TODO Auto-generated method stub
return winnerState;
}
}
//所有狀態(tài)的接口,里面包括狀態(tài)轉(zhuǎn)移的方法
public interface State {
public void insertQuarter();
public void ejectQuarter();
public void turnCrank();
public void dispense();???
}
public class SoldState implements State {
GumballMachine gumballMachine;
public SoldState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
@Override
public void dispense() {
// TODO Auto-generated method stub
gumballMachine.releaseBall();
if (gumballMachine.getCount()>0) {
gumballMachine.setState(gumballMachine.getNoQuarterState());
} else {
System.out.println("Oops, out of gumballs!");
gumballMachine.setState(gumballMachine.getSoldOutState());
}
}
@Override
public void ejectQuarter() {
// TODO Auto-generated method stub
System.out.println("sorry, you already turned the crank");
}
@Override
public void insertQuarter() {
// TODO Auto-generated method stub
System.out.println("Please wait, we're already giving you a gumball");
}
@Override
public void turnCrank() {
// TODO Auto-generated method stub
System.out.println("Turning the crank twice doesn't get you another gumball!");
}
}
//implement the state which there is no 25$ in the machine
public class NoQuarterState implements State{
GumballMachine gumballMachine;
public NoQuarterState(GumballMachine gumballMachine) {
this.gumballMachine = gumballMachine;
}
@Override
public void dispense() {
// if there is no money
System.out.println("You need to pay first!");
}
@Override
public void ejectQuarter() {
// 沒有給錢就不能退錢
System.out.println("You haven't insert a quarter");
}
@Override
public void insertQuarter() {
// 接收25$然后改變狀態(tài)為HasQuarterState
System.out.println("You insert a quarter");
gumballMachine.setState(gumballMachine.getHasQuarterState());
}
@Override
public void turnCrank() {
// 沒給錢就沒有糖果
System.out.println("You turned, but there's no quarter");
}
}
轉(zhuǎn)載于:https://www.cnblogs.com/aquar/archive/2010/05/27/3451450.html
總結(jié)
以上是生活随笔為你收集整理的head first Design Pattern State的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cxgrid按条件计算合计值
- 下一篇: 数据结构趣题——顺序表就地逆置