In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of combination mode and decoration mode in PHP. Xiaobian thinks it is quite practical, so share it with you as a reference. I hope you can gain something after reading this article.
The details are as follows:
combination mode
Definition: The composite pattern defines a single-rooted inheritance system that enables collections with distinct responsibilities to work side by side.
A military case,
The army integrates its members and outputs firepower
output:
48
The army further integrates with other armies
output:
48
96
Better way to support add, remove, etc. Other features.
output:
Army Object( [units:Army:private] => Array ( [0] => Soldier Object ( ) [1] => Tank Object ( ) [2] => Tank Object ( ) ))16Army Object( [units:Army:private] => Array ( [1] => Tank Object ( ) [2] => Tank Object ( ) ))8
Add exception handling
output:
Fatal error: Uncaught exception 'UnitException' with message 'Archer is a leaf'
Comment: All classes in the composite pattern share the same parent type, making it easy to add new composite or partial objects to your design without extensive code modifications.
Final effect, gradual optimization (perfect):
output:
attacking with strength: 60
More powerful combinations,
output:
Army Object( [units:CompositeUnit:private] => Array ( [0] => Archer Object ( ) [1] => Archer Object ( ) [2] => Army Object ( [units:CompositeUnit:private] => Array ( [0] => Archer Object ( ) [1] => Archer Object ( ) [2] => LaserCannonUnit Object ( ) ) ) ))
Comments: Unit basics, CompositeUnit composite to achieve add and remove. Armies inherit Composite, archers inherit Archer. This way there are no redundant add and remove methods in archers.
decorator pattern
Decorative patterns help us change the functionality of specific components.
Look at examples.
output:
-2
Comment: Without flexibility, we cannot obtain funding for both diamonds and contaminated land.
Decorative patterns use composition and delegation rather than inheritance alone to solve the problem of functional variation.
See examples:
output:
2
4
0
Comments: This model is scalable. We don't need to create DiamondPollutionPlains objects to construct a diamond-contaminated object.
A more realistic example
output:
AuthenticateRequest: authenticating requestStructureRequest: structuring requestLogRequest: logging requestMainProcess: doing something useful with requestAuthenticateRequest Object( [processrequest:protected] => StructureRequest Object ( [processrequest:protected] => LogRequest Object ( [processrequest:protected] => MainProcess Object ( ) ) )) About "PHP combination pattern and decoration pattern example analysis" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.