策略模式

来源:互联网 发布:sql server 官网 编辑:程序博客网 时间:2024/06/05 11:26
<?php
interface Strategy
{
public function showAd();
public function showGt();
}




require_once "Strategy.php";
class MaleGrategy implements Strategy
{
public function showAd()
{
echo "Male Ad<br/>";
}
public function showGt()
{
echo "Male Gt<br/>";
}
}




require_once "Strategy.php";
class FemaleGrategy implements Strategy
{
public function showAd()
{
echo "Female Ad<br/>";
}
public function showGt()
{
echo "Female Gt<br/>";
}
}




require_once "MaleGrategy.php";
require_once "FemaleGrategy.php";
class Index
{
public $object;
public function setStrategy($object)
{
$this->object = $object;
}


public function showAd()
{
$this->object->showAd();
}


public function showGt()
{
$this->object->showGt();
}
}




if(isset($_GET['type']) && $_GET['type'] == "male")
{
$object = new MaleGrategy();
} else {
$object = new FemaleGrategy();
}




$index = new Index();
$index->setStrategy($object);
$index->showAd();
$index->showGt();






?> 
原创粉丝点击