PHP面向对象程序设计组合模式与装饰模式详解(2)

<?php class UnitException extends Exception {} abstract class Unit { abstract function bombardStrength(); function addUnit( Unit $unit ) { throw new UnitException( get_class($this)." is a leaf" ); } function removeUnit( Unit $unit ) { throw new UnitException( get_class($this)." is a leaf" ); } } class Archer extends Unit { function bombardStrength() { return 4; } } class LaserCannonUnit extends Unit { function bombardStrength() { return 44; } } class Army extends Unit { private $units = array(); function addUnit( Unit $unit ) { if ( in_array( $unit, $this->units, true ) ) { return; } $this->units[] = $unit; } function removeUnit( Unit $unit ) { // >= php 5.3 //$this->units = array_udiff( $this->units, array( $unit ), // function( $a, $b ) { return ($a === $b)?0:1; } ); // < php 5.3 $this->units = array_udiff( $this->units, array( $unit ), create_function( '$a,$b', 'return ($a === $b)?0:1;' ) ); } function bombardStrength() { $ret = 0; foreach( $this->units as $unit ) { $ret += $unit->bombardStrength(); } return $ret; } } // create an army $main_army = new Army(); // add some units $main_army->addUnit( new Archer() ); $main_army->addUnit( new LaserCannonUnit() ); // create a new army $sub_army = new Army(); // add some units $sub_army->addUnit( new Archer() ); $sub_army->addUnit( new Archer() ); $sub_army->addUnit( new Archer() ); // add the second army to the first $main_army->addUnit( $sub_army ); // all the calculations handled behind the scenes print "attacking with strength: {$main_army->bombardStrength()}\n"; ?>

output:

attacking with strength: 60

更牛逼的组合处理,

<?php abstract class Unit { function getComposite() { return null; } abstract function bombardStrength(); } abstract class CompositeUnit extends Unit { // 抽象类继承抽象类 private $units = array(); function getComposite() { return $this; } protected function units() { return $this->units; } function removeUnit( Unit $unit ) { // >= php 5.3 //$this->units = array_udiff( $this->units, array( $unit ), // function( $a, $b ) { return ($a === $b)?0:1; } ); // < php 5.3 $this->units = array_udiff( $this->units, array( $unit ), create_function( '$a,$b', 'return ($a === $b)?0:1;' ) ); } function addUnit( Unit $unit ) { if ( in_array( $unit, $this->units, true ) ) { return; } $this->units[] = $unit; } } class Army extends CompositeUnit { function bombardStrength() { $ret = 0; foreach( $this->units as $unit ) { $ret += $unit->bombardStrength(); } return $ret; } } class Archer extends Unit { function bombardStrength() { return 4; } } class LaserCannonUnit extends Unit { function bombardStrength() { return 44; } } class UnitScript { static function joinExisting( Unit $newUnit, Unit $occupyingUnit ) { // 静态方法,直接通过类名来使用 $comp; if ( ! is_null( $comp = $occupyingUnit->getComposite() ) ) { // 军队合并处理 $comp->addUnit( $newUnit ); } else { // 士兵合并处理 $comp = new Army(); $comp->addUnit( $occupyingUnit ); $comp->addUnit( $newUnit ); } return $comp; } } $army1 = new Army(); $army1->addUnit( new Archer() ); $army1->addUnit( new Archer() ); $army2 = new Army(); $army2->addUnit( new Archer() ); $army2->addUnit( new Archer() ); $army2->addUnit( new LaserCannonUnit() ); $composite = UnitScript::joinExisting( $army2, $army1 ); print_r( $composite ); ?>

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 ( ) ) ) ) )

点评:Unit 基础,CompositeUnit复合中实现add与remove。军队继承Composite,射手继承Archer。这样射手中就不会有多余的add与remove方法了。

装饰模式

装饰模式帮助我们改变具体组件的功能。

看例子

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/7e02596eb2cf7b6e1d319e92d6386472.html