标签:设计模式之php项目应用 php设计模式 策略模式 自动驾驶系统 依赖倒置原则
1)strategy.php
<?php
/**
* strategy.php
*
* 策略类
*
* Copyright (c) 2015 http://blog.csdn.net/CleverCode
*
* modification history:
* --------------------
* 2015/5/14, by CleverCode, Create
*
*/
// 定义自动驾驶汽车能够实现的操作
interface ICar{
// 启动
public function run();
// 停止
public function stop();
// 转弯
public function turn();
}
// 本田汽车
class HondaCar implements ICar{
/**
* 启动
*
* @return void
*/
public function run(){
echo "本田车启动了!\r\n";
}
/**
* 转弯
*
* @return void
*/
public function turn(){
echo "本田车拐弯了!\r\n";
}
/**
* 停止
*
* @return void
*/
public function stop(){
echo "本田车停止了!\r\n";
}
}
// 福特汽车
class FordCar implements ICar{
/**
* 启动
*
* @return void
*/
public function run(){
echo "福特车启动了!\r\n";
}
/**
* 转弯
*
* @return void
*/
public function turn(){
echo "福特车拐弯了!\r\n";
}
/**
* 停止
*
* @return void
*/
public function stop(){
echo "福特车停止了!\r\n";
}
}
// 吉普汽车
class JeepCar implements ICar{
/**
* 启动
*
* @return void
*/
public function run(){
echo "吉普车启动了!\r\n";
}
/**
* 转弯
*
* @return void
*/
public function turn(){
echo "吉普车拐弯了!\r\n";
}
/**
* 停止
*
* @return void
*/
public function stop(){
echo "吉普车停止了!\r\n";
}
}
<?php
/**
* strategyPattern.php
*
* 设计模式:策略模式
*
* Copyright (c) 2015 http://blog.csdn.net/CleverCode
*
* modification history:
* --------------------
* 2015/5/14, by CleverCode, Create
*
*/
// 加载所有的策略
include_once ('strategy.php');
// 创建一个环境类,根据不同的需求调用不同策略
class AutoSystem{
// 策略
private $_car = null;
/**
* 构造函数
*
* @param string $type 类型
* @return void
*/
public function __construct($type = null){
if (!isset($type)) {
return;
}
$this->setCar($type);
}
/**
* 设置策略(简单工厂与策略模式配合使用)
*
* @param string $type 类型
* @return void
*/
public function setCar($type){
$cs = null;
switch ($type) {
// 本田汽车
case 'Honda' :
$cs = new HondaCar();
break;
// 福特汽车
case 'Ford' :
$cs = new FordCar();
break;
// 吉普汽车
case 'Jeep' :
$cs = new JeepCar();
break;
}
$this->_car = $cs;
}
/**
* 启动汽车
*
* @return void
*/
public function runCar(){
$this->_car->run();
}
/**
* 转弯汽车
*
* @return void
*/
public function turnCar(){
$this->_car->turn();
}
/**
* 停止汽车
*
* @return void
*/
public function stopCar(){
$this->_car->stop();
}
}
/*
* 客户端类
* 让客户端和业务逻辑尽可能的分离,降低客户端和业务逻辑算法的耦合,
* 使业务逻辑的算法更具有可移植性
*/
class Client{
public function main(){
$autoSystem = new AutoSystem();
// 选择汽车应用自动驾驶系统
$autoSystem->setCar('Honda');
$autoSystem->runCar();
$autoSystem->turnCar();
$autoSystem->stopCar();
$autoSystem->setCar('Ford');
$autoSystem->runCar();
$autoSystem->turnCar();
$autoSystem->stopCar();
$autoSystem->setCar('Jeep');
$autoSystem->runCar();
$autoSystem->turnCar();
$autoSystem->stopCar();
}
}
/**
* 程序入口
*/
function start(){
$client = new Client();
$client->main();
}
start();
?>3) 依赖倒置原则:A.高层次的模块不应该依赖于低层次的模块,他们都应该依赖于抽象。B.抽象不应该依赖于具体,具体应该依赖于抽象。
版权声明:
1)原创作品,出自"CleverCode的博客",转载时请务必注明以下原创地址,否则追究版权法律责任。
2)原创地址:http://blog.csdn.net/clevercode/article/details/45723773(转载务必注明该地址)。
3)欢迎大家关注我博客更多的精彩内容:http://blog.csdn.net/CleverCode。
标签:设计模式之php项目应用 php设计模式 策略模式 自动驾驶系统 依赖倒置原则
原文地址:http://blog.csdn.net/clevercode/article/details/45723773