PHP Design Pattern Series - Appearance Pattern_PHP Tutorial

WBOY
Release: 2016-07-13 17:52:10
Original
861 people have browsed it

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478146.htmlTechArticleFacade design pattern hides calls by creating a simple facade interface in front of a collection of necessary logic and methods. Object complexity. Appearance design mode and builder mode are not...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!