<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/8/23
* Time: 16:22
*/
interface Shoes
{
public function ShoesList();
}
class Man implements Shoes
{
public function ShoesList()
{
echo "man list";
}
}
class Woman implements Shoes
{
public function ShoesList()
{
echo "woman list";
}
}
class UserStrategy
{
public $strategy;
public function __construct(Shoes $shoes)
{
$this->strategy = $shoes;
}
public function ShoesList()
{
$this->strategy->ShoesList();
}
}
$sex = 1;
switch ($sex)
{
case 1:
(new UserStrategy(new Man()))->ShoesList();
break;
case 2:
(new UserStrategy(new Woman()))->ShoesList();
break;
}
結果:
D:\myapache\www\demo\guancazhe>php Strategy.php
man list
使用策略模式,使得我們的程序可以根據不同的參數,調取不同的結果。