Appearance Mode
By creating a simple appearance interface before the set of required logic and methods, the appearance design mode hides the complexity of calling objects.
The appearance design mode is very similar to the builder mode. The Builder mode is generally used to simplify the complexity of object calling. The appearance mode is generally used to simplify the complexity of many logical steps and method calls.
Application scenarios
Design a User class with the getUser interface for getting User information
When using the getUser interface, you must set the user name and age.
Therefore, in normal cases, to call the getUser interface, you need to instantiate the User class, set the User information, and then call the getUser method. This process is complex. If there are many User information, or changing, calling user information will be costly, for example, adding information such as the user's mobile phone number, address, weight, and marital status as the business expands.
A UserFacade is designed with a static method getUserCall, which can directly call the getUser function.
Code: getUser class
[Php]
<? Php
// Appearance mode. By creating a simple appearance interface before a set of required logic and methods, the appearance design mode hides the complexity of the called 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 call 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); // a function can simplify the call class.
Author: initphp