PHP design mode Simple Factory mode, detailed PHP design pattern
This paper introduces the simple Factory mode of PHP design mode in detail, which is very useful for PHP programming. Specific as follows:
First, the concept
Simple Factory mode "static Factory method Mode" (Static Factory methods)
is the creation mode of the class
Several forms of Factory mode:
1. Simple Factory mode (Factory) is also called Static Factory method mode (Factory)
2, factory method mode (Factory) is also known as the polymorphism of the Factory mode (polymorphic Factory)
3, abstract Factory mode (abstract Factory) is also called Toolbox mode (ToolKit)
Second, the distribution diagram analysis:
Third, code example
The instance code is tested to run, as follows:
<?php/** * An example of a farm that sells fruit to the market * There are three kinds of fruit apples and grapes in the farm. We envision: 1. Fruit has a variety of properties, each with different properties, but they have a common place | Grow, grow, receive, eat * 2, in the future it is possible to add new fruit, we need to define an interface to standardize the method they must implement * 3, we need to obtain a fruit of the class, from the farmer to obtain an example of a fruit, to know how to grow, grow, receive, eat *//** * Virtual Product Interface Class * Defines the method that needs to be implemented */interface fruit{/** * growth */Public function grow (); /** * * Planting */Public function plant (); /** * Harvest * * Public function harvest (); /** * * Eat */Public function eat (); }/** * Defining specific product classes Apple * First, we want to implement the method defined by the inherited interface * and then define Apple's unique properties, as well as 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"; }//Take Apple Tree Age public Function gettreeage () {return $this->treeage; }//Set Apple tree's age public Function settreeage ($age) {$this->treeage = $age; return trie; }}/** * Defining specific product classes grapes * First, we want to implement the method defined by the inherited interface * and then define the grape specialThere are properties, as well as methods */class grape implements fruit{//grape seed 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"; }//have no seed value public function getseedless () {return $this->seedless; }//Set seed-free public Function setseedless ($seed) {$this->seedless = $seed; return true; }}/** * Farmer class used to get instantiated fruit * */class farmer{//define 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; }}/** * Get fruit instantiation method */try{$appleInstance = farmer::factory (' Apple '); Var_dump ($appleInstance);} catch (Badfruitexception $err) {echo $err->msg. "_______" . $err->errtype;}
We hope that the examples described in this article will help you with PHP programming.
PHP design Pattern: Write the PHP5 sample code for factory and single mode
Example #1 Call factory method (with parameters)
Class Example
{
The Parameterized factory method
public static function factory ($type)
{
if (include_once ' drivers/'. $type. '. php ') {
$classname = ' Driver_ '. $type;
return new $classname;
} else {
throw new Exception (' Driver not found ');
}
}
}
?>
------------------------------------
Example #2 Single case mode
Class Example
{
Save the class instance in this property
private static $instance;
The construction method is declared private, preventing the object from being created directly
Private Function __construct ()
{
Echo ' I am constructed ';
}
Singleton method
public static function Singleton ()
{
if (!isset (self:: $instance)) {
$c = __class__;
Self:: $instance = new $c;
}
Return self:: $instance;
}
Common methods in the example class
Public Function bark ()
{
Echo ' woof! ';
}
Prevent users from replicating object instances
Public Function __clone ()
{
Trigger_error (' Clone is not allowed. ', e_user_error);
}
}
?>
Design mode Simple Factory mode
A factory is a place where a production instance is made. That's a simple one, directly named CreateInstance (). This method is usually static and has parameters and return values. For example: cattle, sheep, horses, dogs all inherit the animal class, that CreateInstance () The return value should be animal (because the factory is the production of animals, so the return value should also be an animal), the parameters should be animal name (so the factory according to the name to know what kind of animal you want to produce AH). This makes it possible to produce an animal instance based on the name of the animal being passed in. CreateInstance implementation: switch (animal name) Case Cow: return new Cow (), Case sheep: return new sheep (), Case horse: return new horse (); Case dog: return new dog ();
http://www.bkjia.com/PHPjc/874635.html www.bkjia.com true http://www.bkjia.com/PHPjc/874635.html techarticle PHP design mode of the simple factory model, the PHP design pattern in detail in this paper, in the form of a detailed introduction of PHP design mode of the simple Factory mode, for PHP programming ...