首頁 > 後端開發 > php教程 > head first設計模式 《Head First 設計模式》代碼之PHP版物件導向學習第1/2頁

head first設計模式 《Head First 設計模式》代碼之PHP版物件導向學習第1/2頁

WBOY
發布: 2016-07-29 08:43:07
原創
1077 人瀏覽過

書中的例子都比較淺顯易懂,不過由於是外國佬寫的,所以例子的習慣不是很附合中國特色,可能偶爾看起來有些彆扭,還有語言習慣也不是中國風。當然��看過這本書之後,你才能深刻理解設計模式到底能為你解決哪些問題,不能為你解決哪些問題(例如不能代替你的編碼)。
  我將書中部分程式碼改成PHP,看下程式碼再配合概念應該是比較容易理解了。
策略模式

複製程式碼 程式碼如下:


/**
* 策略模式
* 定義了演算法族,分別封裝起來,讓它們之間可以互相替換,
* 此模式讓演算法的變化獨立於使用演算法的客戶。
* /
//飛行行為介面
interface FlyBehavior {
public function fly();
}
//呱呱叫行為介面
interface(SackBehavior { public function 介面}
//翅膀飛行
class FlyWithWings implements FlyBehavior {
public function fly() {
echo "I'm flying!!n";
}
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 implementsments🎜>}
}
class Qquack implementsments public function quack() {
echo "Quackn";
}
}
class Squeak implements QuackBehavior {
public function quack() {
echo "Squeakn"; }
}
class MuteQuack implements QuackBehavior {
public function quack() {
echo ">n";
}
}
abstract class Duck.
protected $quack_obj;
protected $fly_obj;
public abstract function display();
public function performQuack() {
$this->quack_obj->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) public function setQuackBehavior(QuackBehavior $qb) 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 FlyetRocketPowered ());
$model->setQuackBehavior(new Squeak());
$model->performFly();
$model->performQuack();
?>
performQuack();
?>


單一模式

複製程式碼 程式碼如下:


** 🎝>*/
class MyClass {
private static $uniqueInstance;
private function __construct() {
}
::public static function getInstance() {
if uniqueInstance == null) {
self::$uniqueInstance = new MyClass();
}
return self::$uniqueInstance;
}
}
$myClass = MyClass:$myClass = MyClass:$myClass = MyClass:$myClass = MyClass:$myClass :getInstance();
var_dump($myClass);
$myClass = MyClass::getInstance();
var_dump($myClass);
?>



工廠方法模式



複製程式碼 程式碼如下:


抽象類別PizzaStore {
public function orderPizza($type) {
$pizza = $this->createPizza($type);
$pizza-> prepare();
$pizza->bake();
$pizza->cut();
$pizza->box();
回傳$pizza;
}
}
回傳$pizza;
}
}
公有抽象函數createPizza($type);
}
class NYPizzaStore 擴充PizzaStore {
public function createPizza($type) {
if ($type == "cheese") {
return new NYStyleCheesePizza(); }
} elseif ($type == "veggie") {
return new NYStyleVeggiePizza();
} elseif ($type == "clam") { re new NYStyleClamPizza();
} elseif ($type == "papperoni") {
return new NYStylePapperoniPizza();
} else {
回傳null
}
} 🎜>}
class ChicagoPizzaStore 擴充PizzaStore {
public function createPizza($type) {
if ($type == "cheese") {
return new } elseif ($type == "veggie") {
return new ChicagoStyleVeggiePizza();
} elseif ($type == "clam") {
return new ChicagoStyleClamPizza()}
elseif ($type == "papperoni") {
return new ChicagoStylePapperoniPizza();
} else {
回傳null;
}
}
}
}
} {
public $name;
公用$麵團;
公用$醬;
public $toppings = array();
public function prepare() {
echo "準備中" 。 $這個->名稱。 “n”;
echo“喲辛麵團...n”;
echo“加醬...n”;
echo "添加配料:n";
for ($i = 0; $i toppings); $i++) {
echo " " . $this->toppings[$i] 。 「n」;
}
}
public function Baker() {
echo "在350n 下烘烤25 分鐘";
}
public function cut() {
echo "將披薩切成對角線片n";
}
public function box() {
echo "將披薩放入官方PizzaStore boxn";
}
public function getName () {
return $this->name;
}
}
class NYStyleCheesePizza extends Pizza {
public function __construct() {
$this->name = "NY Style Sauce and Cheese Pizza";
$this->dough = "薄皮麵團";
$this->sauce = "Marinara 醬";
$this->toppings[] = "磨碎的雷吉亞諾起司";
}
}
class NYStyleVeggiePizza extends Pizza {
public function __construct() {
$this->name = "NY Style Sauce and Veggie Pizza"; 🎜>$this->dough = "薄皮麵團";
$this->sauce = "Marinara 醬";
$this->toppings[] = "磨碎的雷吉亞諾蔬菜";
}
}
class NYStyleClamPizza extends Pizza {
public function __construct() {
$this->name = "NY Style Sauce and clam Pizza";
$this->name = "NY Style Sauce and clam Pizza";
$this->name = "NY Style Sauce and clam Pizza";
$-this-this> = "薄皮麵團";
$this->sauce = "Marinara 醬";
$this->toppings[] = "磨碎的雷吉亞諾蛤";
}
}
class NYStylePapperoniPizza extends Pizza {
public function __construct() {
$this->name = "NY Style Sauce and papperoni Pizza";
$this->dough = "薄皮麵團"; 🎜>$this->sauce = "Marinara 醬";
$this->toppings[] = "磨碎的Reggiano papperoni";
}
}
class ChicagoStyleCheesePizza extends Pizza {
public function __construct() {
$this->name = "芝加哥風格深盤起司披薩";
$this->dough = "超厚皮麵團";
$this->sauce = "梅子番茄醬";
$this->toppings[] = "馬蘇里拉起司絲";
}
public function cut() {
echo "將披薩切成方形片n ";
}
}
$myStore = new NYPizzaStore();
$chicagoStore = new ChicagoPizzaStore();
$pizza = $myStore->Pizza("cheese"); 🎜>echo「伊森訂了一份」。 $pizza->getName() 。 「n」;
$pizza = $chicagoStore->orderPizza("cheese");
echo「伊森訂了一份」。 $pizza->getName() 。 “n”;
? >



工廠模式

複製代碼🎜>代碼如下:


抽象類別PizzaStore {
public function orderPizza($type) {
$pizza = $this->createPizza($type);
$pizza-> prepare();
$pizza->bake();
$pizza->cut();
$pizza->box();
回傳$pizza;
}
}
回傳$pizza;
}
}
公有抽象函數createPizza($type);
}
class NYPizzaStore 擴充PizzaStore {
public function createPizza($type) {
$pizza = null; $ingFactoryIienty = newyredat 或者);
if ($type == "cheese") {
$pizza = new CheesePizza($ingredientFactory);
$pizza->setName('紐約風味芝士披薩');
} elseif ($type == "veggie") {
$pizza = new VeggiePizza($ingredientFactory);
$pizza->setName('紐約式素食披薩');
} elseif ($type = = "clam") {
$pizza = new ClamPizza($ingredientFactory);
$pizza->setName('紐約式蛤蜊披薩');
} elseif ($type == "papperoni") {
$pizza = new PapperoniPizza($ingredientFactory);
$pizza->setName('紐約式帕帕羅尼披薩');
}
返回$pizza;
}
}
class ChicagoPizzaStore 擴充PizzaStore {
public function createPizza($type) {
if ($type == "cheese") {
return new ChicagoStyleCheesePizza(); elseif ($type == "veggie") {
return new ChicagoStyleVeggiePizza();
} elseif ($type == "clam") {
return new ChicagoStyleClamPizza(); $type == "papperoni") {
return new ChicagoStylePapperoniPizza();
} else {
回傳null;
}
}
} public function createDough();
公用函數createSauce();
公用函數createCheese();
公用函數createVeggies();
公用函數createPepperoni();
公用函數);
}
class NYPizzaIngredientFactory 實作PizzaIngredientFactory {
public function createDough() {
return new ThinCrustDough();
}
return new ThinCrustDough();
}
public function" MarinaraSauce();
}
public function createCheese() {
return new ReggianoCheese();
}
public function createVeggies() {
}
public function createVeggies() {
} new Onion(),
new Mushroom(),
new RedPepper (),
);
回傳$蔬菜;
}
public function 。 {
public function createDough() {
return new ThickCrustDough();
}
public function createSauce() {
return new PlumTomatoSauce(); return new Mozzarella();
}
public function createVeggies() {
$veggies = array(
new BlackOlives(),
new Spinach(),
new EggPlant(),
);
返回$蔬菜;
}
public function createPepperoni() {
return new SlicedPepperoni();
}
return new SlicedPepperoni();
function createClam() {
return new FrozenClams();
}
}
抽象類別Pizza {
public $name;
公有麵團;
公有醬;
public $veggies = array();
公共$奶酪;
公共$意大利辣香腸;
公共$蛤;
公共抽象函數prepare();
public function bread () {
echo "在350n 烘烤25 分鐘";
}
public function cut() {
echo "將披薩切成對角線片n";
}
public function box() {
echo "將披薩放入官方PizzaStore boxn";
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function __toString() {
}
}
擴展 CheesePizza 擴充Pizza 擴充{
public $ingredientFactory;
public function __construct(PizzaIngredientFactory $ingredientFactory) {
$this->ingredientFactory = $ingredientFactory;
}
public functionpref.中" . $這個->名稱。 「n」;
$this->dough = $this->ingredientFactory->createDough();
$this->sauce = $this->ingredientFactory->createSauce();
$this- >cheese = $this->ingredientFactory->createCheese();
}
}
class ClamPizza 擴充Pizza {
public $ingredientFactory;
public function __construct(PizzFactora? 🎜>$this->ingredientFactory = $ingredientFactory;
}
public functionprepare() {
echo "準備中" . $這個->名稱。 「n」;
$this->dough = $this->ingredientFactory->createDough();
$this->sauce = $this->ingredientFactory->createSauce();
$this- >cheese = $this->ingredientFactory->createCheese();
$clam = $this->ingredientFactory->createClam();
}
}
$nyPizzaStore = newPizzaStore();
$nyPizzaStore->orderPizza('起司');
? >


目前1/2頁 12下一頁

以上就介紹了head first設計模式 《Head First 設計模式》代碼之PHP版面向對象學習第1/2頁,包括了head first設計模式方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板