PHP common design patterns

  • Simple Factory
? < PHP 
 # simple plant 
 abstract  class mycoach 
 { 
     public function the __construct () {} 
 } 
 
 class cultivatecpc the extends mycoach {
      public function the __construct () { 
         echo " to Chen Peichang culture as fighting trainer " . " \ N- " ; 
     } 
 } 
 
 class cultivatecj the extends mycoach {
      public function the __construct () { 
         echo " the Cheng Jinpei to keep fighting trainer " . " \ n- " ; 
     } 
 } 
 
 classmartialclub 
 { 
     public function cultivate ($ name) 
     { 
         Switch ($ name) {
              Case  ' CJ ' :
                  return  new new cultivatecj ();
              Case  ' CPC ' :
                  return  new new cultivatecpc ();
              default : 
                 echo ' culture a small coach ' . " \ n- " ;
                  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();

Output:

Cheng Jinpei is fighting to keep the coach 
to train for the wrestling coach Chen Peichang

 

Guess you like

Origin www.cnblogs.com/saintdingspage/p/11330426.html