jasu's blog
블로그 메뉴글
Iterator
Programming/Design Patterns
2007. 2. 21. 11:09
1 class DinerMenu{
2 public static var MAX_ITEMS = 6;
3 private var numberOfItems = 0;
4 private var menuItems:Array;
5
6 public function DinerMenu(){
7 menuItems = new Array();
8
9 addItem("채식주의자용 BLT", "통밀 위에(식물성) 베이컨, 상추, 토마토를 얹은 메뉴", true, 2.99);
10 addItem("BLT", "통밀 위에 베이컨, 상추, 토마토를 얹은 메뉴", false, 2.99);
11 addItem("오늘의 스프", "감자 샐러드를 곁들인 오늘의 스프", false, 3.29);
12 addItem("핫도그", "사워크라우트, 갖은 양념, 양파, 치즈가 곁들여진 핫도그", false, 3.05);
13
14 }
15
16 public function addItem(name:String, description:String, vegetarian:Boolean, price:Number):Void{
17 var menuItem:MenuItem = new MenuItem(name, description, vegetarian, price);
18 if(numberOfItems >= MAX_ITEMS){
19 trace("죄송합니다. 메뉴가 꽉 찼습니다. 더이상 추가할 수 없습니다.");
20 }else{
21 menuItems[numberOfItems] = menuItem;
22 numberOfItems+=1;
23 }
24 }
25 /* public function getMenuItems():Array{
26 return menuItems;
27 }*/
28 public function createIterator():Iterator{
29 return new DinerMenuIterator(menuItems);
30 }
31
32 }
////////////////////////////////////////
1 class DinerMenuIterator implements Iterator{
2 private var items:Array;
3 private var position:Number;
4
5 public function DinerMenuIterator(items:Array){
6 this.items = items;
7 this.position = 0;
8 }
9 public function next():Object{
10 var menuItem:MenuItem = items[position];
11 position += 1;
12 return menuItem;
13 }
14 public function hasNext():Boolean{
15 if(position>= items.length || items[position] == null){
16 return false;
17 }else{
18 return true;
19 }
20 }
21 }
///////////////////////////////////////////
1 interface Iterator{
2 public function hasNext():Boolean;
3 public function next():Object;
4 }
///////////////////////////////////////////
1 class MenuItem{
2 private var name;
3 private var description:String;
4 private var vegetarian:Boolean;
5 private var price:Number;
6
7 public function MenuItem(name:String, description:String, vegetarian:Boolean, price:Number){
8 this.name = name;
9 this.description = description;
10 this.vegetarian = vegetarian;
11 this.price = price;
12 }
13 public function getName():String{
14 return name;
15 }
16 public function getDescription():String{
17 return description;
18 }
19 public function getPrice():Number{
20 return price;
21 }
22 public function isVegetarian():Boolean{
23 return vegetarian;
24 }
25 public function toString():String{
26 return name+" of MenuItem Object\n";
27 }
28 }
//////////////////////////////////////////
1 class PancakeHouseMenu{
2 private var menuItems:Array;
3
4 public function PancakeHouseMenu(){
5 menuItems = new Array();
6 addItem("K&B 팬케이크 세트", "스크램블드 에그와 포스트가 곁들여진 팬케이크", true, 2.99);
7 addItem("레귤러 팬케이크 세트", "달걀 후라이와 소시지가 곁들여진 팬케이크", false, 2.99);
8 addItem("블루베리 팬케이크", "신선한 블루베리와 블루베리 시럽으로 만든 팬케이크", true, 3.49);
9 addItem("와플", "와플, 취향에 따라 블루베리나 딸기를 얹을 수 있습니다.", true, 3,59);
10 }
11 public function addItem(name:String, description:String, vegetarian:Boolean, price:Number):Void{
12 var menuItem:MenuItem = new MenuItem(name, description, vegetarian, price);
13 menuItems.push(menuItem);
14 }
15 /* public function getMenuItems():Array{
16 return menuItems;
17 }*/
18 public function createIterator():Iterator{
19 return new PancakeIterator(menuItems);
20 }
21 }
/////////////////////////////////////////////
1 class PancakeIterator implements Iterator{
2 private var items:Array;
3 private var position:Number;
4
5 public function PancakeIterator(items:Array){
6 this.items = items;
7 this.position = 0;
8 }
9 public function next():Object{
10 var menuItem:MenuItem = items[position];
11 position += 1;
12 return menuItem;
13 }
14 public function hasNext():Boolean{
15 if(position>= items.length || items[position] == null){
16 return false;
17 }else{
18 return true;
19 }
20 }
21 }
////////////////////////////////////////////////
1 class Waitress{
2 private var pancakeHouseMenu:PancakeHouseMenu;
3 private var dinerMenu:DinerMenu;
4
5 public function Waitress(pancakeHouseMenu:PancakeHouseMenu, dinerMenu:DinerMenu){
6 this.pancakeHouseMenu = pancakeHouseMenu;
7 this.dinerMenu = dinerMenu;
8 }
9 public function printMenu():Void{
10 var pancakeIterator:Iterator = pancakeHouseMenu.createIterator();
11 var dinerIterator:Iterator = dinerMenu.createIterator();
12 trace("메뉴 ----- \n 아침 메뉴");
13 printMenuList(pancakeIterator);
14 trace("\n점심 메뉴");
15 printMenuList(dinerIterator);
16 }
17 private function printMenuList(iterator:Iterator):Void{
18 while(iterator.hasNext()){
19 var menuItem:MenuItem = MenuItem(iterator.next());
20 trace(menuItem.getName()+", ");
21 trace(menuItem.getPrice()+"--");
22 trace(menuItem.getDescription());
23 }
24
25 }
26 }
////////////////////////////////////////////
1 class MenuTestDrive{
2 public function MenuTestDrive(){
3 initialize();
4 }
5 private function initialize():Void{
6 var pancakeHouseMenu:PancakeHouseMenu = new PancakeHouseMenu();
7 var dinerMenu:DinerMenu = new DinerMenu();
8 var waitress:Waitress = new Waitress(pancakeHouseMenu, dinerMenu);
9 waitress.printMenu();
10 }
11 }