Php design mode-simple factory mode (static factory method mode), php design mode _ PHP Tutorial

Source: Internet
Author: User
Php design mode-simple factory mode (static factory method mode), php design mode. Php design mode-simple factory mode (static factory method mode), php design mode concept simple factory mode [static factory method mode] (StaticFactoryMethod) is the class creation mode php design mode-simple factory mode (static factory method mode), php design mode

Simple Factory mode [Static Factory Method] (Static Factory Method)
Is the class creation mode

Several forms of factory models:
1. Simple Factory | also called Static Factory Method)
2. Factory Method | also called Polymorphic Factory)
3. Abstract Factory | ToolKit)

Matching diagram

Code instance

Run the code directly. all of them are tested.

 TreeAge; 76} 77 78 // Set the apple tree age 79 public function setTreeAge ($ age) {80 $ this-> treeAge = $ age; 81 return trie; 82} 83 84} 85 86/** 87 * define a specific product grape 88 * First, we need to implement the method 89 defined by the inherited interface and then define the unique attributes of the grape, method 90 */91 class grape implements fruit {92 93 94 // indicates whether the grape has seeds 95 private $ seedLess; 96 97 public function grow () {98 echo "apple grow "; 99} 100 101 public function plant () {102 echo "apple plant"; 103} 104 105 public function harvest () {106 echo "apple harvest "; 107} 108 109 public function eat () {110 echo "apple eat"; 111} 112 113 // seeless value 114 public function getSeedLess () {115 return $ this-> seedLess; 116} 117 118 // set seed seedLess 119 public function setSeedLess ($ seed) {120 $ this-> seedLess = $ seed; 121 return true; 122} 123 124 125 126/** 127 * farmer class used to obtain instantiated fruit 128*129 */130 class farmer {131 132 133 // define a static factory method 134 public static function factory ($ fruitName) {135 switch ($ fruitName) {136 case 'apple': 137 return new apple (); 138 break; 139 case 'grape': 140 return new grape (); 141 break; 142 default: 143 throw new badFruitException ("Error no the fruit", 1); 144 break; 145} 146} 147 148 149 class badFruitException extends Exception {150 public $ msg; 151 public $ errType; 152 public function _ construct ($ msg = '', $ errType = 1) {153 $ this-> msg = $ msg; 154 $ this-> errType = $ errType; 155} 156 157 158/** 159 * method for obtaining fruit instantiation 160 */161 try {162 $ appleInstance = farmer :: factory ('apple'); 164 var_dump ($ appleInstance); 165} catch (badFruitException $ err) {166 echo $ err-> msg. "_______". $ err-> errType; 167}

Php design mode: write the PHP5 sample code of the factory mode and single-state mode.

Example #1 call the 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 Singleton mode

Class Example
// Save the class instance in this attribute
Private static $ instance;

// The constructor is declared as private to prevent direct object creation.
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! ';

// Prevents users from copying object instances
Public function _ clone ()
Trigger_error ('Clone is not allowed. ', E_USER_ERROR );



What are the similarities and differences between the simple factory model and the factory method model?

It is recommended that LZ study the simple factory, Factory method and abstract factory together. haha, my teacher asked me to study the differences between the three models, so I may be more familiar with learning together.
The simple factory mode is also called the static factory method mode. After renaming, we can see that this mode must be very simple. Its purpose is to define an interface for creating objects.
Let's take a look at its composition:
1) factory roles: this is the core of this model and contains some commercial logic and judgment logic. In java, it is often implemented by a specific class.
2) abstract product role: it is generally the parent class or implemented interface inherited by a specific product. It is implemented by interfaces or abstract classes in java.
3) specific product role: the object created by the factory class is the instance of this role. It is implemented by a specific class in java.
The factory method mode removes the static attribute of the factory method in the simple factory mode so that it can be inherited by the quilt class. In this way, the pressure on the factory method in the simple factory mode can be shared by different factory subclass in the factory method mode.
Take a look at its composition:
1) Abstract factory role: this is the core of the factory method model and has nothing to do with the application. It is an interface that must be implemented by a specific factory role or a parent class that must be inherited. In java, it is implemented by abstract classes or interfaces.
2) specific factory role: it contains code related to the specific business logic. An application is called to create the objects of a specific product.
3) abstract product role: it is the parent class or implemented interface inherited by a specific product. In java, abstract classes or interfaces are generally used for implementation.
4) specific product role: the object created by the specific factory role is the instance of this role. It is implemented by specific classes in java.

Quiet-simple Factory mode (Static Factory Method mode), php design mode concept simple Factory mode [Static Factory Method mode] (Static Factory Method) is the class creation mode...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.