步骤1.定义策略接口
#UserStrategy.php 用户策略步骤2.实现策略业务
#FemaleStrategy.php 女性用户策略继续添加策略
#MaleStrategy.php 男性用户策略步骤3.在实际业务场景中运用策略
本实例的业务场景为:根据男女、性用户自动区分广告及分类
#index.php 默认业务访问入口 strategy->showAd(); echo "
"; echo "显示分类:"; $this->strategy->showCategory(); } //设置显示策略 public function setStrategy(celvmoshiUserStrategy $strategy)//(约定接口类型) { $this->strategy = $strategy; } } $page = new Page(); if (isset($_GET['female'])) { $userStrategy = new celvmoshiFemaleStrategy(); } else if (isset($_GET['male'])) { $userStrategy = new celvmoshiMaleStrategy(); } else { return; } $page->setStrategy($userStrategy); $page->index();
至此已大功告成!



