Introduction and application of facade of PHP design pattern

Time:2021-12-1

This paper describes the introduction and application of facade of PHP design pattern. Share with you for your reference, as follows:

This appearance mode is to create a simple appearance interface in front of the collection of necessary logic and methods, and also hide the complexity of the calling object. It is very similar to the builder mode. The builder mode generally simplifies the complexity of object calls, but the external mode generally simplifies the complexity of many logical steps and method calls.

Let’s take a look at the following examples to describe:

  • Design a user class, which has an interface for getuser to obtain user information
  • When using the getuser interface, you need to set the user name and user age
  • Therefore, under normal circumstances, to call the getuser interface, you need to instantiate the user class first, then set the user information, and finally call the getuser method. This process is complex. If the user information is very large or constantly changing, calling the user information class will be a matter of great maintenance cost. For example, with the business expansion, the user’s mobile phone and address are added, Weight, marriage and other information.
  • A userfacade is designed, which has a static method getusercall, which can directly call the getuser function.

Look at the following code:

<?php
//Facade patterns hide the complexity from the calling object by creating a simple facade interface before the collection of necessary logic and methods
class User {
 protected $userName;
 protected $userAge;
 public function setUserName($userName) {
 return $this->userName = $userName;
 }
 public function setUserAge($userAge) {
 return $this->userAge = $userAge;
 }
 public function getUser() {
 Echo 'user name:'. $this - > username. '; User age: '. $this - > userage;
 }
}
//Create a user class calling interface to simplify the call to get the user getuser method
class UserFacade {
 public static function getUserCall($userInfo) {
 $User = new User;
 $User->setUserName($userInfo['username']);
 $User->setUserAge($userInfo['userAge']);
 return $User->getUser();
 }
}
$userInfo = array('username' => 'initphp', 'userAge' => 12);
UserFacade::getUserCall($userInfo); // Just one function can simplify the calling class

The above example is a classic case on the Internet, which is easy to understand.

The advantages of this appearance mode are as follows:

(1) For the caller, the access code becomes concise and very convenient

(2) Since the subsystem is called in only one place, the possibility of error is reduced

(3) The facade class also enables callers to avoid incorrect use of internal methods, thereby reducing errors

Let’s look at the structure diagram below:

There are two more pictures. Let’s have a look:

Finally, we refer to an online example to design a scene in which a shopping mall uniformly purchases household appliances and furniture, as follows:

//Household appliances - refrigerator
class BingXiang
{/*{{{*/
  public function buy()
  {
    Echo "buy a refrigerator \ R \ n";
  }
}/*}}}*/
//Household appliances - washing machines
class XiYiJi
{/*{{{*/
  public function buy()
  {
    Echo "buy a washing machine \ R \ n";
  }
}/*}}}*/
//Furniture - Chuang
class Chuang
{/*{{{*/
  public function buy()
  {
    Echo "buy a bed \ R \ n";
  }
}/*}}}*/
//Furniture - wardrobe
class YiGui
{/*{{{*/
  public function buy()
  {
    Echo "buy a wardrobe \ R \ n";
  }
}/*}}}*/
//Mall (facade)
class ShangChang
{/*{{{*/
  public $bingXiang;
  public $xiYiJi;
  public $chuang;
  public $yiGui;
  public function __construct()
  {
    $this->bingXiang = new BingXiang();
    $this->xiYiJi = new XiYiJi();
    $this->chuang = new Chuang();
    $this->yiGui = new YiGui();
  }
  //Household appliances
  public function buyJiaDian()
  {/*{{{*/
    $this->bingXiang->buy();
    $this->xiYiJi->buy();
  }/*}}}*/
  //Furniture
  public function buyJiaJu()
  {/*{{{*/
    $this->chuang->buy();
    $this->yiGui->buy();
  }/*}}}*/
}/*}}}*/
class Client
{/*{{{*/
  public static function main($argv)
  {
    $shangChang = new ShangChang();
    //Buy home appliances
    $shangChang->buyJiaDian();
    //Buy furniture
    $shangChang->buyJiaJu();
  }
}/*}}}*/
Client::main($argv);
?>

The output results can be tried manually.

Well, that’s all for this record.

For more information about PHP, readers who are interested can see the topics on this site: introduction to PHP object-oriented programming, complete operation skills of PHP array, introduction to PHP basic syntax, summary of PHP operation and operator usage, summary of PHP character string usage, introduction to PHP + MySQL database operation, and Summary of common database operation skills in PHP

I hope this article will help you in PHP programming.