Facades are used to simplify the use of existing complex interfaces. It isolates a client from a sub-system so that the client can interact with the facade rather than with the sub-system. This is especially useful when working with legacy code, third-party code, complex code, or hard to refactor/test code. So in short, a mask turns some **** into something good.
A good frontend will have a constructor with interface-type-indicating parameters and no new keyword.
How to use it
- Create a square that will be the mask.
- Pass the parent class to the constructor of the facade as an interface-type-hint parameter.
- Create methods that use the methods of the parent class.
How it works
- The client requests something.
- The request goes to the mask.
- The facade communicates with related subsystems to work.
- Sub-system responses are returned after the job is done – (Optional).
- Return to Client – (Optional).
Example
<?php
interface VegetableInterface
{
public function doit();
}
class WashingVeg implements VegetableInterface
{
public function doit()
{
echo "My Vegetables is washed done";
}
}
class Chopping implements VegetableInterface
{
public function doit()
{
echo "Vegetable chopped,onion etc done";
}
}
class AddingOil implements VegetableInterface
{
public function doit()
{
echo "Adding Oil in cooker and onion cook upto 5 mints";
}
}
class AddingWater implements VegetableInterface
{
public function doit()
{
echo "Some water is added";
}
}
class Cooked implements VegetableInterface
{
public function doit()
{
echo "Put all veg and wait 10 mint for making it";
}
}
$washingObj = new WashingVeg();
$washingObj->doit();
$choppingObj = new Chopping();
$choppingObj->doit();
$addingOilObj = new AddingOil();
$addingOilObj->doit();
$addingWaterObj = new AddingWater();
$addingWaterObj->doit();
$CookerObj = new Cooked();
$CookerObj->doit();
?>
As you can see above, we are cooking vegetables. We have repeated the same step four times. It will be even more painful. What would be an ideal scenario to cook vegetables in one step. To better rephrase the above code, we can create a class called VegMakingFacade that handles all the tasks needed to create rice as follows:
<?php
interface VegetableInterface
{
public function doit();
}
class WashingVeg implements VegetableInterface
{
public function doit()
{
echo "My Vegetables is washed done";
}
}
class Chopping implements VegetableInterface
{
public function doit()
{
echo "Vegetable chopped,onion etc done";
}
}
class AddingOil implements VegetableInterface
{
public function doit()
{
echo "Adding Oil in cooker and onion cook upto 5 mints";
}
}
class AddingWater implements VegetableInterface
{
public function doit()
{
echo "Some water is added";
}
}
class Cooked implements VegetableInterface
{
public function doit()
{
echo "Put all veg and wait 10 mint for making it";
}
}
class VegMakingFacade
{
public static function makeVeg()
{
(new WashingVeg)->doit();
(new Chopping)->doit();
(new AddingOil)->doit();
(new AddingWater)->doit();
(new Cooked)->doit();
}
}
VegMakingFacade::makeVeg();
All the complexities of instantiating different classes to make vegetables are handled by the VegMakingFacade class and its only static method called makeVeg() and the user can easily create vegetables by just writing a single line of code.
jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The jQuery Certification exam by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.