重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

PHP设计模式:策略模式

步骤1.定义策略接口

创新互联公司专注于恭城企业网站建设,自适应网站建设,商城开发。恭城网站建设公司,为恭城等地区提供建站服务。全流程按需求定制开发,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务

#UserStrategy.php 用户策略

步骤2.实现策略业务

#FemaleStrategy.php 女性用户策略

继续添加策略

#MaleStrategy.php 男性用户策略

步骤3.在实际业务场景中运用策略

本实例的业务场景为:根据男女、性用户自动区分广告及分类

#index.php 默认业务访问入口

strategy->showAd();

        echo "
";         echo "显示分类:";         $this->strategy->showCategory();     }     //设置显示策略     public function setStrategy(celvmoshi\UserStrategy $strategy)//(约定接口类型)     {         $this->strategy = $strategy;     } } $page = new Page(); if (isset($_GET['female'])) {     $userStrategy = new celvmoshi\FemaleStrategy(); } else if (isset($_GET['male'])) {     $userStrategy = new celvmoshi\MaleStrategy(); } else {     return; } $page->setStrategy($userStrategy); $page->index();

至此已大功告成!


标题名称:PHP设计模式:策略模式
文章网址:http://cqcxhl.cn/article/gcpocj.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP