PHP design pattern series-decorator _ PHP Tutorial

Source: Internet
Author: User
PHP design pattern series-decorator. What is the decorator mode? adjust part of the content or functions of an existing object, but you do not need to modify the original object structure. you can use the decorator to design an application scenario. what is the decorator?
Decorator mode. you can use the decorator to adjust some content or functions of an existing object, but you do not need to modify the original object structure.
Application scenarios
Design a UserInfo class containing the UserInfo array to store user name information.
Use addUser to add a user name
The getUserList method prints the username information.
Now we need to change the added user information to uppercase. We do not need to change the original class and do not change the original data structure.
We have designed a UserInfoDecorate class to complete this operation. just like decoration, we have decorated the original data.
The decorator mode is similar to the adapter mode. However, you must note that the decorator mode does not change the existing object data structure.
Code: UserInfo
[Php]
// Decorator mode. you can use the decorator design mode to adjust some content or functions of an existing object, but do not need to modify the original object structure.
Class UserInfo {

Public $ userInfo = array ();

Public function addUser ($ userInfo ){
$ This-> userInfo [] = $ userInfo;
}

Public function getUserList (){
Print_r ($ this-> userInfo );
}
}

Code: like UserInfoDecorate, the output of user information is changed to uppercase without changing the UserInfo class.
[Php]
Include ("UserInfo. php ");
Class UserInfoDecorate {

Public function makeCaps ($ UserInfo ){
Foreach ($ UserInfo-> userInfo as & $ val ){
$ Val = strtoupper ($ val );
}
}

}

$ UserInfo = new UserInfo;
$ UserInfo-> addUser ('zhu ');
$ UserInfo-> addUser ('initphp ');
$ UserInfoDecorate = new UserInfoDecorate;
$ UserInfoDecorate-> makeCaps ($ UserInfo );
$ UserInfo-> getUserList ();

Author: initphp

The decoration device mode adjusts some content or functions of existing objects, but does not need to modify the original object structure. you can use the decoration tool to design an application scenario...

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.