欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-09 22:53:16
书中的例子都比较浅显易懂,不过由于是外国佬写的,所以例子的习惯不是很附合中国特色,可能偶尔看起来有些别扭,还有语言习惯也不是中国风。当然��看过...
书中的例子都比较浅显易懂,不过由于是外国佬写的,所以例子的习惯不是很附合中国特色,可能偶尔看起来有些别扭,还有语言习惯也不是中国风。当然��看过这本书之后,你才能深刻理解设计模式到底能为你解决哪些问题,不能为你解决哪些问题(比如不能代替你的编码)。
  我将书中部分代码改成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');
?>


1