In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "the usage of Visitor pattern and composition pattern in PHP object-oriented". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the usage of Visitor pattern and composition pattern in PHP object-oriented.
Look directly at the code:
/ / the base class abstract class Unit {abstract function bombardStrength (); / / gets the attack power of the unit / / this method will call the visitor class and pass itself to it function accept (ArmyVisitor $visitor) {$method = "visit". Get_class ($this); $visitor- > $method ($this); / / call the methods of the visitor class, where "visit" is used. Get_class ($this) makes up the name of the method} / / sets a depth, although it will be called later, but this method is not important to understand the pattern and can be ignored. (there is often some code in the original sample code that does not have much to do with understanding the principle of the pattern) protected function setDepth ($depth) {$this- > depth = $depth } function getDepth () {return $this- > depth;}} / / Archer class Archer extends Unit {function bombardStrength () {return 4;}} / / Laser cannon class LaserCannonUnit extends Unit {function bombardStrength () {return 44;}} / / Cavalry class Cavalry extends Unit {function bombardStrength () {return 2; / / the attack power of cavalry is lower than that of Archers? }} / / used to combine instances that inherit the unit class, and let the Army and TroopCarrier classes inherit the removeUnit and addUnit methods, leaving the base class because the above three classes are already the smallest units, not a military group, the removeUnit and addUnit methods are of no use to them. Abstract class CompositeUnit extends Unit {private $units = array (); / / stores any instance that inherits the unit class function getComposite () {/ / this method is mainly used to determine whether the current instance is a CompositeUnit class return $this;} protected function units () {return $this- > units } function removeUnit (Unit $unit) {/ / Delete a military unit $this- > units = array_udiff ($this- > units,array ($unit), function ($ameme / b) {return ($a = = $b)? 0unit 1;}) } function addUnit (Unit $unit) {/ / add a military unit if (in_array ($unit,$this- > units,true)) {return;} $unit- > setDepth ($this- > depth + 1); $this- > units [] = $unit;} function bombardStrength () {$ret = 0 Foreach ($this- > units as $unit) {$ret + = $unit- > bombardStrength ();} return $ret;} function accept (Armyvisitor $visitor) {/ / call visitor parent::accept ($visitor) / / call the accept method of the base class, in the first client code usage, an information of the military group as a whole will be saved foreach ($this- > units as $thisunit) {/ / call the military unit accept method, and in the first client code usage, the information of each military unit will be saved $thisunit- > accept ($visitor). } / / Army class Army extends CompositeUnit {} / / Fleet class TroopCarrier extends CompositeUnit {} / / Visitor class abstract class ArmyVisitor {abstract function visit (Unit $node) / / the business logic to be executed by the visitor function visitArcher (Archer $node) {/ / in fact, I think it is enough for understanding that this abstract class has an abstract method visit (). The original text also adds the following methods to call visit / / in a circle. $this- > visit ($node);} function visitCavalry (Cavalry $node) {/ / .$ this- > visit ($node);} function visitLaserCannonUnit (LaserCannonUnit $node) {/ / .$ this- > visit ($node);} function visitTroopCarrierUnit (Cavalry $node) {/ / .$ this- > visit ($node) } function visitArmy (Cavalry $node) {/ / .$ this- > visit ($node);}} / / this visitor class is mainly used to obtain and save information about the visited object class TextDumpArmyVisitor extends ArmyVisitor {private $text = ""; function visit (Unit $node) {$ret = "; $pad = 4 * $node- > getDpth (); $ret. = sprintf ("% {$pad} s ",") $ret. = get_class ($node). ":"; $ret. = "bombard:". $node- > bombardStrength (). "\ n"; $this- > text. = $ret;} function getText () {return $this- > text;}} / / for the visitor class taxing each object, class TaxCollectionVisitor extends ArmyVisitor {private $due=0; private $report = "; function visit (Unit $node) {$this- > levy ($node,1) will be called in client code 2 } function visitArcher (Archer $node) {/ / overrides the parent method, levying different taxes on different units $this- > levy ($node,2);} function visitCavalry (Cavalry $node) {$this- > levy ($node,3);} function visitTroopCarrierUnit (TroopCarrierUnit $node) {$this- > levy ($node,5) } private function levy (Unit $unit,$amount) {/ / main business logic $this- > report. = "Tax levied for". Get_class ($unit); $this- > report. = ": $amount\ n"; $this- > due + = $amount;} function getReport () {return $this- > report;} function getTax () {return $this- > due;}} / / client code 1 (get and output some information for each object) class UnitScript {static function joinExisting (Unit $newUnit,Unit $occupyingUnit) {$comp If (! is_null ($com = $occupyingUnit- > getComposite ()) {$comp- > addUnit ($newUnit);} else {$comp = new Army (); $comp- > addUnit ($occupyingUnit); $com- > addUnit ($newUnit);} return $comp;}} $main_army = new Army (); UnitScript::joinExisting (new Archer (), $main_army) UnitScript::joinExisting (new LaserCannonUnit (), $main_army); UnitScript::joinExisting (new Cavalry (), $main_army); $textdump = new TextDumpArmyVisitor (); $main_army- > accept ($textdump); print $textdump- > getText (); / / client code 2 (taxing each object, how much is levied on the final output) $main_army = new Army (); UnitScript::joinExisting (new Archer (), $main_army); UnitScript::joinExisting (new LaserCannonUnit (), $main_army) UnitScript::joinExisting (new Cavalry (), $main_army); $taxcollector = new TaxCollectionVisitor (); $main_army- > accept ($taxcollector); print $taxcollector- > getTax (); / / the above code was not tested because it was too lazy. Sorry! If you are interested, just run and debug it yourself. At this point, I believe that you have a deeper understanding of "the usage of visitor pattern and composition pattern in PHP object-oriented". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.