Last active
August 14, 2022 02:27
-
-
Save thephucit/3b96e7e9d501f43eb8ae12486fa5f958 to your computer and use it in GitHub Desktop.
các design patterns thông dụng trong php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# đây là một mẫu lập trình biến việc sử dụng nhiều class với các phương thức khác nhau về một interface đơn giản với một phương thức, | |
# nói một cách đơn giản hơn là thay vì thực hiện nhiều lời gọi phương thức chúng ta chỉ thực hiện gọi một phương thức. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# là một supper class dùng để tạo ra nhiều factory design pattern khác |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# adapter design pattern | |
# khi project phụ thuộc vào các api, thirt party hoặc các class khác mà có xu hướng thường xuyên thay đổi | |
# thì ta cần tạo một lớp khác định nghĩa lại phương thức của class đó gọi là adapter, để khi nó có thay đổi thì chỉ cần vào | |
# adapter thay đổi tên phương thức của nó mà ko bị ảnh hưởng đến toàn project | |
class PayPal | |
{ | |
public function __construct() {} | |
public function sendPayment($amount) | |
{ | |
echo "Paying via PayPal: ". $amount; | |
} | |
} | |
interface paymentAdapter | |
{ | |
public function pay($amount); | |
} | |
class paypalAdapter implements paymentAdapter | |
{ | |
private $paypal; | |
public function __construct(PayPal $paypal) | |
{ | |
$this->paypal = $paypal; | |
} | |
public function pay($amount) | |
{ | |
$this->paypal->sendPayment($amount); | |
} | |
} | |
$paypal = new paypalAdapter(new PayPal()); | |
$paypal->pay('2629'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# factory design pattern | |
# nhằm tạo ra một đối tượng động, để gom các đối tượng có cách xử lý giống nhau, có tham số giống nhau | |
# không cần biết phương thức của các lớp con thực hiện thế nào, mà chỉ cần yêu cầu đến factory, | |
# nó sẽ cung cấp chính xác điều mà chúng ta muốn. | |
interface Shape { | |
const SQUARE = 1; | |
const CIRCLE = 2; | |
const RECTANGLE = 3; | |
function draw(); | |
} | |
class Circle implements Shape | |
{ | |
public function draw() | |
{ | |
echo "Draw circle"; | |
} | |
} | |
class Square implements Shape | |
{ | |
public function draw() { | |
echo "Draw square"; | |
} | |
} | |
class ShapeFactory | |
{ | |
public function getShape($type) { | |
switch ($type) { | |
case Shape::SQUARE: | |
return new Square; | |
break; | |
case Shape::CIRCLE: | |
return new Circle; | |
break; | |
default: | |
return null | |
break; | |
} | |
return null; | |
} | |
} | |
$factory = new ShapeFactory(); | |
$shapeCircle = $factory->getShape(Shape::CIRCLE); | |
$shapeCircle->draw(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# singleton design pattern | |
# đảm bảo việc khởi tạo đối tượng chỉ diễn ra một lần trong suốt quá trình chạy ứng dụng. | |
# ngăn chặn việc tạo một object thông qua từ khoá new | |
class DatabaseManager { | |
private static $singletonObj = null; | |
private function __construct() { | |
} | |
public static function createSingletonObject() { | |
if (self::$singletonObj !== null) { | |
return self::$singletonObj; | |
} | |
self::$singletonObj = new self(); | |
return self::$singletonObj; | |
} | |
} | |
$con1 = DatabaseManager::createSingletonObject(); | |
$con2 = DatabaseManager::createSingletonObject(); | |
var_dump($con1 === $con2); # return true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment