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

hand first 设计模式 -状态模式

 2009-09-17 00:00:00 来源:WEB开发网   
核心提示: 打包 Java代码publicclassPackingStatextendsBuyStat{privateShopshop;publicPackingStat(Shopshop){this.shop=shop;}@Overridepublicvoidpacking(){//TODOAuto-ge

打包

Java代码   

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

抽奖

Java代码   

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

上一页  1 2 3 4  下一页

Tags:hand first 设计模式

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