PHP simple factory pattern example PHP design pattern introductory tutorial
Release: 2016-07-25 08:52:50
Original
1104 people have browsed it
/**
- * An example
- *
- * A farm wants to sell fruits to the market
- * There are three kinds of fruits on the farm, apples and grapes
- * We imagine: 1. Fruits have many attributes, and each attribute is different, but they have Common place | Growing, planting, receiving, eating
- * 2. New fruits may be added in the future. We need to define an interface to standardize the methods they must implement
- * 3. We need to obtain the class of a certain fruit, You need to get examples of a certain fruit from the farmer to know how to grow, plant, harvest and eat
- */
/**
- * Virtual product interface class
- * Define the methods that need to be implemented
- */
interface fruit{
/**
- *Growth
- */
- public function grow();
/**
- *Planting
- */
- public function plant();
/**
- * Harvest
- */
- public function harvest();
/**
- * Eat
- */
- public function eat();
- }
/**
- * Define the specific product category Apple
- * First, we need to implement the methods defined by the inherited interface
- * Then define Apple-specific attributes and methods
- */
- 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;
- }
}
/**
- * Define the specific product class Grape
- * First, we need to implement the methods defined by the inherited interface
- * Then define the unique attributes and methods of Grape
- */
- 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;
- }
- }
/**
- *Farmer class is used to obtain instantiated fruits
- *
- */
- 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;
- }
- }
/**
- * Method to obtain fruit instantiation
- */
- try{
- $appleInstance = farmer::factory('apple');
- var_dump($appleInstance);
- }catch(badFruitException $err){
- echo $err->msg . "_______" . $err->errType;
- }
复制代码
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-08-25 12:40:24
-
2024-08-25 12:35:24
-
2024-08-25 12:31:38
-
2024-08-25 12:30:55
-
2024-08-25 12:26:24
-
2024-08-25 12:21:24
-
2024-08-25 12:16:24
-
2024-08-25 12:15:24
-
2024-08-25 12:13:24
-
2024-08-25 12:11:24