This article mainly introduces the simple factory mode of php design mode, also known as the static factory method mode. it is an important PHP design mode, if you need it, you can refer to the simple factory mode of the PHP design mode described in detail in this article in the form of an instance. it can be used as a reference for PHP program design. The details are as follows:
I. concepts
Simple Factory mode [Static Factory Method] (Static Factory Method)
Is the class creation mode
Several forms of factory models:
1. Simple Factory is also called Static Factory Method)
2. Factory Method is also called Polymorphic Factory)
3. Abstract Factory is also called ToolKit)
II. graph analysis:
III. code example
The instance code can be run after testing, as follows:
<? Php/*** example: ** a farm needs to sell fruits to the market. * There are three types of fruits, apples, and grapes in the farm. * We imagine: 1. fruits have multiple attributes, each attribute is different,, they share a common place | growth, planting, receiving, and eating * 2. new fruits may be added in the future. we need to define an interface to regulate the methods they must implement * 3, we need to obtain a fruit class, to get an instance of a fruit from a farmer, to learn how to grow, plant, receive, eat * // *** virtual product interface class * define the method to be implemented */interface fruit {/*** grow */public function grow (); /*** planting */public function plant ();/*** harvesting */public function harvest ();/*** eat */public function eat ();} /*** define a specific product Apple ** First, we need to implement the method defined by the inherited interface * and then define the unique attributes of Apple, and the method */class apple implements fruit {// apple tree has Age private $ treeAge; // apple has color 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 ";} // Retrieve the apple tree age public function getTreeAge () {return $ this-> treeAge;} // Set the apple tree age public function setTreeAge ($ age) {$ this-> treeAge = $ age; return trie ;}/ *** define a specific product grape * First, we need to implement the method defined by the inherited interface * and then define the unique attributes of the grape and the method */class grape implements fruit {// indicates whether the grape has seeds 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 ;} // setSeedLess ($ seed) {$ this-> seedLess = $ seed; return true ;}} /*** the farmer class is used to obtain the instantiated fruit **/class farmer {// defines a static factory method 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 for getting fruit instantiation */try {$ appleInstance = farmer :: factory ('apple'); var_dump ($ appleInstance);} catch (badFruitException $ err) {echo $ err-> msg. "_______". $ err-> errType ;}
I hope the examples described in this article will be helpful for PHP programming.