Adapter适用场景代码复用性强。我们一直在使用适配器。如果只有USB接口,手机就不能插入标准插座充电。这时候就需要一个适配器,一端接USB接口,另一端接插座。固定的规则是充值有抽奖机会,突然有需求需要分享充值才有机会抽奖。这时候可以使用这个设计模式案例1(来自php设计模式书)——假设一个企业网站同时销售软件服务和软件产品,目前所有的交易都在美国进行,接下来-up业务决定扩展到欧洲以增加货币转换(添加适配器)。案例2在事件业务中自行设计,但逻辑被抽离出来,无法直接运行代码。优点灵活性可扩展性非常好。目标类和适配器类是解耦的。适配器中涉及的角色包括以下几种:目标(Target):定义客户端使用的特定接口。客户端(Client):使用目标接口,配合与目标接口一致的对象。Adaptee:需要适配的现有接口。适配器(Adapter):负责将Adaptee的接口转换为Target的接口。适配器是一个具体的类,它是模式的核心。适配器有两种类型:类适配器和对象适配器,下面将详细介绍。Case1Adapterdesignpatterncasefromphpdesignpatternbook**/***EuroCalc.php*USD-可累计购买的服务和产品价格来自phpdesignpattern*/classDollarCalc{private$dollar;私人$产品;私人服务;公共$rate=1;publicfunctionrequestCalc($productNow,$serviceNow){$this->product=$productNow;$this->service=$serviceNow;$this->dollar=$this->product+$this->service;返回$this->requestCount();}privatefunctionrequestCount(){$this->dollar*=$this->rate;返回$this->dollar;}}/***EuroCalc.php*Euro-可以累计购买的服务和产品的价格*/classEuroCalc{private$euro;私人$产品;私人服务;公共$rate=1;publicfunctionrequestCalc($productNow,$serviceNow){$this->product=$productNow;$this->service=$serviceNow;$this->euro=$this->product+$this->service;返回$this->requestCount();}私有函数requestCount(){$this->euro*=$this->rate;返回$this->euro;}}/***InterfaceITarget.php*/interfaceITarget{functionrequester();}/***Example:找到一个适合欧洲插座的适配器,下面是创建这个适配器*EuroAdapter实现了一个接口和扩展类*/classEuroAdapterextendsEuroCalcimplementsITarget{publicfunction__construct(){$this->requester();}publicfunctionrequester(){$this->rate=0.8111;返回$this->rate;}}/***User*/classClient{private$euroRequest;私人$dollarRequest;公共函数__construct(){$this->euroRequest=newEuroAdapter();$this->dollarRequest=newDollarCalc();$欧元="";echo"Euros:$euros".$this->makeAdapterRequest($this->euroRequest)."
";echo"美元:".$this->makeDollarRequest($this->dollarRequest);}publicfunctionmakeAdapterRequest(ITarget$req){return$req->requestCalc(40,50);}publicfunctionmakeDollarRequest(DollarCalc$req){return$req->requestCalc(40,50);}}$worker=newClient();案例2为自动化活动抽奖主题的界面设计示例(代码只展示了相应的设计逻辑部分)添加相应的adapter增加代码复用*authorAT*/namespacecontroller;//2006年3月每个月对应的Adapter=SixMarchuse\Adapter\SixMarch\LotteryAdapter;/***彩票接口入口文件*/finalclassLotteryextendsWebController{//活动配置信息存放后台配置活动信息private$hd_info;publicfunction__construct(){//加载配置信息$this->hd_info='';}publicfunction_lottery(){//实例$lottery=newLotteryAdapter($hd_info);//获取彩票结果$rs=$lottery->_getLotteryResult();返回$rs;}}namespacecontroller;use\bbts\AppasBaseApp;/****/classLotteryAdapterextendsAutoLotteryimplementsILottery{publicfunction__construct($hd_info){//后台配置是否需要使用适配器if($hd_info['使用适配器']===true){$adapter_name=$hd_info['adapter_name'];//$适配器名称='适配器名称';测试$this->$adapter_name();}}/***adapter_name这个activity对应的adapter*/privatefunctionadapter_name(){//假设不满足自动化要求,进入游戏5分钟后才能抽奖,就添加适配器的内容,这样就不需要重写整个乐透$game_info=App::$app->model()->checkEnterGameInfo();if(strtotime($info['TIME'])-time()<300){$this->adapter_status=false;$this->adapter_code=-51;}}}namespacecontroller;use\bbts\AppasBaseApp;/***自动抽奖接口基类*/classAutoLottery{//个体开发条件状态public$adapter_status=true;公共$adapter_code=0;publicfunction_getLotteryResult(){//最后判断适配器的状态if(!$this->adapter_status){App::jetJsonpOutput($this->adapter_return_code);}//业务逻辑判断//如果通过则将数据存入$rs_status=App::$app->model()->insert($rs);//返回结果return$rs;}}?>
