`
pan_java
  • 浏览: 286194 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

hand first 设计模式 -状态模式

阅读更多
状态模式:允许对象在内部状态改变时改变它的的行为.对象看起来好像修改了化的类.

将以超市购物为例--说明状态模式




所有状态抽象类
public abstract class BuyStat {

         //在超市状态
	public void inShop() {
		System.out.println("welcome come our shop,please start shoping");
	}
         //选购商品状态
	public void select() {
		System.out.println("you not in shop");
	}

         //支付状态
	public void pay() {
		System.out.println("you don't have merchandise,so don't pay!");
	}

         //商品打包
	public void packing() {
		System.out.println("you don't have merchandise,so don't packing!");
	}
	
         //抽奖
	public void winner(){
		System.out.println("you don't winner!");
	}

}


在超市内
public class InShopStat extends BuyStat {
	private Shop shop;

	public InShopStat(Shop shop) {
		this.shop = shop;
	}

	@Override
	public void inShop() {
		// TODO Auto-generated method stub
		System.out.println("welcome come our shop,please start shoping");
	        //设置下一个状态
                 shop.setStat(shop.getSelectStat());
	}

}


选购商品
public class SelectStat extends BuyStat {

	private Shop shop;

	public SelectStat(Shop shop) {
		this.shop = shop;
	}

	@Override
	public void select() {
		// TODO Auto-generated method stub
		System.out.println("I'm shop merchandise!");
		//已选购了商品
                  shop.setHasMer(true);
		//设置下一个状态
                  shop.setStat(shop.getPayStat());
	}
}



支付
public class PayStat extends BuyStat {
	private Shop shop;

	public PayStat(Shop shop) {
		this.shop = shop;
	}

	@Override
	public void inShop() {
		// TODO Auto-generated method stub
		super.inShop();
	}

	@Override
	public void packing() {
		// TODO Auto-generated method stub
		 if (shop.isHasMer()) {
			System.out.println("you don't pay!");
		} else {
			System.out.println("no merchandise don't packing");
		}
	}

	@Override
	public void pay() {
		// TODO Auto-generated method stub
		//确认是否已选购商品
                     if (shop.isHasMer()) {
			System.out.println("ok,your pay success!");
		} else {
			System.out.println("you don't have merchandise,so don't pay");
		}
		shop.setStat(shop.getPackingStat());

	}

	@Override
	public void select() {
		// TODO Auto-generated method stub

		System.out.println("time is pay,don't select merchandise");

	}

}


打包
public class PackingStat extends BuyStat {
	private Shop shop;

	public PackingStat(Shop shop) {
		this.shop = shop;
	}

	@Override
	public void packing() {
		// TODO Auto-generated method stub
		if (shop.isHasMer()) {
			System.out.println("time is packing");
			shop.setStat(shop.getWinnerStat());
			//清空已购商品状态
                           shop.setHasMer(false);
		} else {
			System.out.println("don't packing,so your not merchandise");
			shop.setStat(shop.getInShopStat());
		}
	}

	@Override
	public void pay() {
		// TODO Auto-generated method stub
		System.out.println("pay is end");
	}

	@Override
	public void select() {
		// TODO Auto-generated method stub
		System.out.println("don't select merchandise");
	}

}



抽奖
public class WinnerStat extends BuyStat {

	Random random = new Random();

	private Shop shop;

	public WinnerStat(Shop shop) {
		this.shop = shop;
	}

	@Override
	public void packing() {
		// TODO Auto-generated method stub
		System.out.println("your merchandise been packing");
	}

	@Override
	public void pay() {
		// TODO Auto-generated method stub
		System.out.println("your merchandise been pay");
	}

	@Override
	public void select() {
		// TODO Auto-generated method stub
		System.out.println("don't select merchandise");
	}

	@Override
	public void winner() {
		// TODO Auto-generated method stub
		int num = random.nextInt(10);
		if (num == 8) {
			System.out.println("you are a winner");
		} else {
			System.out.println("sorry,you are not winner");
		}
		//初始化
                  shop.setStat(shop.getInShopStat());

	}

}


超市
public class Shop {

	private BuyStat selectStat;

	private BuyStat payStat;

	private BuyStat packingStat;

	private BuyStat inShopStat;
	
	private BuyStat winnerStat;

	private boolean hasMer;

	private BuyStat stat  = null;

	public Shop() {
		selectStat = new SelectStat(this);
		payStat = new PayStat(this);
		packingStat = new PackingStat(this);
		inShopStat = new InShopStat(this);
		winnerStat = new WinnerStat(this);
		stat = inShopStat;
	}

	public void inShop() {
		stat.inShop();
	}

	public void select() {
		stat.select();
	}

	public void pay() {
		stat.pay();
	}

	public void packing() {
		stat.packing();
	}
	
	public void winner(){
		stat.winner();
	}

	public boolean isHasMer() {
		return hasMer;
	}

	public void setHasMer(boolean hasMer) {
		this.hasMer = hasMer;
	}

	public BuyStat getStat() {
		return stat;
	}

	public void setStat(BuyStat stat) {
		this.stat = stat;
	}

	public BuyStat getSelectStat() {
		return selectStat;
	}

	public BuyStat getPayStat() {
		return payStat;
	}

	public BuyStat getPackingStat() {
		return packingStat;
	}

	public BuyStat getInShopStat() {
		return inShopStat;
	}

	public BuyStat getWinnerStat() {
		return winnerStat;
	}
}


测试
public class ShopTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Shop shop = new Shop();
		shop.inShop();
		shop.select();
		
		
		shop.pay();
		
		shop.packing();
		
		shop.winner();

	}

}


使用状态值主要作用是便于以的新增和修改状态,其他的状态不会受到影响.改变了以前将状态设置为静态变量.扩展时修改很复杂现象.更加容易扩展.

以上相关例子还有一些缺陷,真是作用是理解这个模式的设计原理,能类代理以前的静态变量状态值,以后更容易维护.


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics