一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

《Head First 設計模式》代碼之PHP版(面向對象學習)第1/2頁

書中的例子都比較淺顯易懂,不過由于是外國佬寫的,所以例子的習慣不是很附合中國特色,可能偶爾看起來有些別扭,還有語言習慣也不是中國風。當然��看過這本書之后,你才能深刻理解設計模式到底能為你解決哪些問題,不能為你解決哪些問題(比如不能代替你的編碼)。
  我將書中部分代碼改成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技術《Head First 設計模式》代碼之PHP版(面向對象學習)第1/2頁,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 日本免费新一区二区三区 | 久久香蕉国产精品一区二区三 | 久久久久青草大香线综合精品 | 2017天天干| 视频国产一区 | 国产精品女在线观看 | 玖玖精品在线视频 | 亚欧成人乱码一区二区 | 国产精品1区2区 | 中文字幕精品在线观看 | 婷婷视频网站 | 国产精品欧美久久久久天天影视 | 亚洲网色 | 韩日成人 | 国产资源在线观看 | 国产精品久久久香蕉 | 激情视频一区 | 国产亚洲综合成人91精品 | 日韩专区中文字幕 | 婷婷亚洲综合五月天小说在线 | 国产人成激情视频在线观看 | 91亚洲免费视频 | 日韩一区二区国色天香 | 四色成人网 | 国产一级特黄一级毛片 | 久久亚洲国产成人亚 | 一本大道久久香蕉成人网 | 久久精品国产国语对白 | 精品香蕉一区二区三区 | 色狠狠狠狠综合影视 | 六月婷婷啪啪 | 一二三区在线观看 | 亚洲精品国产精品乱码不97 | 四虎永久在线精品国产免费 | 成人免费动作大片黄在线 | 免费观看成人鲁鲁鲁鲁鲁视频 | 成人精品视频在线观看播放 | 免费观看欧美一级特黄 | 视频一区亚洲 | 久久综合加勒比 | 欧美日韩亚洲国产千人斩 |