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...