Php design mode policy mode, php design mode
Rule mode:
Encapsulate a group of specific behaviors and algorithms into classes to adapt to certain context environments;
For example, in an e-commerce website system, for male and female users, they must jump to different product categories, and all advertising spaces should display different advertisements.
UserStrategy. php
<?phpnamespace Baobab;interface UserStrategy{ function showAd(); function showCategory();}?>
FemaleUserStrategy. php
<? Phpnamespace Baobab; class FemaleUserStrategy implements UserStrategy {function showAd () {echo '2017 new women's clothing ';} function showCategory () {echo 'Women's clothing'; }}?>
MaleUserStrategy. php
<? Phpnamespace Baobab; class MaleUserStrategy implements UserStrategy {function showAd () {echo 'iphone6s plus ';} function showCategory () {echo 'electronic product' ;}}?>
Index. php
class Page{ protected $strategy; function Index(){ $this->strategy->showAd(); echo '<br/>'; $this->strategy->showCategory(); } function setStrategy(Baobab\UserStrategy $strategy){ $this->strategy = $strategy; }}$page = new Page();if (isset($_GET['female'])){ $strategy = new Baobab\FemaleUserStrategy();}else{ $strategy = new Baobab\MaleUserStrategy();}$page->setStrategy($strategy);$page->Index();
Ioc can be implemented using the Policy mode, with dependency inversion and control inversion.