状态者模式
本章任务
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:fangyong2006@163.com
* 修改时间: 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:fangyong2006@163.com
* 修改时间: 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:fangyong2006@163.com
*/
protected void changeState(ShopContext c, ShopState s) {
c.changeState(s);
}
}
package com.aptech.behavior.state;
/**
*
* 文件名: ShopContext.java
* 版权: 方勇
* 描述:状态上下文切换管理器
* Email:fangyong2006@163.com
* 修改时间: 2010-7-9下午09:07:10
*/
public class ShopContext {
//正在进行的状态
private ShopState currentState;
public ShopContext() {
}
/**
*
* 〈状态的切换〉
* @param [ShopState] [正在进行的状态]
* @return [void]
* @Author 方勇
* Email:fangyong2006@163.com
*/
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:fangyong2006@163.com
* 修改时间: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:fangyong2006@163.com
* 修改时间: 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:fangyong2006@163.com
* 修改时间: 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. 理解状态者模式