書中的例子都比較淺顯易懂,不過由於是外國佬寫的,所以例子的習慣不是很附合中國特色,可能偶爾看起來有些彆扭,還有語言習慣也不是中國風。當然��看過這本書之後,你才能深刻理解設計模式到底能為你解決哪些問題,不能為你解決哪些問題(例如不能代替你的編碼)。
我將書中部分程式碼改成PHP,看下程式碼再配合概念應該是比較容易理解了。
策略模式
複製程式碼 程式碼如下:
/**
* 策略模式
* 定義了演算法族,分別封裝起來,讓它們之間可以互相替換,
* 此模式讓演算法的變化獨立於使用演算法的客戶。
* /
//飛行行為介面
interface FlyBehavior {
public function fly();
}
//呱呱叫行為介面
interface(SackBehavior {
//翅膀飛行
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)
}
}
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();
?>
?>
單一模式
複製程式碼 程式碼如下:
** 🎝>*/
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);
?>
工廠方法模式
複製程式碼 程式碼如下: 複製代碼🎜>代碼如下: 目前1/2頁 12下一頁
以上就介紹了head first設計模式 《Head First 設計模式》代碼之PHP版面向對象學習第1/2頁,包括了head first設計模式方面的內容,希望對PHP教程有興趣的朋友有所幫助。
抽象類別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") {
} 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.在用戶登錄後重新生成會話ID;2.使用安全的會話ID生成算法;3.實施會話超時機制;4.使用HTTPS加密會話數據,這些措施能確保應用在面對會話固定攻擊時堅不可摧。

實現無會話身份驗證可以通過使用JSONWebTokens(JWT)來實現,這是一種基於令牌的認證系統,所有的必要信息都存儲在令牌中,無需服務器端會話存儲。 1)使用JWT生成和驗證令牌,2)確保使用HTTPS防止令牌被截獲,3)在客戶端安全存儲令牌,4)在服務器端驗證令牌以防篡改,5)實現令牌撤銷機制,如使用短期訪問令牌和長期刷新令牌。

PHP會話的安全風險主要包括會話劫持、會話固定、會話預測和會話中毒。 1.會話劫持可以通過使用HTTPS和保護cookie來防範。 2.會話固定可以通過在用戶登錄前重新生成會話ID來避免。 3.會話預測需要確保會話ID的隨機性和不可預測性。 4.會話中毒可以通過對會話數據進行驗證和過濾來預防。

銷毀PHP會話需要先啟動會話,然後清除數據並銷毀會話文件。 1.使用session_start()啟動會話。 2.用session_unset()清除會話數據。 3.最後用session_destroy()銷毀會話文件,確保數據安全和資源釋放。

如何改變PHP的默認會話保存路徑?可以通過以下步驟實現:在PHP腳本中使用session_save_path('/var/www/sessions');session_start();設置會話保存路徑。在php.ini文件中設置session.save_path="/var/www/sessions"來全局改變會話保存路徑。使用Memcached或Redis存儲會話數據,如ini_set('session.save_handler','memcached');ini_set(

tomodifyDataNaphPsession,startTheSessionWithSession_start(),然後使用$ _sessionToset,修改,orremovevariables.1)startThesession.2)setthesession.2)使用$ _session.3)setormodifysessessvariables.3)emovervariableswithunset()

在PHP會話中可以存儲數組。 1.啟動會話,使用session_start()。 2.創建數組並存儲在$_SESSION中。 3.通過$_SESSION檢索數組。 4.優化會話數據以提升性能。

PHP會話垃圾回收通過概率機制觸發,清理過期會話數據。 1)配置文件中設置觸發概率和會話生命週期;2)可使用cron任務優化高負載應用;3)需平衡垃圾回收頻率與性能,避免數據丟失。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

WebStorm Mac版
好用的JavaScript開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

記事本++7.3.1
好用且免費的程式碼編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器