[AS]デザインパターン「Adapter」の練習
矢沢久雄の早わかりGoFデザインパターン(2) のAdapterの項をAS3で書いてみた。
こんなかんじでいいんだろうか?
Kyuuyoprog.as(ドキュメントクラス)
package { import flash.display.Sprite; /** * ... * @author 393 */ public class KyuuyoProg extends Sprite { public function KyuuyoProg() { init(); } private function init():void{ var shacho:Shacho = new Shacho(10000); var shainAdapter:ShainAdapter = new ShainAdapter(shacho); var shachokyuuyo:Number = getKyuuyo(shainAdapter); var shain1:Shain = new Shain(90, 80); var kyuuyo:Number = getKyuuyo(shain1); trace( "shachokyuuyo : " + shachokyuuyo ); //出力:shachokyuuyo : 10000 trace( "kyuuyo : " + kyuuyo ); //出力:kyuuyo : 5670 } private function getKyuuyo(obj:Shain):Number{ return obj.getJikan() * obj.getJikyu(); } } }
Shain.as
package { /** * ... * @author 393 */ public class Shain { private var _jikyu:Number; private var _jikan:Number; public function Shain(jikyu:Number = 0, jikan:Number = 0) { this._jikyu= jikyu; this._jikan= jikan; } public function getJikan():Number { //搾取 return _jikan - 10; } public function getJikyu():Number { //搾取 return _jikyu * 0.9; } } }
Shacho.as
package { /** * ... * @author 393 */ public class Shacho { private var _koteikyu:Number; public function Shacho(koteikyu:Number) { _koteikyu = koteikyu } public function get koteikyu():Number { return _koteikyu; } } }
ShainAdapter.as(アダプタークラス)
package { /** * ... * @author 393 */ public class ShainAdapter extends Shain { private var shachoObj:Shacho; public function ShainAdapter(obj:Shacho) { shachoObj = obj; } public override function getJikan():Number { return 1; } public override function getJikyu():Number { return shachoObj.koteikyu; } } }
Tweet