• PHP常见设计模式


    • 简单工厂
    <?php
     # 简单工厂
     abstract class mycoach
     {
         public function __construct(){}
     }
     
     class cultivatecpc extends mycoach{
         public function __construct(){
             echo "把陈培昌培养为搏击教练"."
    ";
         }
     }
     
     class cultivatecj extends mycoach{
         public function __construct(){
             echo "把程劲培养为搏击教练"."
    ";
         }
     }
     
     class martialclub 
     {
         public function cultivate($name)
         {
             switch($name){
                 case 'cj':
                     return new cultivatecj();
                 case 'cpc':
                     return new cultivatecpc();
                 default:
                     echo '培养一个小教练'."
    ";
                     break;
             }
             return null;
         }
     }
     class clubmaster{
         public function cultivate()
         {
             $myclub = new martialclub();
             $myclub->cultivate('cj');
             $myclub = new martialclub();
             $myclub->cultivate('cpc');
         }
     }
     
     $mymaster = new clubmaster();
     $mymaster->cultivate();

    输出结果:

    把程劲培养为搏击教练
    把陈培昌培养为搏击教练
    •  (复杂)工厂
    <?php
    abstract class mycoach
    {
        public function __construct()
        {}
    }
    
    class cpc extends mycoach
    {
        public function __construct()
        {
            echo "培养出了搏击教练昌仔"."
    ";
        }
    }
    
    class cj extends mycoach
    {
        public function __construct()
        {
            echo "培养出了搏击教练劲儿弟弟"."
    ";  
        }
    }
    
    interface cultivate{
        function cultivate();
    }
    
    class cultivatecpc implements cultivate
    {
        public function cultivate()
        {
            return new cpc();
        }
    }
    
    class cultivatecj implements cultivate
    {
        public function cultivate()
        {
            return new cj();
        }
    }
    
    class coach{
        public function __construct()
        {
            $coach = new cultivatecpc();
            $coach->cultivate();
            
            $coach = new cultivatecj();
            $coach->cultivate();
        }
    }
    
    $littlecoach = new coach();

    输出结果:

    培养出了搏击教练昌仔
    培养出了搏击教练劲儿弟弟
    •  观察者模式
    <?php
     class watcher
     {
         private $name;
         public function __construct($myname){
             $this->name=$myname;
         }
         public function update($food)
         {
             switch($food)
             {
                 case '奶油蛋糕':
                     echo $food."上桌了,".$this->name.'取出了一份纸盘子和塑料叉子,切了一块'."
    ";
                     break;
                 case '烤鸭':
                     echo $food."上桌了,".$this->name.'取了张薄饼,夹了些许葱丝,刷了些甜面酱'."
    ";
                     break;
                 default:
                     echo $food."上桌了,".$this->name."赶紧加了两筷子"."
    ";
                     break;
             }
             return null;
         }
     }
     
     class watchee
     {
         private $watchlist = [];
         public function addwatcher($watcher)
         {
             $this->watchlist[]=$watcher;
         }
         public function removewatcher($watcher)
         {
             $target = array_search($watcher,$this->watchlist);
             $target = (int)$target;
             unset($this->watchlist[$target]);
             //echo $target."
    ";
         }
         public function loaddish($dishname)
         {
             $this->notify($dishname);
         }
         public  function  notify($dishname)
         {
          #这里推荐用foreach循环,for循环带来的陷阱是,如果以数字为索引,执行removewatcher方法后,有些索引就没了,访问不到就会报错
        foreach($this->watchlist as $k=>$v)
           {
                 $this->watchlist[$k]->update($dishname);
           }
         }
         
     }
     
     $cpc = new watcher('陈培昌');
     $cj = new watcher('程劲');
     $observee = new watchee();
     $observee->addwatcher($cpc);
     $observee->addwatcher($cj);
     $observee->loaddish('葱香四川凉面');
     $observee->removewatcher($cj);
     $observee->loaddish('奶油蛋糕');
     $xxd = new watcher('徐晓冬');
     $observee->addwatcher($xxd);
     $observee->loaddish('避风塘茄子');

    输出结果:

    葱香四川凉面上桌了,陈培昌赶紧加了两筷子
    葱香四川凉面上桌了,程劲赶紧加了两筷子
    奶油蛋糕上桌了,陈培昌取出了一份纸盘子和塑料叉子,切了一块
    避风塘茄子上桌了,陈培昌赶紧加了两筷子
    避风塘茄子上桌了,徐晓冬赶紧加了两筷子
  • 相关阅读:
    Scramble String
    Burst Balloons
    Coins in a Line III
    maven初识
    Java类加载器初识
    HTTP协议
    Map和Set的联系
    Thread类与Runnable接口
    Throwable和Exception的区别
    SpringMVC的@ResponseBody注解简介
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/11330426.html
Copyright © 2020-2023  润新知