デザインモード--ポリシーモード


ポリシー・モード
ポリシー・モードは、通常、抽象的なベース・クラスを定義し、状況に応じて異なるクラスを作成してベース・クラスを継承します.次に,実際の状況の判断に基づいて,このベースクラスを異なる方式で継承する.
次のコードは、クライアントブラウザのタイプに応じて異なる文字式を出力する機能を実現します.
phpは$SERVER["HTTP_USER_AGENT"ユーザ側情報を取得する
 
<?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["HTTP_USER_AGENT"]),"IEAGENT")
{
$currPage = new ieAgent();
}
else
{
$currPage = new otherAgent();
}
echo $currPage->PrintPage();
?>