jasu's blog
블로그 메뉴글
Decorator
Programming/Design Patterns
2007. 2. 21. 11:07
1 class Beverage{
2 private var descriptions:String = "제목없음";
3 public function getDescription():String{
4 return descriptions;
5 }
6 public function cost():Number{
7 return 0;
8 }
9 }
////////////////////////////////////
1 class CondimentDecorator extends Beverage{
2 public function getDepscription(){
3
4 }
5 }
////////////////////////////////////
1 class DarkRoast extends Beverage{
2 public function DarkRoast(){
3 descriptions = "다트로스트 커피";
4 }
5 public function cost():Number{
6 return 2.89;
7 }
8 }
/////////////////////////////////////
1 class Espresso extends Beverage{
2 public function Espresso(){
3 descriptions = "에스프레소";
4 }
5 public function cost():Number{
6 return 1.99;
7 }
8 }
//////////////////////////////////////
1 class HouseBlend extends Beverage{
2 public function HouseBlend(){
3 descriptions = "하우스 브렌드 커피";
4 }
5 public function cost():Number{
6 return .89;
7 }
8 }
//////////////////////////////////////
1 class Mocha extends CondimentDecorator{
2 private var beverage:Beverage;
3 public function Mocha(beverage:Beverage){
4 this.beverage = beverage;
5 }
6 public function getDescription():String{
7 return beverage.getDescription()+", 모카";
8 }
9 public function cost():Number{
10 return .20+beverage.cost();
11 }
12 }
///////////////////////////////////////
1 class Whip extends Beverage{
2 private var beverage:Beverage;
3 public function Whip(beverage:Beverage){
4 this.beverage = beverage; 5 } 6 public function getDescription():String{
7 return beverage.getDescription()+", 휘핑";
8 }
9 public function cost():Number{
10 return .20+beverage.cost();
11 }
12 }
///////////////////////////////////////
1 class Main{
2 public function Main(){
3 init();
4 }
5 private function init(){
6 var beverage:Beverage = new Espresso();
7 trace(beverage.getDescription()+" $"+beverage.cost());
8
9 var beverage2:Beverage = new DarkRoast();
10 beverage2 = new Mocha(beverage2);
11 beverage2 = new Mocha(beverage2);
12 beverage2 = new Whip(beverage2);
13 trace(beverage2.getDescription()+" $"+beverage2.cost());
14
15 var beverage3:Beverage = new HouseBlend();
16 beverage3 = new Mocha(beverage3);
17 beverage3 = new Whip(beverage3);
18 trace(beverage3.getDescription()+" $"+beverage3.cost());
19
20 }
21 }
2 private var descriptions:String = "제목없음";
3 public function getDescription():String{
4 return descriptions;
5 }
6 public function cost():Number{
7 return 0;
8 }
9 }
////////////////////////////////////
1 class CondimentDecorator extends Beverage{
2 public function getDepscription(){
3
4 }
5 }
////////////////////////////////////
1 class DarkRoast extends Beverage{
2 public function DarkRoast(){
3 descriptions = "다트로스트 커피";
4 }
5 public function cost():Number{
6 return 2.89;
7 }
8 }
/////////////////////////////////////
1 class Espresso extends Beverage{
2 public function Espresso(){
3 descriptions = "에스프레소";
4 }
5 public function cost():Number{
6 return 1.99;
7 }
8 }
//////////////////////////////////////
1 class HouseBlend extends Beverage{
2 public function HouseBlend(){
3 descriptions = "하우스 브렌드 커피";
4 }
5 public function cost():Number{
6 return .89;
7 }
8 }
//////////////////////////////////////
1 class Mocha extends CondimentDecorator{
2 private var beverage:Beverage;
3 public function Mocha(beverage:Beverage){
4 this.beverage = beverage;
5 }
6 public function getDescription():String{
7 return beverage.getDescription()+", 모카";
8 }
9 public function cost():Number{
10 return .20+beverage.cost();
11 }
12 }
///////////////////////////////////////
1 class Whip extends Beverage{
2 private var beverage:Beverage;
3 public function Whip(beverage:Beverage){
4 this.beverage = beverage; 5 } 6 public function getDescription():String{
7 return beverage.getDescription()+", 휘핑";
8 }
9 public function cost():Number{
10 return .20+beverage.cost();
11 }
12 }
///////////////////////////////////////
1 class Main{
2 public function Main(){
3 init();
4 }
5 private function init(){
6 var beverage:Beverage = new Espresso();
7 trace(beverage.getDescription()+" $"+beverage.cost());
8
9 var beverage2:Beverage = new DarkRoast();
10 beverage2 = new Mocha(beverage2);
11 beverage2 = new Mocha(beverage2);
12 beverage2 = new Whip(beverage2);
13 trace(beverage2.getDescription()+" $"+beverage2.cost());
14
15 var beverage3:Beverage = new HouseBlend();
16 beverage3 = new Mocha(beverage3);
17 beverage3 = new Whip(beverage3);
18 trace(beverage3.getDescription()+" $"+beverage3.cost());
19
20 }
21 }