Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of combination pattern and Decoration Mode in PHP

Shulou Source: shulou.com Published: 2022-06-03 20:19:46 09月28日 Update

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.

Tags: Patterns combinations troops objects methods diamonds processing pollution examples functions firepower articles examples analysis lots soldiers shooters attributes more static Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Microsoft Apple OPPO Reno NVidia