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