Appearance Mode
The facade design pattern hides the complexity of the calling object by creating a simple facade interface in front of a collection of required logic and methods.
The appearance design pattern is very similar to the builder pattern. The builder pattern generally simplifies the complexity of object calls, and the appearance pattern generally simplifies the complexity of many logical steps and method calls.
Application scenarios
Design a User class with a getUser interface to obtain user information
When using the getUser interface, you need to set the user's username and user age
So under normal circumstances, when calling 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 complicated. If there is a lot of user information, or if it is constantly changing, call the user information class. It will be a very expensive thing to maintain. For example, as the business expands, the user's mobile phone, address, weight, marital status and other information are added.
A UserFacade is designed, which contains a static method getUserCall, which can directly call the getUser function.
Code: getUser class
[php]
//Facade pattern, by creating a simple facade interface in front of a collection of necessary logic and methods, the facade design pattern hides the complexity from the calling object
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;
}
}
Code: UserFacade user class appearance interface, a getUserCall interface
[php] www.2cto.com
//Create a User class calling interface to simplify the call to get the user's 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
Author: initphp