PHP design pattern series-appearance Pattern

Source: Internet
Author: User

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

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.