WEB开发网
开发学院软件开发Java hand first 设计模式 -状态模式 阅读

hand first 设计模式 -状态模式

 2009-09-17 00:00:00 来源:WEB开发网   
核心提示: 选购商品 Java代码publicclassSelectStatextendsBuyStat{privateShopshop;publicSelectStat(Shopshop){this.shop=shop;}@Overridepublicvoidselect(){//TODOAuto-gen

选购商品

Java代码   

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()); 
 } 
}

支付

Java代码   

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"); 
 
 } 
 
}

上一页  1 2 3 4  下一页

Tags:hand first 设计模式

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接