设计模式--策略模式
策略模式
策略模式通常通过定义一个抽象的基类,然后根据不同的情况创建不同的类继承这个基类。接下来,根据实际情况的判断,对这个基类采用不同的方式进行继承。
以下代码实现了根据客户端浏览器的类型输出不同的文字表达式的功能
php是用$_SERVER<?php
abstract class baseAgent
{
abstract function PrintPage();
}
class ieAgent exrend baseAgent { function PrintPage() { return "当前浏览器是IE!"; } }
class otherAgent exrend baseAgent { function PrintPage() { return "当前浏览器不是IE!"; } }
if(isset($_server),"IEAGENT") { $currPage = new ieAgent(); } else { $currPage = new otherAgent(); } echo $currPage->PrintPage(); ?>