《Head First 设计模式》代码之PHP版(面向对象学习)第1/2页

2015-01-24信息快讯网

《Head First 设计模式》是本不错的讲解设计模式的书,不像F4写的那么枯燥,应该算是比较容易理解的好书。

书中的例子都比较浅显易懂,不过由于是外国佬写的,所以例子的习惯不是很附合中国特色,可能偶尔看起来有些别扭,还有语言习惯也不是中国风。当然��看过这本书之后,你才能深刻理解设计模式到底能为你解决哪些问题,不能为你解决哪些问题(比如不能代替你的编码)。
  我将书中部分代码改成PHP,看下代码再配合概念应该是比较容易理解了。

策略模式
 
<?php 
/** 
* 策略模式 
* 定义了算法族,分别封装起来,让它们之间可以互相替换, 
* 此模式让算法的变化独立于使用算法的客户。 
*/ 
//飞行行为接口 
interface FlyBehavior { 
public function fly(); 
} 
//呱呱叫行为接口 
interface QuackBehavior { 
public function quack(); 
} 
//翅膀飞行 
class FlyWithWings implements FlyBehavior { 
public function fly() { 
echo "I'm flying!!\n"; 
} 
} 
//不会飞 
class FlyNoWay implements FlyBehavior { 
public function fly() { 
echo "I can't fly!\n"; 
} 
} 
class FlyRocketPowered implements FlyBehavior { 
public function fly() { 
echo "I'm flying with a rocket!\n"; 
} 
} 
class Qquack implements QuackBehavior { 
public function quack() { 
echo "Quack\n"; 
} 
} 
class Squeak implements QuackBehavior { 
public function quack() { 
echo "Squeak\n"; 
} 
} 
class MuteQuack implements QuackBehavior { 
public function quack() { 
echo "<< Silence >>\n"; 
} 
} 
abstract class Duck { 
protected $quack_obj; 
protected $fly_obj; 
public abstract function display(); 

public function performQuack() { 
$this->quack_obj->quack(); 
} 
public function performFly() { 
$this->fly_obj->fly(); 
} 
public function swim() { 
echo "All ducks float, even decoys!\n"; 
} 
public function setFlyBehavior(FlyBehavior $fb) { 
$this->fly_obj = $fb; 
} 
public function setQuackBehavior(QuackBehavior $qb) { 
$this->quack_obj = $qb; 
} 
} 

class ModelDuck extends Duck { 
public function __construct() { 
$this->fly_obj = new FlyNoWay(); 
$this->quack_obj = new MuteQuack(); 
} 
public function display() { 
echo "I'm a model duck!\n"; 
} 
} 

$model = new ModelDuck(); 
$model->performFly(); 
$model->performQuack(); 
//提供新的能力 
$model->setFlyBehavior(new FlyRocketPowered()); 
$model->setQuackBehavior(new Squeak()); 
$model->performFly(); 
$model->performQuack(); 

?> 

单件模式
 
<?php 
/** 
* 单件模式 
* 确保一个类只有一个实例,并提供一个全局访问点。 
*/ 
class MyClass { 
private static $uniqueInstance; 
private function __construct() { 

} 
public static function getInstance() { 
if (self::$uniqueInstance == null) { 
self::$uniqueInstance = new MyClass(); 
} 
return self::$uniqueInstance; 
} 
} 
$myClass = MyClass::getInstance(); 
var_dump($myClass); 
$myClass = MyClass::getInstance(); 
var_dump($myClass); 
?> 

工厂方法模式
 
<?php 
abstract class PizzaStore { 
public function orderPizza($type) { 
$pizza = $this->createPizza($type); 

$pizza->prepare(); 
$pizza->bake(); 
$pizza->cut(); 
$pizza->box(); 
return $pizza; 
} 

public abstract function createPizza($type); 
} 
class NYPizzaStore extends PizzaStore { 
public function createPizza($type) { 
if ($type == "cheese") { 
return new NYStyleCheesePizza(); 
} elseif ($type == "veggie") { 
return new NYStyleVeggiePizza(); 
} elseif ($type == "clam") { 
return new NYStyleClamPizza(); 
} elseif ($type == "papperoni") { 
return new NYStylePapperoniPizza(); 
} else { 
return null; 

} 
} 
} 
class ChicagoPizzaStore extends PizzaStore { 
public function createPizza($type) { 
if ($type == "cheese") { 
return new ChicagoStyleCheesePizza(); 
} elseif ($type == "veggie") { 
return new ChicagoStyleVeggiePizza(); 
} elseif ($type == "clam") { 
return new ChicagoStyleClamPizza(); 
} elseif ($type == "papperoni") { 
return new ChicagoStylePapperoniPizza(); 
} else { 
return null; 
} 
} 
} 
abstract class Pizza { 
public $name; 
public $dough; 
public $sauce; 
public $toppings = array(); 

public function prepare() { 
echo "Preparing " . $this->name . "\n"; 
echo "Yossing dough...\n"; 
echo "Adding sauce...\n"; 
echo "Adding toppings: \n"; 
for ($i = 0; $i < count($this->toppings); $i++) { 
echo " " . $this->toppings[$i] . "\n"; 
} 
} 

public function bake() { 
echo "Bake for 25 minutes at 350\n"; 
} 

public function cut() { 
echo "Cutting the pizza into diagonal slices\n"; 
} 

public function box() { 
echo "Place pizza in official PizzaStore box\n"; 
} 

public function getName() { 
return $this->name; 
} 
} 

class NYStyleCheesePizza extends Pizza { 
public function __construct() { 
$this->name = "NY Style Sauce and cheese Pizza"; 
$this->dough = "Thin Crust Dough"; 
$this->sauce = "Marinara Sauce"; 

$this->toppings[] = "Grated Reggiano Cheese"; 
} 
} 

class NYStyleVeggiePizza extends Pizza { 
public function __construct() { 
$this->name = "NY Style Sauce and veggie Pizza"; 
$this->dough = "Thin Crust Dough"; 
$this->sauce = "Marinara Sauce"; 

$this->toppings[] = "Grated Reggiano veggie"; 
} 
} 
class NYStyleClamPizza extends Pizza { 
public function __construct() { 
$this->name = "NY Style Sauce and clam Pizza"; 
$this->dough = "Thin Crust Dough"; 
$this->sauce = "Marinara Sauce"; 

$this->toppings[] = "Grated Reggiano clam"; 
} 
} 
class NYStylePapperoniPizza extends Pizza { 
public function __construct() { 
$this->name = "NY Style Sauce and papperoni Pizza"; 
$this->dough = "Thin Crust Dough"; 
$this->sauce = "Marinara Sauce"; 

$this->toppings[] = "Grated Reggiano papperoni"; 
} 
} 

class ChicagoStyleCheesePizza extends Pizza { 
public function __construct() { 
$this->name = "Chicago Style Deep Dish Cheese Pizza"; 
$this->dough = "Extra Thick Crust Dough"; 
$this->sauce = "Plum Tomato Sauce"; 

$this->toppings[] = "Shredded Mozzarella Cheese"; 
} 

public function cut() { 
echo "Cutting the pizza into square slices\n"; 
} 
} 

$myStore = new NYPizzaStore(); 
$chicagoStore = new ChicagoPizzaStore(); 
$pizza = $myStore->orderPizza("cheese"); 
echo "Ethan ordered a " . $pizza->getName() . "\n"; 

$pizza = $chicagoStore->orderPizza("cheese"); 
echo "Ethan ordered a " . $pizza->getName() . "\n"; 

?> 

工厂模式
 
<?php 
abstract class PizzaStore { 
public function orderPizza($type) { 
$pizza = $this->createPizza($type); 

$pizza->prepare(); 
$pizza->bake(); 
$pizza->cut(); 
$pizza->box(); 
return $pizza; 
} 

public abstract function createPizza($type); 
} 
class NYPizzaStore extends PizzaStore { 
public function createPizza($type) { 
$pizza = null; 
$ingredientFactory = new NYPizzaIngredientFactory(); 
if ($type == "cheese") { 
$pizza = new CheesePizza($ingredientFactory); 
$pizza->setName('New York Style Cheese Pizza'); 
} elseif ($type == "veggie") { 
$pizza = new VeggiePizza($ingredientFactory); 
$pizza->setName('New York Style Veggie Pizza'); 
} elseif ($type == "clam") { 
$pizza = new ClamPizza($ingredientFactory); 
$pizza->setName('New York Style Clam Pizza'); 
} elseif ($type == "papperoni") { 
$pizza = new PapperoniPizza($ingredientFactory); 
$pizza->setName('New York Style Papperoni Pizza'); 
} 
return $pizza; 
} 
} 
class ChicagoPizzaStore extends PizzaStore { 
public function createPizza($type) { 
if ($type == "cheese") { 
return new ChicagoStyleCheesePizza(); 
} elseif ($type == "veggie") { 
return new ChicagoStyleVeggiePizza(); 
} elseif ($type == "clam") { 
return new ChicagoStyleClamPizza(); 
} elseif ($type == "papperoni") { 
return new ChicagoStylePapperoniPizza(); 
} else { 
return null; 
} 
} 
} 
interface PizzaIngredientFactory { 
public function createDough(); 
public function createSauce(); 
public function createCheese(); 
public function createVeggies(); 
public function createPepperoni(); 
public function createClam(); 
} 
class NYPizzaIngredientFactory implements PizzaIngredientFactory { 
public function createDough() { 
return new ThinCrustDough(); 
} 
public function createSauce() { 
return new MarinaraSauce(); 
} 
public function createCheese() { 
return new ReggianoCheese(); 
} 
public function createVeggies() { 
$veggies = array( 
new Garlic(), 
new Onion(), 
new Mushroom(), 
new RedPepper(), 
); 
return $veggies; 
} 
public function createPepperoni() { 
return new SlicedPepperoni(); 
} 
public function createClam() { 
return new FreshClams(); 
} 
} 
class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory { 
public function createDough() { 
return new ThickCrustDough(); 
} 
public function createSauce() { 
return new PlumTomatoSauce(); 
} 
public function createCheese() { 
return new Mozzarella(); 
} 
public function createVeggies() { 
$veggies = array( 
new BlackOlives(), 
new Spinach(), 
new EggPlant(), 
); 
return $veggies; 
} 
public function createPepperoni() { 
return new SlicedPepperoni(); 
} 
public function createClam() { 
return new FrozenClams(); 
} 
} 
abstract class Pizza { 
public $name; 
public $dough; 
public $sauce; 
public $veggies = array(); 
public $cheese; 
public $pepperoni; 
public $clam; 

public abstract function prepare(); 

public function bake() { 
echo "Bake for 25 minutes at 350\n"; 
} 

public function cut() { 
echo "Cutting the pizza into diagonal slices\n"; 
} 

public function box() { 
echo "Place pizza in official PizzaStore box\n"; 
} 

public function getName() { 
return $this->name; 
} 

public function setName($name) { 
$this->name = $name; 
} 

public function __toString() { 

} 
} 

class CheesePizza extends Pizza { 
public $ingredientFactory; 

public function __construct(PizzaIngredientFactory $ingredientFactory) { 
$this->ingredientFactory = $ingredientFactory; 
} 

public function prepare() { 
echo "Preparing " . $this->name . "\n"; 
$this->dough = $this->ingredientFactory->createDough(); 
$this->sauce = $this->ingredientFactory->createSauce(); 
$this->cheese = $this->ingredientFactory->createCheese(); 
} 
} 

class ClamPizza extends Pizza { 
public $ingredientFactory; 

public function __construct(PizzaIngredientFactory $ingredientFactory) { 
$this->ingredientFactory = $ingredientFactory; 
} 

public function prepare() { 
echo "Preparing " . $this->name . "\n"; 
$this->dough = $this->ingredientFactory->createDough(); 
$this->sauce = $this->ingredientFactory->createSauce(); 
$this->cheese = $this->ingredientFactory->createCheese(); 
$clam = $this->ingredientFactory->createClam(); 
} 
} 

$nyPizzaStore = new NYPizzaStore(); 
$nyPizzaStore->orderPizza('cheese'); 
?> 


PHP循环语句笔记(foreach,list)
PHP中strtotime函数使用方法详解
用PHP写的基于Memcache的Queue实现代码
php中XMLHttpRequest(Ajax)不能设置自定义的Referer的解决方法
php中$_REQUEST、$_POST、$_GET的区别和联系小结
PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数
php数组函数序列之array_search()- 按元素值返回键名
php数组函数序列之each() - 获取数组当前内部指针所指向元素的键名和键值,并将指针移到下一位
提示Trying to clone an uncloneable object of class Imagic的解决
jQuery EasyUI API 中文文档 - DateBox日期框
php方法调用模式与函数调用模式简例
PHP下利用shell后台运行PHP脚本,并获取该脚本的Process ID的代码
PHP 命令行工具 shell_exec, exec, passthru, system详细使用介绍
PHP正确配置mysql(apache环境)
判断Keep-Alive模式的HTTP请求的结束的实现代码
PHP header函数分析详解
php设计模式 Visitor 访问者模式
关于php mvc开发模式的感想
php开启安全模式后禁用的函数集合
PHP常用代码大全(新手入门必备)
PHP print类函数使用总结
php $_SERVER["REQUEST_URI"]获取值的通用解决方法
php中static静态变量的使用方法详解
php Static关键字实用方法
PHP stristr() 函数(不区分大小写的字符串查找)
php strstr查找字符串中是否包含某些字符的查找函数
Discuz!下Memcache缓存实现方法
PHP 工厂模式使用方法
php 提速工具eAccelerator 配置参数详解
memcached 和 mysql 主从环境下php开发代码详解
php select,radio和checkbox默认选择的实现方法
php checkbox复选框值的获取与checkbox默认值输出方法
php imagecreatetruecolor 创建高清和透明图片代码小结
PHP foreach循环使用详解与实例代码
在Windows下编译适用于PHP 5.2.12及5.2.13的eAccelerator.dll(附下载)
asp.net Repeater控件的说明及详细介绍及使用方法
Windows下安装Memcached的步骤说明
php中的观察者模式
初次接触php抽象工厂模式(Elgg)
PHP MVC模式在网站架构中的实现分析
网页游戏开发入门教程二(游戏模式+系统)
©2014-2024 dbsqp.com