behavior_state
状態者モード
本章タスク
1.状態者モードの定義
ステータス者モード:異なるステータス、異なる動作;あるいは、それぞれの状態に相応の行為がある.
2.我々は常にIf elseif elseを使用して状態切り替えを行い、状態の切り替えが繰り返されることを解決する.
eg:「hello」-->「hi」-->「bye」-->「hello」の配列の組合せ
本章の目標
1.状態者パターンの理解
本章タスク
1.状態者モードの定義
ステータス者モード:異なるステータス、異なる動作;あるいは、それぞれの状態に相応の行為がある.
2.我々は常にIf elseif elseを使用して状態切り替えを行い、状態の切り替えが繰り返されることを解決する.
eg:「hello」-->「hi」-->「bye」-->「hello」の配列の組合せ
package com.aptech.behavior.state;
/**
*
* : IShopState.java
* :
* : -- ( , , )
* "hello"-->"hi"-->"bye"-->"hello" ,
* State , State , ,
* : ,
* , State .
* Email:[email protected]
* : 2010-7-9 08:07:46
*/
public interface IShopState {
//
public void shop();
//
public void generateBill();
//
public void pay();
}
package com.aptech.behavior.state;
/**
*
* : ShopState.java
* :
* :The parent class of state
* Email:[email protected]
* : 2010-7-9 08:57:50
*/
public class ShopState implements IShopState {
public ShopState() {
}
public void shop() {
}
public void generateBill() {
}
public void pay() {
}
/**
*
* 〈 〉
*
* @param [ShopContext] [ ]
* @param [ShopState] [ ]
* @return [void]
* @Author
* Email:[email protected]
*/
protected void changeState(ShopContext c, ShopState s) {
c.changeState(s);
}
}
package com.aptech.behavior.state;
/**
*
* : ShopContext.java
* :
* :
* Email:[email protected]
* : 2010-7-9 09:07:10
*/
public class ShopContext {
//
private ShopState currentState;
public ShopContext() {
}
/**
*
* 〈 〉
* @param [ShopState] [ ]
* @return [void]
* @Author
* Email:[email protected]
*/
public void changeState(ShopState s) {
currentState = s;
}
public void shop() {
currentState.shop();
}
public void generateBill() {
currentState.generateBill();
}
public void pay() {
currentState.pay();
}
}
package com.aptech.behavior.state;
/**
*
* : Shop.java
* :
* :
* Email:[email protected]
* :2010-7-9 08:52:11
*/
public class Shop extends ShopState {
public static boolean instanceFlag = false; //true if have 1 instance
private Shop() {
}
public static Shop getInstance() {
if(! instanceFlag) {
instanceFlag = true;
return new Shop();
}
return null;
}
@Override
public void shop() {
System.out.println("The state is shopping now !");
}
}
package com.aptech.behavior.state;
/**
*
* : GenerateBill.java
* :
* :
* Email:[email protected]
* : 2010-7-9 08:55:34
*/
public class GenerateBill extends ShopState {
public static boolean instanceFlag = false; // true if have 1 instance
private GenerateBill() {
}
public static GenerateBill getInstance() {
if (!instanceFlag) {
instanceFlag = true;
return new GenerateBill();
}
return null;
}
public void generateBill() {
System.out.println("The state is generating bill now !");
}
}
package com.aptech.behavior.state;
/**
*
* : Pay.java
* :
* :
* Email:[email protected]
* : 2010-7-9 08:16:15
*/
public class Pay extends ShopState {
public static boolean instanceFlag = false; //true if have 1 instance
private Pay() {
}
public static Pay getInstance() {
if(! instanceFlag) {
instanceFlag = true;
return new Pay();
}
return null;
}
public void pay() {
System.out.println("The state is pay now !");
}
}
package com.aptech.behavior.state;
import junit.framework.TestCase;
public class StateTest extends TestCase {
protected void setUp() throws Exception {
super.setUp();
}
public void testState(){
//
ShopContext myContext = new ShopContext();
//
ShopState myShop = Shop.getInstance();
ShopState myGenBill = GenerateBill.getInstance();
ShopState myPay = Pay.getInstance();
//
myContext.changeState(myShop);
myContext.shop();
//
myContext.changeState(myGenBill);
myContext.generateBill();
//
myContext.changeState(myPay);
myContext.pay();
}
}
本章の目標
1.状態者パターンの理解