php 디자인 모델 의 간단 한 공장 모델 상세 설명

3938 단어 php디자인 모드
본 고 는 사례 형식 으로 PHP 디자인 모델 의 간단 한 공장 모델 을 상세 하 게 소개 하여 PHP 프로 그래 밍 을 하 는 데 좋 은 참고 역할 을 한다.구체 적 으로 다음 과 같다.
개념
단순 공장 모드[정적 공장 방법 모드](Static Factory Method)
클래스 생 성 모드 입 니 다.
공장 모델 의 몇 가지 형태:
1.단순 공장 모델(Simple Factory)을 정적 공장 방법 모델(Static Factory Method)이 라 고도 부른다.
2.공장 방법 모델(Factory Method)을 다 형 공장 모델(Polymorphic Factory)이 라 고도 부른다.
3.추상 적 인 공장 모델(Abstract Factory)을 공구 상자 모델(ToolKit)이 라 고도 부른다.
2.배치 도 분석:

3.코드 인 스 턴 스
이 인 스 턴 스 코드 는 테스트 를 통 해 실 행 될 수 있 습 니 다.구체 적 으로 다음 과 같 습 니 다.

<?php
/**
 *     
 *
 *     ,        
 *            、  
 *     :1、       ,        ,  ,         |   、  、  、 
 *       2、            、                      
 *       3、            ,                 ,       、  、  、 
 */


/**
 *        
 *           
 */

interface fruit{

  /**
   *   
   */
  public function grow();

  /**
   *   
   */
  public function plant();

  /**
   *   
   */
  public function harvest();

  /**
   *  
   */
  public function eat();
  
}

/**
 *           
 *   ,                 
 *             ,    
 */
class apple implements fruit{

  //      
  private $treeAge;

  //     
  private $color;

  public function grow(){
    echo "grape grow";
  }

  public function plant(){
    echo "grape plant";
  }

  public function harvest(){
    echo "grape harvest";
  }

  public function eat(){
    echo "grape eat";
  }

  //       
  public function getTreeAge(){
    return $this->treeAge;
  }

  //        
  public function setTreeAge($age){
    $this->treeAge = $age;
    return trie;
  }

}

/**
 *           
 *   ,                 
 *             ,    
 */
class grape implements fruit{

  //      
  private $seedLess;

  public function grow(){
    echo "apple grow";
  }

  public function plant(){
    echo "apple plant";
  }

  public function harvest(){
    echo "apple harvest";
  }

  public function eat(){
    echo "apple eat";
  }

  //     
  public function getSeedLess(){
    return $this->seedLess;
  }

  //      
  public function setSeedLess($seed){
    $this->seedLess = $seed;
    return true;
  }
}

/**
 *               
 *
 */
class farmer{

  //         
  public static function factory($fruitName){
    switch ($fruitName) {
      case 'apple':
        return new apple();
        break;
      case 'grape':
        return new grape();
        break;
      default:
        throw new badFruitException("Error no the fruit", 1);
        break;
    }
  }
}

class badFruitException extends Exception{
  public $msg;
  public $errType;
  public function __construct($msg = '' , $errType = 1){
    $this->msg = $msg;
    $this->errType = $errType;
  }  
}

/**
 *           
 */
try{
  $appleInstance = farmer::factory('apple');
  var_dump($appleInstance);
}catch(badFruitException $err){
  echo $err->msg . "_______" . $err->errType;
}

본 논문 에서 말 한 사례 가 여러분 의 PHP 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기