jasu's blog
블로그 메뉴글
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 }