-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
#################################### | ||
##### FACADE DESIGN PATTERN ####### | ||
#################################### | ||
class Cart | ||
{ | ||
/** | ||
* Product added in cart | ||
* @param $products | ||
* @return void | ||
*/ | ||
public function addProducts($products) | ||
{ | ||
echo "Product added in cart<br/>"; | ||
} | ||
|
||
/** | ||
* Checkout products | ||
* @return void | ||
*/ | ||
public function getProducts() | ||
{ | ||
echo "Checkout product<br/>"; | ||
} | ||
} | ||
|
||
class Order | ||
{ | ||
/** | ||
* Process order | ||
* @param $products | ||
* @return void | ||
*/ | ||
public function process($products) | ||
{ | ||
echo "Order processing<br/>"; | ||
} | ||
} | ||
|
||
class Payment | ||
{ | ||
/** | ||
* Add additional charges | ||
* @param $charge | ||
* @return void | ||
*/ | ||
public function charge($charge) | ||
{ | ||
echo "Include additional charge and calculate total charge<br/>"; | ||
} | ||
|
||
/** | ||
* Make payment and return success or failed | ||
* @return string | ||
*/ | ||
public function makePayment() | ||
{ | ||
echo "Redirect to payment site and make payment<br/>"; | ||
return 1; // 1 means success and 0 means failed | ||
} | ||
} | ||
|
||
class Shipping | ||
{ | ||
/** | ||
* Calculate and added shipping charge | ||
* @return void | ||
*/ | ||
public function calculateCharge() | ||
{ | ||
echo "Added shipping charge<br/>"; | ||
} | ||
|
||
/** | ||
* Ship products | ||
* @return void | ||
*/ | ||
public function shipProducts() | ||
{ | ||
echo "Ship this products<br/>"; | ||
} | ||
} | ||
|
||
class CustomerFacade | ||
{ | ||
/** | ||
* Assign all classes instance | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->cart = new Cart; | ||
$this->order = new Order; | ||
$this->payment = new Payment; | ||
$this->shipping = new Shipping; | ||
} | ||
|
||
/** | ||
* Added to cart | ||
* @return Cart | ||
*/ | ||
public function addToCart($products) | ||
{ | ||
$this->cart->addProducts($products); | ||
} | ||
|
||
/** | ||
* Checkout process | ||
* @return void | ||
*/ | ||
public function checkout() | ||
{ | ||
$products = $this->cart->getProducts(); | ||
} | ||
|
||
/** | ||
* Calculate charge, make payment and ship products | ||
* @return void | ||
*/ | ||
public function makePayment() | ||
{ | ||
$charge = $this->shipping->calculateCharge(); | ||
$this->payment->charge($charge); | ||
|
||
$isCompleted = $this->payment->makePayment(); | ||
|
||
if ($isCompleted) { | ||
$this->shipping->shipProducts();; | ||
} | ||
} | ||
} | ||
|
||
// Instantiate objects of facade class | ||
$customer = new CustomerFacade; | ||
|
||
// Product List | ||
$products = [ | ||
[ | ||
'name' => 'Polo T-Shirt', | ||
'price' => 40, | ||
], | ||
[ | ||
'name' => 'Smart Watch', | ||
'price' => 400, | ||
] | ||
]; | ||
|
||
// Add to cart | ||
$customer->addToCart($products); | ||
// Checkout | ||
$customer->checkout(); | ||
// Make payment | ||
$customer->makePayment(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
#################################### | ||
##### FACADE DESIGN PATTERN ####### | ||
#################################### | ||
class Cpu | ||
{ | ||
/** | ||
* Run cpu | ||
* @return void | ||
*/ | ||
public function run() | ||
{ | ||
echo "Cpu starts to run<br/>"; | ||
} | ||
} | ||
|
||
class Memory | ||
{ | ||
/** | ||
* Run memory | ||
* @return void | ||
*/ | ||
public function run() | ||
{ | ||
echo "Memory starts to run<br/>"; | ||
} | ||
} | ||
|
||
class Monitor | ||
{ | ||
/** | ||
* Run monitor | ||
* @return void | ||
*/ | ||
public function run() | ||
{ | ||
echo "Monitor starts to run<br/>"; | ||
} | ||
} | ||
|
||
class ComputerFacade | ||
{ | ||
protected $cpu; | ||
protected $memory; | ||
protected $monitor; | ||
|
||
/** | ||
* Assign cpu, memory, monitor class instantiate | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->cpu = new Cpu(); | ||
$this->memory = new Memory(); | ||
$this->monitor = new Monitor(); | ||
} | ||
|
||
/** | ||
* Run cpu, memory, monitor | ||
* @return void | ||
*/ | ||
public function run() | ||
{ | ||
$this->cpu->run(); | ||
$this->memory->run(); | ||
$this->monitor->run(); | ||
} | ||
} | ||
|
||
// Instantiate facade class and run | ||
$computer = new ComputerFacade(); | ||
$computer->run(); |