PHP design pattern series-appearance pattern

Source: Internet
Author: User
& Nbsp; the appearance mode hides the complexity of calling objects by creating a simple appearance interface before the set of required logic and methods. 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. In the application scenario, design a User class, which contains the getUser interface for getting User information. when using the getUser interface, set the 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

 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

// Create a User class Call Interface to simplify the call 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.