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 }
글
TemplateMethod
Programming/Design Patterns
2007. 2. 21. 11:09
1 class CaffeineBeverage{
2 public function prepareRecipe():Void{
3 boilWater();
4 brew();
5 pourInCup();
6 if(hook()){
7 addCondiments();
8 }
9 }
10 public function brew():Void{
11
12 }
13 public function addCondiments():Void{
14
15 }
16 public function boilWater():Void{
17 trace("물 끓이는 중");
18 }
19 public function pourInCup():Void{
20 trace("컵에 따르는 중");
21 }
22 public function hook():Boolean{
23 return true;
24 }
25 }
/////////////////////////////////////////////
1 class Coffee extends CaffeineBeverage{
2 public function brew():Void{
3 trace("필터로 커피를 우려내는 중");
4 }
5 public function addCondiments():Void{
6 trace("설탕과 커피를 추가하는 중");
7 }
8 public function hook():Boolean{
9 return false;
10 }
11 }
//////////////////////////////////////////////
1 class Tea extends CaffeineBeverage{
2 public function brew():Void{
3 trace("차를 우려내는 중");
4 }
5 public function addCondiments():Void{
6 trace("레몬을 추가하는 중");
7 }
8 }
//////////////////////////////////////////////
1 class TemplateTest{
2 public function TemplateTest(){
3 init();
4 }
5 private function init():Void{
6 var tea:CaffeineBeverage = new Tea();
7 var coffee:CaffeineBeverage = new Coffee();
8
9 trace("Tea Test..............");
10 tea.prepareRecipe();
11
12 trace("Coffee Test...........");
13 coffee.prepareRecipe();
14 }
15 }
2 public function prepareRecipe():Void{
3 boilWater();
4 brew();
5 pourInCup();
6 if(hook()){
7 addCondiments();
8 }
9 }
10 public function brew():Void{
11
12 }
13 public function addCondiments():Void{
14
15 }
16 public function boilWater():Void{
17 trace("물 끓이는 중");
18 }
19 public function pourInCup():Void{
20 trace("컵에 따르는 중");
21 }
22 public function hook():Boolean{
23 return true;
24 }
25 }
/////////////////////////////////////////////
1 class Coffee extends CaffeineBeverage{
2 public function brew():Void{
3 trace("필터로 커피를 우려내는 중");
4 }
5 public function addCondiments():Void{
6 trace("설탕과 커피를 추가하는 중");
7 }
8 public function hook():Boolean{
9 return false;
10 }
11 }
//////////////////////////////////////////////
1 class Tea extends CaffeineBeverage{
2 public function brew():Void{
3 trace("차를 우려내는 중");
4 }
5 public function addCondiments():Void{
6 trace("레몬을 추가하는 중");
7 }
8 }
//////////////////////////////////////////////
1 class TemplateTest{
2 public function TemplateTest(){
3 init();
4 }
5 private function init():Void{
6 var tea:CaffeineBeverage = new Tea();
7 var coffee:CaffeineBeverage = new Coffee();
8
9 trace("Tea Test..............");
10 tea.prepareRecipe();
11
12 trace("Coffee Test...........");
13 coffee.prepareRecipe();
14 }
15 }
글
Adapter
Programming/Design Patterns
2007. 2. 21. 11:08
1 interface Duck{
2 public function quack():Void;
3 public function fly():Void;
4 }
////////////////////////////////////
1 class MallardDuck implements Duck{
2 public function quack():Void{
3 trace("Quack");
4 }
5 public function fly():Void{
6 trace("I'm flying");
7 }
8 }
/////////////////////////////////////
1 interface Turkey{
2 public function gobble():Void;
3 public function fly():Void;
4 }
/////////////////////////////////////
class TurkeyAdapter implements Duck{
private var turkey:Turkey;
public function TurkeyAdapter(turkey:Turkey){
this.turkey = turkey;
}
public function quack():Void{
turkey.gobble();
}
public function fly():Void{
for(var i=0;i<5;i++){
turkey.fly();
}
}
}
//////////////////////////////////////
1 class WildTurkey implements Turkey{
2 public function gobble():Void{ 3 trace("Gobble gobble");
4 }
5 public function fly():Void{
6 trace("I'm flying a short distance");
7 }
8 }
//////////////////////////////////////
1 class DuckTestDrive{
2 public function DuckTestDrive(){
3 initialize();
4 }
5 public function initialize():Void{
6 var duck:MallardDuck = new MallardDuck();
7
8 var turkey:WildTurkey = new WildTurkey();
9 var turkeyAdapter:Duck = new TurkeyAdapter(turkey);
10
11 trace("The Turkey says...");
12 turkey.gobble();
13 turkey.fly();
14
15 trace("The Duck says...");
16 testDuck(duck);
17
18 trace("The TurkeyAdapter says...");
19 testDuck(turkeyAdapter);
20 }
21 private function testDuck(duck:Duck):Void{
22 duck.quack();
23 duck.fly();
24 }
25
26 }
2 public function quack():Void;
3 public function fly():Void;
4 }
////////////////////////////////////
1 class MallardDuck implements Duck{
2 public function quack():Void{
3 trace("Quack");
4 }
5 public function fly():Void{
6 trace("I'm flying");
7 }
8 }
/////////////////////////////////////
1 interface Turkey{
2 public function gobble():Void;
3 public function fly():Void;
4 }
/////////////////////////////////////
class TurkeyAdapter implements Duck{
private var turkey:Turkey;
public function TurkeyAdapter(turkey:Turkey){
this.turkey = turkey;
}
public function quack():Void{
turkey.gobble();
}
public function fly():Void{
for(var i=0;i<5;i++){
turkey.fly();
}
}
}
//////////////////////////////////////
1 class WildTurkey implements Turkey{
2 public function gobble():Void{ 3 trace("Gobble gobble");
4 }
5 public function fly():Void{
6 trace("I'm flying a short distance");
7 }
8 }
//////////////////////////////////////
1 class DuckTestDrive{
2 public function DuckTestDrive(){
3 initialize();
4 }
5 public function initialize():Void{
6 var duck:MallardDuck = new MallardDuck();
7
8 var turkey:WildTurkey = new WildTurkey();
9 var turkeyAdapter:Duck = new TurkeyAdapter(turkey);
10
11 trace("The Turkey says...");
12 turkey.gobble();
13 turkey.fly();
14
15 trace("The Duck says...");
16 testDuck(duck);
17
18 trace("The TurkeyAdapter says...");
19 testDuck(turkeyAdapter);
20 }
21 private function testDuck(duck:Duck):Void{
22 duck.quack();
23 duck.fly();
24 }
25
26 }
글
Command
Programming/Design Patterns
2007. 2. 21. 11:08
1 interface Command{
2 public function execute();
3 }
///////////////////////////////
1 class Light{
2 private var name:String;
3 public function Light(name:String){
4 this.name = name;
5 }
6 public function lightOn():Void{
7 trace(name+" : 불을 켜다");
8 }
9 public function lightOff():Void{
10 trace(name+" : 불을 끄다");
11 }
12 }
/////////////////////////////////
1 class LightOffCommand implements Command{
2 private var light:Light;
3
4 public function LightOffCommand(light:Light){
5 this.light = light;
6 }
7 public function execute(){
8 light.lightOff();
9 }
10 }
////////////////////////////////////
1 class LightOnCommand implements Command{
2 private var light:Light;
3
4 public function LightOnCommand(light:Light){
5 this.light = light;
6 }
7 public function execute(){
8 light.lightOn();
9 }
10 }
/////////////////////////////////////
1 class RemoteControl{
2 private var onCommands:Array;
3 private var offCommands:Array;
4
5 public function RemoteControl(){
6 onCommands = new Array();
7 offCommands = new Array();
8 }
9 public function setCommand(slot:Number, onCommand:LightOnCommand, offCommand:LightOffCommand):Void{
10 onCommands[slot] = onCommand;
11 offCommands[slot] = offCommand;
12 }
13 public function onButtonWasPressed(slot:Number):Void{
14 onCommands[slot].execute();
15 }
16 public function offButtonWasPressed(slot:Number):Void{
17 offCommands[slot].execute();
18 }
19 }
//////////////////////////////////////
1 class RemoteControlTest
2 {
3 public function RemoteControlTest ()
4 {
5 init ();
6 }
7 public function init () : Void
8 {
9 var remote : RemoteControl = new RemoteControl ();
10 var light : Light = new Light ("스텐드");
11 var lightOn : LightOnCommand = new LightOnCommand (light);
12 var lightOff : LightOffCommand = new LightOffCommand (light);
13 remote.setCommand (0, lightOn, lightOff);
14 remote.onButtonWasPressed (0);
15 remote.offButtonWasPressed (0);
16 }
17 }
2 public function execute();
3 }
///////////////////////////////
1 class Light{
2 private var name:String;
3 public function Light(name:String){
4 this.name = name;
5 }
6 public function lightOn():Void{
7 trace(name+" : 불을 켜다");
8 }
9 public function lightOff():Void{
10 trace(name+" : 불을 끄다");
11 }
12 }
/////////////////////////////////
1 class LightOffCommand implements Command{
2 private var light:Light;
3
4 public function LightOffCommand(light:Light){
5 this.light = light;
6 }
7 public function execute(){
8 light.lightOff();
9 }
10 }
////////////////////////////////////
1 class LightOnCommand implements Command{
2 private var light:Light;
3
4 public function LightOnCommand(light:Light){
5 this.light = light;
6 }
7 public function execute(){
8 light.lightOn();
9 }
10 }
/////////////////////////////////////
1 class RemoteControl{
2 private var onCommands:Array;
3 private var offCommands:Array;
4
5 public function RemoteControl(){
6 onCommands = new Array();
7 offCommands = new Array();
8 }
9 public function setCommand(slot:Number, onCommand:LightOnCommand, offCommand:LightOffCommand):Void{
10 onCommands[slot] = onCommand;
11 offCommands[slot] = offCommand;
12 }
13 public function onButtonWasPressed(slot:Number):Void{
14 onCommands[slot].execute();
15 }
16 public function offButtonWasPressed(slot:Number):Void{
17 offCommands[slot].execute();
18 }
19 }
//////////////////////////////////////
1 class RemoteControlTest
2 {
3 public function RemoteControlTest ()
4 {
5 init ();
6 }
7 public function init () : Void
8 {
9 var remote : RemoteControl = new RemoteControl ();
10 var light : Light = new Light ("스텐드");
11 var lightOn : LightOnCommand = new LightOnCommand (light);
12 var lightOff : LightOffCommand = new LightOffCommand (light);
13 remote.setCommand (0, lightOn, lightOff);
14 remote.onButtonWasPressed (0);
15 remote.offButtonWasPressed (0);
16 }
17 }
글
Singleton
Programming/Design Patterns
2007. 2. 21. 11:08
1 class Singleton{
2 private static var uniqueInstance;
3
4 private function Singleton(){
5
6 }
7 public static function getInstance():Singleton{
8 if(uniqueInstance == null){
9 uniqueInstance = new Singleton();
10 }
11 return uniqueInstance;
12 }
13 public function getName(){
14 trace("Singleton pattern");
15 }
16 }
2 private static var uniqueInstance;
3
4 private function Singleton(){
5
6 }
7 public static function getInstance():Singleton{
8 if(uniqueInstance == null){
9 uniqueInstance = new Singleton();
10 }
11 return uniqueInstance;
12 }
13 public function getName(){
14 trace("Singleton pattern");
15 }
16 }