---
title: "设计模式--策略模式"
author: "tiehao"
date: "2009-12-13T22:24:35Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-02-02T16:08:54Z"
id: 547689
url: "https://tiehao.iteye.com/blog/547689"
---

策略模式
    策略模式通常通过定义一个抽象的基类，然后根据不同的情况创建不同的类继承这个基类。接下来，根据实际情况的判断，对这个基类采用不同的方式进行继承。
以下代码实现了根据客户端浏览器的类型输出不同的文字表达式的功能
  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();
?>