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 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Laravel - 변환된 유효성 검사 규칙으로 API 요청 제공동적 콘텐츠를 위해 API를 통해 Laravel CMS에 연결하는 모바일 앱(또는 웹사이트) 구축을 고려하십시오. 이제 앱은 CMS에서 번역된 콘텐츠를 받을 것으로 예상되는 다국어 앱이 될 수 있습니다. 일반적으로 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.