Skip to content

Commit

Permalink
Facade Design Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
razu1991 committed Nov 19, 2022
1 parent 41445dc commit 150b94e
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 0 deletions.
152 changes: 152 additions & 0 deletions Facade/example_one.php
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();
71 changes: 71 additions & 0 deletions Facade/example_two.php
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();

0 comments on commit 150b94e

Please sign in to comment.