《Head First 设计模式》代码之PHP版(面向对象学习)
前端之家收集整理的这篇文章主要介绍了
《Head First 设计模式》代码之PHP版(面向对象学习),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
书中的例子都比较浅显易懂,不过由于是外国佬写的,所以例子的习惯不是很附合中国特色,可能偶尔看起来有些别扭,还有语言习惯也不是中国风。当然��看过这本书之后,你才能深刻理解设计模式到底能为你解决哪些问题,不能为你解决哪些问题(比如不能代替你的编码)。
我将书中部分代码改成PHP,看下代码再配合概念应该是比较容易理解了。 策略模式
<div class="codetitle"><a style="CURSOR: pointer" data="63726" class="copybut" id="copybut63726" onclick="doCopy('code63726')"> 代码如下:
<div class="codebody" id="code63726">
<?
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(); ?>
单件模式
<div class="codetitle">
<a style="CURSOR: pointer" data="55440" class="copybut" id="copybut55440" onclick="doCopy('code55440')"> 代码如下: <div class="codebody" id="code55440">
<?
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);
?>
工厂
方法模式
<div class="codetitle">
<a style="CURSOR: pointer" data="33414" class="copybut" id="copybut33414" onclick="doCopy('code33414')"> 代码如下: <div class="codebody" id="code33414">
<?
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"; ?>