PHP中设计模式的学习笔记
设计模式(Design Pattern)是面向对象设计中反复出现的问题的解决方案,设计模式是一种比一般类的设计更加抽象的一种思想,
它往往涉及到多个类的定义和使用。
在PHP的开发过程中,经常使用到得设计模式包括:简单工厂模式、单元素模式、观察者模式、命令模式、策略模式以及MVC模式等。
/**
* 简单工厂模式(Simple Factory Pattern),具体指创建一个类似于工厂的类,通过对类中成员方法的调用返回不同的对象
* 该模式的核心是工厂类,该类中含有必要的判断逻辑,可以决定在什么时候创建并返回哪个产品的实例化对象
*/
//定义抽象类Product
abstract class Product
{
abstract function getName();
}
//定义具体产品类A
class ProductA extends Product
{
public function getName()
{
echo "I am product A";
}
}
//定义具体产品类B
class ProductB extends Product
{
public function getName()
{
echo "I am product B";
}
}
//定义工厂类
class ProductFactory
{
static function create($name)
{
switch($name)
{
case "A":
return new ProductA();
case "B":
return new ProductB();
}
}
}
$product=ProductFactory::create("A");
$product->getName();
echo "<br/>";
$product=ProductFactory::create("B");
$product->getName();
/**
* 单元素模式(Singleton Pattern)是指对指定的类只能实例化一次。
* 该模式通常包含一个私有构造方法,用来确保无法通过创建对象或者克隆的方式对其进行实例化
* 该模式还包含有一个私有静态属性和公有静态方法。
* 公有静态方法负责对其本身进行实例化,而私有静态属性负责存储实例化的对象。
*/
class Singleton
{
private static $instance=null; //定义私有静态属性
private function __construct(){} //定义私有构造方法
public static function getInstance() //定义公有静态方法
{
if(self::$instance==null)
{
self::$instance=new Singleton(); //将实例化的对象存储在私有静态属性中
return self::$instance;
}
//return self::$instance;
}
public function printString()
{
echo "这是单元素模式,只能实例化一次。";
}
}
$single=Singleton::getInstance();
$single->printString();
//$two=Singleton::getInstance();
//$two->printString();
/**
* 观察者模式(Observer Pattern)
*/
interface IObserver
{
function onChanged($sender,$args);
}
interface IObservable
{
function addObserver($observer);
}
class UserList implements IObservable
{
private $_observers=array();
public function addObserver($observer)
{
$this->_observers[]=$observer;
}
public function addCustomer($name)
{
foreach($this->_observers as $obj)
{
$obj->onChanged($this,$name);
}
}
}
class UserListLogger implements IObserver
{
public function onChanged($sender,$args)
{
echo "'$args'被添加到了用户列表中<br/>";
}
}
$ul=new UserList();
$ul->addObserver(new UserListLogger());
$ul->addCustomer("aaa");
/**
* 命令模式(Command Pattern)
*/
interface ICommand //定义命令接口
{
function onCommand($name,$args);
}
class CommandChain
{
private $_commands=array();
public function addCommand($cmd)
{
$this->_commands[]=$cmd;
}
public function runCommand($name,$args) //执行命令
{
foreach($this->_commands as $cmd)
{
if($cmd->onCommand($name,$args)) //如果命令执行成功就退出循环
return;
}
}
}
class UserCommand implements ICommand //定义用户命令类
{
public function onCommand($name,$args)
{
if($name!='用户')
{
return false;
}
else
{
echo '用户命令处理:'.$name;
return true;
}
}
}
class MailCommand implements ICommand //定义邮件命令类
{
public function onCommand($name,$args)
{
if($name!='邮件')
{
return false;
}
else
{
echo '邮件处理命令:'.$name;
return true;
}
}
}
$cc=new CommandChain();
$cc->addCommand(new UserCommand());
$cc->addCommand(new MailCommand());
$cc->runCommand('用户',null);
echo '<br/>';
$cc->runCommand('邮件',null);
/**
* 策略模式(Strategy Pattern)
*/
interface IStrategy
{
function OnTheWay();
}
class WalkStrategy implements IStrategy
{
public function OnTheWay()
{
echo '在路上步行<br/>';
}
}
class RideBikeStrategy implements IStrategy
{
public function OnTheWay()
{
echo '在路上骑车<br/>';
}
}
class CarStrategy implements IStrategy
{
public function OnTheWay()
{
echo '在路上开车<br/>';
}
}
class Context
{
public function find($strategy)
{
$strategy->OnTheWay();
}
}
$context=new Context();
$context->find(new WalkStrategy());
$context->find(new RideBikeStrategy());
$context->find(new CarStrategy());